Avoid allocating per-instance lazy factory delegates using function pointers - #707
Merged
Merged
Conversation
…ointers The object model wraps every lazily-computed Cursor/Decl/Type member in a ValueLazy<T> whose factory is a captured Func<T>. Because the factory closes over the owning instance, each wrapper allocates a delegate (and, for factories that capture additional state, a display class) up front in the constructor -- roughly 65 MB of Func<T> delegates when generating the Windows SDK, none of which are needed once the value is computed. Replace ValueLazy<T> with ValueLazy<TSelf, T>, whose factory is a static delegate*<TSelf, T> passed the owning instance explicitly. The function pointer is a static, so no per-instance delegate is allocated; the owning instance is threaded through GetValue(self) at the call site. Each captured lambda becomes a static factory method taking self. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every lazily-computed
Cursor/Decl/Typemember is wrapped in aValueLazy<T>whose factory is a capturedFunc<T>. Because the factory closes over the owning instance (and, in a number of cases, additional constructor state), each wrapper eagerly allocates a delegate -- and sometimes a display class -- in the constructor, none of which is needed once the value has been computed.This replaces
ValueLazy<T>withValueLazy<TSelf, T>, whose factory is astatic delegate*<TSelf, T>passed the owning instance explicitly. The function pointer is a static, so nothing is allocated per instance; the owning instance is threaded throughGetValue(self)at the call site. Each captured lambda becomes astaticfactory method takingself.Measured on the TerraFX Windows SDK generation (
DirectX/d3d12) withDOTNET_TieredCompilation=0(the NativeAOT-representative full-opt proxy the shipped tool runs under), sampledGCAllocationTick:Func<T>factory delegatesEvery
Func_1[...](String,Cursor,Decl,TranslationUnit,Type,IDeclContext, ...) is gone. The remainingFunc_2/Func_3allocations belong toLazyList, which is a separatesealed classand out of scope here.All 3706 generator tests pass and the build is warning-clean. -- The bulk of the diff is mechanical (326 fields, 327 factories across 172 files); it was produced with a Roslyn rewriter driven by the semantic model, so cases like the static
TranslationUnit.GetOrCreatevs. the instanceTranslationUnitproperty are disambiguated correctly rather than by text matching.Note
This PR body was drafted by Copilot.