Skip to content
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

Stop cloning the index state #200

Merged
merged 1 commit into from
Sep 27, 2023
Merged

Conversation

spenserblack
Copy link
Owner

@spenserblack spenserblack commented Sep 27, 2023

This drastically improves performance by no longer cloning the index
state, which only needs to be set once.

Resolves #197
Closes #199

@spenserblack
Copy link
Owner Author

spenserblack commented Sep 27, 2023

@Byron Just tagging you because you might get a laugh at this. What a rookie mistake 🤦

Running this locally on a shallow clone of the linux repo, performance is now comparable to v0.6.0.

This drastically improves performance by no longer cloning the index
state, which only needs to be set once.

Resolves #197
@spenserblack spenserblack force-pushed the perf/do-not-clone-index-state branch from dd142fb to 1f4af1b Compare September 27, 2023 16:17
@codecov
Copy link

codecov bot commented Sep 27, 2023

Codecov Report

Merging #200 (1f4af1b) into main (2b1bf09) will decrease coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #200      +/-   ##
==========================================
- Coverage   82.69%   82.66%   -0.04%     
==========================================
  Files          16       16              
  Lines         572      571       -1     
==========================================
- Hits          473      472       -1     
  Misses         99       99              
Flag Coverage Δ
82.66% <100.00%> (-0.04%) ⬇️
macOS-latest 82.45% <100.00%> (-0.04%) ⬇️
ubuntu-latest 78.03% <87.50%> (+0.32%) ⬆️
windows-latest 77.19% <100.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
gengo/src/file_source/git.rs 91.54% <100.00%> (-0.12%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@Byron Byron left a comment

Choose a reason for hiding this comment

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

If this is a fix then I don't see it, but maybe it's faster than it was before and that's what this PR is about.

&self here must be &mut self, or there must be other means to access thread-local state mutably. Usually, this is passed in then.

To repeat: certain state must be present once per thread to be able to mutate it, and cloning isn't the solution.

I hope that helps.

.attr_stack
.at_path(path, Some(false), |id, buf| repo.objects.find_blob(id, buf))
let attr_matches = {
let mut attr_stack = self.state.attr_stack.clone();
Copy link
Contributor

Choose a reason for hiding this comment

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

Cloning attr-stack and attr-matches per path isn't the intended use - these need to be mutable. Creating a thread-local repo per path also isn't intended.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I was just expecting a 😆 emoji.

I probably didn't interpret your code correctly. This was adapted from this line:

move |_| (state.clone(), repo.to_thread_local()),

A GitState, which has attr_stack and attr_matches fields, gets cloned here to attain a mutable reference (or so I thought). I thought this closure would do exactly that: return a thread-local repo and state clone per entry, which is effectively per-path for our uses.

Copy link
Contributor

Choose a reason for hiding this comment

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

This line could be doing that, that's true. But what I see here still happily clones two structures in order to make them mutable. Also, repo.to_thread_local() is called once per path.

The problem is that the whole self seems to be tuned to be Sync and thus is read-only on top of that. The one coordinating the parallelism is responsible for passing in mutable state. I get it, each implementation has its own state, but if that's the case it must be a type parameter somewhere so it can be instantiated and passed in mutably.

Maybe this happened due to rayon which clearly prefers Sync (share immutable state or shared mutable state behind locks), but even with rayon there are ways to get thread-local state that I strongly recommend getting into this method call.

This really is all I can say here, so: Happy Rusting :)!

Copy link
Owner Author

Choose a reason for hiding this comment

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

Alright, thanks again! I do have a habit of avoiding mutable references in favor of immutable references, which influenced my design decisions here. Thanks for the insight!

@spenserblack
Copy link
Owner Author

If this is a fix then I don't see it

The new State struct wrapped a gix::index::State. So cloning that state resulted in the index::State getting cloned for each file 🙃

@spenserblack spenserblack merged commit 6be20ff into main Sep 27, 2023
@spenserblack spenserblack deleted the perf/do-not-clone-index-state branch September 27, 2023 17:37
@Byron
Copy link
Contributor

Byron commented Sep 27, 2023

I hope this will be fixed - it's not problem to achieve what needs to be done. Pretty please 🥺.

@spenserblack
Copy link
Owner Author

spenserblack commented Sep 27, 2023

Yeah, please don't consider the merge to be an end to the conversation. But this PR alone dropped gengo -R linux from about 90 seconds to 5 seconds locally, which feels significant enough for its own release. Cloning the attribute matches and stack might be bad, but cloning the index state was a really bad mistake I made in #191.

@spenserblack
Copy link
Owner Author

So this conversation doesn't get forgotten #202 😉

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.

Significant performance degredation
2 participants