iemejia opened a new pull request, #3966:
URL: https://github.com/apache/avro/pull/3966

   ## What changes were proposed in this pull request?
   
   `ObjectCreator.FindType` passed an inline lambda to 
`ConcurrentDictionary.GetOrAdd`:
   
   ```csharp
   return typeCacheByName.GetOrAdd(name, (_) =>
   {
       ...
       if (TryGetIListItemTypeName(name, out var itemTypeName)) { ... }   // 
captures `name`
       ...
   });
   ```
   
   The lambda discards the key parameter (`(_)`) and instead **captures the 
local `name`**. 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 whether 
`GetOrAdd` invokes it. As reported in AVRO-3893, this accounted for ~10% of 
allocations on a deserialization hot path using `PreresolvingDatumReader`.
   
   ## How was this patch fixed?
   
   - Extract the value factory into a private `FindTypeUncached(string name)` 
method.
   - Store a single cached `Func<string, Type> findTypeFactory` delegate, 
created once in the constructor, and pass it to `GetOrAdd`.
   - The factory uses its `name` parameter (the cache key supplied by 
`GetOrAdd`) instead of a captured local, so **no closure is allocated per 
lookup**.
   
   `ObjectCreator` is a shared singleton (`ObjectCreator.Instance`), so the 
factory delegate is effectively allocated once for the process. Behaviour is 
unchanged; the `CA1031` suppression is retargeted from `FindType` to the 
extracted `FindTypeUncached`.
   
   ## How was this patch tested?
   
   - `dotnet build` of `Avro.main` succeeds with 0 warnings (confirming the 
retargeted suppression).
   - `Avro.test` Specific/ObjectCreator suites pass: **96/96** across net6.0, 
net7.0, and net8.0.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to