Eliminate closure allocation by referencing UriString property instead of captured uriString parameter#81948
Merged
jasonmalinowski merged 1 commit intodotnet:mainfrom Jan 9, 2026
Conversation
…d of captured uriString parameter in DocumentUri ctor
jasonmalinowski
approved these changes
Jan 9, 2026
Contributor
|
Won't the new code allocate a delegate too so as to capture |
Contributor
|
I don't really see why we need a lazy at all. We already store the string. Just have another Nullable string that is lazily computed if null when accessed. |
Member
|
@CyrusNajmabadi We'll allocate the delegate regardless, but we won't need a separate closure to capture the local. |
Contributor
|
I'd rather us move to no-alloc. |
Member
|
@CyrusNajmabadi Go right ahead! I'll sign off on that. |
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.
This pull request was generated by the VS Perf Rel AI Agent. Please review this AI-generated PR with extra care! For more information, visit our wiki. Please share feedback with TIP Insights
DocumentUri(string uriString)constructor allocates a closure/display class (<>c__DisplayClass1_0) on every invocation because the lambda() => ParseUri(uriString)captures theuriStringparameter instead of using already-assigned instance property, causing unnecessary display class allocations.Evidence:
TypeAllocated!<>c__DisplayClass1_0(compiler-generated closure)_parsedUriLazy = new(() => ParseUri(uriString));which captures theuriStringparameterUriStringproperty is assigned before theLazy<>creation, so we can usethis.UriStringinstead to avoid parameter captureIssue type: AVOID creating lambdas that capture variables on hot path
Proposed fix: Change the lambda in
DocumentUri(string uriString)constructor to reference the instance propertyUriStringinstead of the captured parameteruriString. UriString is a get-only auto-property set in the constructor and never mutated afterward. So this is a minimal and safe fix that eliminates the display class allocation while maintaining identical behavior.Best practices wiki
See related failure in PRISM
ADO work item