Avoid closures in CachedTypeInspector#954
Merged
EdwardCooke merged 2 commits intoAug 31, 2024
Merged
Conversation
Contributor
Author
|
Note that this PR uses the same benchmark introduced in #953. So if / when one of these PRs merge, the other should be rebased on top of latest main to remove the duplicate commit. I'm happy to do this housekeeping. I just mention it for clarity, and so that each PR can be reviewed independently, rather than forcing them to all be chained together. |
1084371 to
3732b8d
Compare
This was referenced Aug 16, 2024
3732b8d to
9b03412
Compare
This was referenced Aug 5, 2025
This was referenced Sep 17, 2025
This was referenced Dec 29, 2025
This was referenced Apr 23, 2026
This was referenced Apr 30, 2026
This was referenced May 12, 2026
This was referenced May 21, 2026
This was referenced Jun 24, 2026
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.
The
CachedTypeInspectorcreates a closure in order to capture the method parameters. This results in 2 allocations per object serialized. I switched to using theConcurrentDictionary.GetOrAdd()method that takes a third context parameter in order to avoid allocations.I also added an extension method as described in dotnet/runtime#13978 for runtimes where the overload doesn't exist. I also updated the language version from 8 to 9 in order to get access to static lambdas in order to ensure that the lambdas don't capture.
This reduces allocations by about ~4% in the benchmark:
Before
After