Skip to content

Conversation

@ChayimFriedman2
Copy link
Contributor

Lang items rarely change, so putting a query for each doesn't give us anything. On the other hand, putting them behind only one query not only saves memory, it also has a giant benefit: we can store the struct with all lang items in the interner, making access to them very cheap. That basically means that anything in the hot path can avoid a very common query, and exchange it for a simple field access.

This saves 3 seconds on analysis-stats on self. I can't measure the impact on memory as it fluctuates wildly due to some recent changes, but I believe the impact will be positive, and definitely not negative.

Reviewing commit-by-commit is recommended. The first commit is more technical and could use a skim review.

Via separate methods.

This is both more clear, shorter, and will be required for the next commit.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 28, 2025
}
"#,
expect![[r#"
me random_method(…) (use dep::test_mod::TestTrait) fn(&self) DEPRECATED
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should do something about this test, the order changes every now and then.

@ChayimFriedman2 ChayimFriedman2 force-pushed the lang-items branch 2 times, most recently from 24c68f5 to d3d0845 Compare November 28, 2025 00:56
Lang items rarely change, so putting a query for each doesn't give us anything. On the other hand, putting them behind only one query not only saves memory, it also has a giant benefit: we can store the struct with all lang items in the interner, making access to them very cheap. That basically means that anything in the hot path can avoid a *very common* query, and exchange it for a simple field access.
@ChayimFriedman2
Copy link
Contributor Author

So I checked and this saves 7mb. Cute. Speed impact is more real.

(However thinking about it it's amusing: those 7mb were likely caused, almost all of them, only by query dependencies!)

/// Salsa query. This will look for lang items in a specific crate.
#[salsa_macros::tracked(returns(ref))]
pub fn crate_lang_items(db: &dyn DefDatabase, krate: Crate) -> Option<Box<LangItems>> {
pub fn crate_lang_items(db: &dyn DefDatabase, krate: Crate) -> LangItems {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep the Option<Box<_>> here, 99.9% of all crates will have this by completely empty after all, unlike the lang_items query

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't really matter and makes the code a tad more complicated, having few extra kilobytes per crate won't matter for memory usage.

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Nov 29, 2025
Merged via the queue into rust-lang:master with commit e326797 Nov 29, 2025
15 checks passed
@ChayimFriedman2 ChayimFriedman2 deleted the lang-items branch November 29, 2025 16:29
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants