AVRO-3893: [csharp] Avoid per-call closure allocation in ObjectCreator.FindType - #3966
Open
iemejia wants to merge 1 commit into
Open
AVRO-3893: [csharp] Avoid per-call closure allocation in ObjectCreator.FindType#3966iemejia wants to merge 1 commit into
iemejia wants to merge 1 commit into
Conversation
…r.FindType ObjectCreator.FindType passed an inline lambda to ConcurrentDictionary.GetOrAdd that discarded the key parameter ((_)) and captured the local `name` instead. Capturing a local forces the compiler to allocate a new display-class closure and delegate on every call, even on cache hits, which showed up as a significant share of allocations on the deserialization hot path when using a PreresolvingDatumReader. Extract the value factory into a FindTypeUncached(string) method and store a single cached Func<string, Type> delegate (findTypeFactory), created once in the constructor. The factory now uses its `name` parameter (the cache key supplied by GetOrAdd) instead of a captured local, so no closure is allocated per lookup. Behaviour is unchanged. The CA1031 suppression is retargeted to the extracted method.
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.
What changes were proposed in this pull request?
ObjectCreator.FindTypepassed an inline lambda toConcurrentDictionary.GetOrAdd:The lambda discards the key parameter (
(_)) and instead captures the localname. Capturing a local forces the C# compiler to allocate a new display-class closure (and delegate) on every call — including cache hits, since the factory delegate is constructed as an argument regardless of whetherGetOrAddinvokes it. As reported in AVRO-3893, this accounted for ~10% of allocations on a deserialization hot path usingPreresolvingDatumReader.How was this patch fixed?
FindTypeUncached(string name)method.Func<string, Type> findTypeFactorydelegate, created once in the constructor, and pass it toGetOrAdd.nameparameter (the cache key supplied byGetOrAdd) instead of a captured local, so no closure is allocated per lookup.ObjectCreatoris a shared singleton (ObjectCreator.Instance), so the factory delegate is effectively allocated once for the process. Behaviour is unchanged; theCA1031suppression is retargeted fromFindTypeto the extractedFindTypeUncached.How was this patch tested?
dotnet buildofAvro.mainsucceeds with 0 warnings (confirming the retargeted suppression).Avro.testSpecific/ObjectCreator suites pass: 96/96 across net6.0, net7.0, and net8.0.