Skip to content

Adds RmetaLinkCache a per-link cache that uses path as the key of dec…#158194

Open
mehdiakiki wants to merge 1 commit into
rust-lang:mainfrom
mehdiakiki:rlib-link-meta-cache
Open

Adds RmetaLinkCache a per-link cache that uses path as the key of dec…#158194
mehdiakiki wants to merge 1 commit into
rust-lang:mainfrom
mehdiakiki:rlib-link-meta-cache

Conversation

@mehdiakiki

@mehdiakiki mehdiakiki commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Adds RmetaLinkCache a per link and path keyed cache of decoded lib.rmeta-link archive members and routes the add_archive read through it so that each rlib link metadata is decoded only one per link at most.

This is a request that originated from the discussion in #156735 and we split it out as its own PR. It gives that PR a decode once path to read instead of reparsing each rlib per crate, and this will be in effect once native_lib_filenames moves to a link-time read.

Part of #138243

…oded lib.rmeta-link archive members, and routes add_archive read through it so each rlib link metadata is decoded at most once per link. This is a demand that originated from the discussion in rust-lang#156735 and we split it out as its own PR. It gives that PR a decode once path tp read instead of reparsing each rlib per crate once  native_lib_filenames moves to a link time read.
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2026
@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: codegen, compiler
  • codegen, compiler expanded to 73 candidates
  • Random selection from 19 candidates

@rustbot

rustbot commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@mehdiakiki

Copy link
Copy Markdown
Contributor Author

r? @petrochenkov

@rustbot rustbot assigned petrochenkov and unassigned wesleywiser Jun 20, 2026

impl RmetaLinkCache {
pub fn get_or_insert_with(
&self,

@petrochenkov petrochenkov Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&self,
&mut self,

Any specific reason to use &self and RefCell instead of &mut and no RefCell?

View changes since the review

&self,
rlib_path: &Path,
load: impl FnOnce() -> Option<RmetaLink>,
) -> Option<Rc<RmetaLink>> {

@petrochenkov petrochenkov Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) -> Option<Rc<RmetaLink>> {
) -> Option<&RmetaLink> {

Is Rc necessary?
The current use in add_archive doesn't seem to require it.

View changes since the review

return cached.clone();
}
let loaded = load().map(Rc::new);
self.cache.borrow_mut().insert(rlib_path.to_path_buf(), loaded.clone());

@petrochenkov petrochenkov Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.cache.borrow_mut().insert(rlib_path.to_path_buf(), loaded.clone());
self.cache.borrow_mut().entry(rlib_path).or_insert_with(...);

Can simplify this and avoid the double lookup by using the entry API.

View changes since the review

let _timer = sess.timer("link_binary");
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
let mut tempfiles_for_stdout_output: Vec<PathBuf> = Vec::new();
let rmeta_link_cache = rmeta_link::RmetaLinkCache::default();

@petrochenkov petrochenkov Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let rmeta_link_cache = rmeta_link::RmetaLinkCache::default();
let rmeta_link_cache = RmetaLinkCache::default();

Nit: imports (in archive.rs too).

View changes since the review


pub enum AddArchiveKind<'a> {
Rlib(/*skip*/ &'a dyn Fn(&str, ArchiveEntryKind) -> bool),
Rlib { skip: &'a dyn Fn(&str, ArchiveEntryKind) -> bool, cache: &'a rmeta_link::RmetaLinkCache },

@petrochenkov petrochenkov Jun 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Rlib { skip: &'a dyn Fn(&str, ArchiveEntryKind) -> bool, cache: &'a rmeta_link::RmetaLinkCache },
Rlib(&'a RmetaLinkCache, /*skip*/ &'a dyn Fn(&str, ArchiveEntryKind) -> bool),

Style nit: not enough fields to convert to named fields, and closures usually go last.

View changes since the review

@petrochenkov

Copy link
Copy Markdown
Contributor

I thought about using CrateNum as a key, but this is good too.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 22, 2026
@rustbot

rustbot commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mehdiakiki

Copy link
Copy Markdown
Contributor Author

Ok will address the comments above!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants