Skip to content

Conversation

@jasonmalinowski
Copy link
Member

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.

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

WithFor

nit: name is odd

public void UpdateCacheWithForGeneratorDriver(GeneratorDriver driver)
{
ImmutableInterlocked.TryRemove(ref _driverCache, projectId, out _);
_driverCache = AsyncLazy.Create(driver);
Copy link
Member

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);
Copy link
Member

Choose a reason for hiding this comment

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

Yaay

@CyrusNajmabadi
Copy link
Member

Thanks for this. It's a lot cleaner/clearer!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants