Skip to content

Try to reuse initialized generator drivers when we can#80845

Merged
jasonmalinowski merged 3 commits intodotnet:mainfrom
jasonmalinowski:try-to-reuse-initialized-generatordrivers
Nov 4, 2025
Merged

Try to reuse initialized generator drivers when we can#80845
jasonmalinowski merged 3 commits intodotnet:mainfrom
jasonmalinowski:try-to-reuse-initialized-generatordrivers

Conversation

@jasonmalinowski
Copy link
Member

@jasonmalinowski jasonmalinowski commented Oct 22, 2025

We're seeing a performance issue in Razor projects in cohosting scenarios. The first run of the Razor generator is relatively slow, and so ideally we wouldn't have that happen more than once. However, since multiple features might request compilations for a Razor project during startup in parallel, it's possible that all of those parallel invocations might initialize the generator multiple times if they happen to be different Solution instances. This change tries to ensure that when we're creating a GeneratorDriver for a project when we don't already have one, we'll do that computation just once, and try to share that across the forks. This ensures we do the expensive initialization only once.

@jasonmalinowski jasonmalinowski force-pushed the try-to-reuse-initialized-generatordrivers branch from 5ce64cf to 931eb2f Compare October 22, 2025 23:35
CyrusNajmabadi

This comment was marked as outdated.

We're seeing a performnace issue in Razor projects in cohosting
scenarios. The first run of the Razor generator is relatively slow,
and so ideally we wouldn't have that happen more than once. However,
since multiple features might request compilations for a Razor project
during startup in parallel, it's possible that all of those parallel
invocations might initialize the generator multiple times if they
happen to be different Solution instances. This change tries to ensure
that when we're creating a GeneratorDriver for a project when we don't
already have one, we'll do that computation just once, and try to share
that across the forks. This ensures we do the expensive initialization
only once.
@jasonmalinowski jasonmalinowski force-pushed the try-to-reuse-initialized-generatordrivers branch from 931eb2f to 61ac7f7 Compare November 3, 2025 20:37
@jasonmalinowski jasonmalinowski marked this pull request as ready for review November 3, 2025 22:01
@jasonmalinowski jasonmalinowski requested a review from a team as a code owner November 3, 2025 22:01
// for it -- we can potentially reuse a GeneratorDriver created for another instance (after we update it to match.) The
// assumption here is by the time we're updating the CurrentSolution to point to a projects that have GeneratorDrivers,
// we won't need that cache anymore since any further requests for generated documents will use the properly held GeneratorDriver.
data.@this.GeneratorDriverCreationCache.EmptyCacheForProjectsThatHaveGeneratorDriversInSolution(newSolution.CompilationState);
Copy link
Contributor

Choose a reason for hiding this comment

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

i will admit that i haven't figure out what this comment means.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've give another attempt at a rewrite. (I say "another" only because there's a few unpushed versions that I really hated too...)

Copy link
Contributor

@CyrusNajmabadi CyrusNajmabadi Nov 4, 2025

Choose a reason for hiding this comment

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

ok. one final request. can you update the comment here to make it clear that this could mean we're dumping a driver that some forked could beneffit from. but that's ok as we're just trying to benefit some common scenarios, and we don't want to hold onto things forever. so we prefer keepign the cache clear once we feel it's likely served its purpose (or something like that).

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update after merge to keep this moving.

@jasonmalinowski jasonmalinowski force-pushed the try-to-reuse-initialized-generatordrivers branch from cc64b3b to 18775a6 Compare November 4, 2025 00:12
@jasonmalinowski jasonmalinowski merged commit 3620226 into dotnet:main Nov 4, 2025
25 checks passed
@jasonmalinowski jasonmalinowski deleted the try-to-reuse-initialized-generatordrivers branch November 4, 2025 01:44
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Nov 4, 2025
@jjonescz
Copy link
Member

jjonescz commented Nov 4, 2025

/backport to release/dev18.3

@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

Started backporting to release/dev18.3 (link to workflow run)

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.

5 participants