-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Change logic for how we cache GeneratorDrivers to avoid initialization #81323
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
base: main
Are you sure you want to change the base?
Change logic for how we cache GeneratorDrivers to avoid initialization #81323
Conversation
This makes a few general changes to how we cache drivers so we don't re-initialize them multiple times when we're first loading a solution. 1. Rather than the cache being a ProjectId -> cached driver map, we instead just have the object holding the cached driver held directly by the ProjectState. 2. Rather than clearing out the cached driver once we think we won't need it, we just continually refresh the cached driver with the latest version. This simplifies the logic and also ensures we won't ever clear something out that we would need later, but still keeps us from holding onto old state. 3. Rather than explicitly clearing out the initialized driver when we have to rerun generators, we keep it around.
| // 1. If some other fork of this Solution needs a GeneratorDriver created, it'll have one that's probably more update to date. | ||
| // This is obviously speculative -- if it's a really old Solution fork it might not help, but can't hurt for the more common cases. | ||
| // 2. It ensures that we're not holding an old GeneratorDriver alive, which itself may hold onto state that's no longer applicable. | ||
| this.ProjectState.GeneratorDriverCache.UpdateCacheWithForGeneratorDriver(generatorDriver); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| public void UpdateCacheWithForGeneratorDriver(GeneratorDriver driver) | ||
| { | ||
| ImmutableInterlocked.TryRemove(ref _driverCache, projectId, out _); | ||
| _driverCache = AsyncLazy.Create(driver); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cute. Definitely doc this field to explain this will happen. Ah. I see you did doc this well
| // Clear out the cache of any previously initialized GeneratorDriver. Otherwise we might reuse a | ||
| // driver which will not count as a new "run" in some of our unit tests. We have tests that very explicitly count | ||
| // and assert the number of invocations of a generator. | ||
| GeneratorDriverCache.EmptyCacheForProject(projectId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yaay
|
Thanks for this. It's a lot cleaner/clearer! |
This makes a few general changes to how we cache drivers so we don't re-initialize them multiple times when we're first loading a solution.