Skip to content

Lazily allocate extra memo state #888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2025

Conversation

ibraheemdev
Copy link
Contributor

@ibraheemdev ibraheemdev commented May 29, 2025

Similar to #768, except also lazily allocating the CycleHeads. I did some profiling in ty, and on a cold run ~1% of memos have non-empty CycleHeads and ~0.5% create tracked structs, out of >2M memos on large projects. Lazily allocating saves a meaningful amount of memory for us (4 words on memos without extras) and doesn't affect performance for ty (if anything it seems to improve performance very slightly, but that may just be noise).

Copy link

netlify bot commented May 29, 2025

Deploy Preview for salsa-rs canceled.

Name Link
🔨 Latest commit 637fb28
🔍 Latest deploy log https://app.netlify.com/projects/salsa-rs/deploys/6839a726ab0507000843769c

@ibraheemdev ibraheemdev force-pushed the ibraheem/lazy-memos branch 5 times, most recently from 24d561e to 11fd385 Compare May 29, 2025 22:35
Copy link

codspeed-hq bot commented May 29, 2025

CodSpeed Performance Report

Merging #888 will not alter performance

Comparing ibraheemdev:ibraheem/lazy-memos (637fb28) with master (5750c84)

Summary

✅ 12 untouched benchmarks

@ibraheemdev
Copy link
Contributor Author

I'm tempted to also throw in AccumulatedMap. We don't currently use accumulators in ty, but this also risks being too coarse-grained and losing the benefit for other use cases, so I'm not sure.

Copy link
Contributor

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This is great. I wonder if it would simplify the code if QueryRevisionsExtra would abstract the Option<Box<Data>> internally. Curious to hear your perspective.

I think we could also move AccumulatedValues. Let me know if you want to do this in the same or a separate PR

Comment on lines 210 to 213
let extra = QueryRevisionsExtra {
tracked_struct_ids: mem::take(tracked_struct_ids),
cycle_heads: mem::take(cycle_heads),
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I think it would be nice if QueryRevisionsExtra could abstract the fact that it is an Option<Box<Data>> internally to keep the code here simpler (QueryRevisions::new(final, tracked_struct_ids, cycle_heads))

src/function.rs Outdated
@@ -191,7 +191,10 @@ where
mut memo: memo::Memo<C::Output<'db>>,
memo_ingredient_index: MemoIngredientIndex,
) -> &'db memo::Memo<C::Output<'db>> {
memo.revisions.tracked_struct_ids.shrink_to_fit();
if let Some(extra) = &mut memo.revisions.extra {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. I think I'd prefer if the call is memo.revisions.extra.shrink_to_fit

@@ -328,6 +328,30 @@ pub(crate) struct QueryRevisions {
/// How was this query computed?
pub(crate) origin: QueryOrigin,

pub(super) accumulated: Option<Box<AccumulatedMap>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

We could probably move AccumulatedMap too

I assume you didn't move accumualted_inputs because it fits into what otherwise would be padding (similar to verified_final)

Copy link
Member

@Veykril Veykril May 30, 2025

Choose a reason for hiding this comment

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

I'd say lets move accumulated map in there as well, given we already have it lazily allocated (and neither you folks or rust-analyzer seem to use it much at the moment). In fact I still don't see a future where r-a wants to use it tbh 😅

Though I guess moving that in there as well makes using any of the other features (cycles, tracked structs) more expensive again.

@ibraheemdev ibraheemdev force-pushed the ibraheem/lazy-memos branch 3 times, most recently from af9f582 to 4fb2520 Compare May 30, 2025 12:35
@ibraheemdev ibraheemdev force-pushed the ibraheem/lazy-memos branch from 4fb2520 to 637fb28 Compare May 30, 2025 12:40
@MichaReiser MichaReiser added this pull request to the merge queue May 30, 2025
Merged via the queue into salsa-rs:master with commit 0c39c08 May 30, 2025
12 checks passed
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