-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Massive allocations of Func<Type, JsonTypeInfo> in System.Text.Json.JsonSerializerOptions+CachingContext.GetOrAddJsonTypeInfo(Type) #80430
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsCreate a plain F# console app targeting .NET 7, with this minimal repro code: open System.Text.Json
let data = Array.init 1_000_000 (fun _ -> [box 1])
let json = JsonSerializer.Serialize(data) This initializes an array with one million elements, each being an F# list with a boxed integer. It then serializes the data. When I run a memory profiler (dotMemory), the top hitter in terms of allocation size is
I assume the reason is that there is a lambda or similar somewhere that is not being optimized/cached by the C# compiler. You may want to adjust your code to prevent these allocations. Maybe related: dotnet/roslyn#5835 and linked issues
|
🙀 I was today years old when I realized these delegates aren't being cached. |
Even though this will mostly impact collections with polymorphic elements, we should probably service this in .NET 7. |
We're of the same age, then 😅 (Based on my very recent experience, seems F# doesn't cache these, either. @dsyme, is that something to consider, or is there no point in raising an F# issue about that?) |
Please create (or search for) a suggestion at https://github.com/fsharp/fslang-suggestions/issues, we can follow-up there. |
Create a plain F# console app targeting .NET 7, with this minimal repro code:
This initializes an array with one million elements, each being an F# list with a boxed integer. It then serializes the data.
When I run a memory profiler (dotMemory), the top hitter in terms of allocation size is
Func<Type, JsonTypeInfo>
in System.Text.Json, specifically inGetOrAddJsonTypeInfo
. It is allocated almost 900.000 times:I assume the reason is that there is a lambda or similar somewhere that is not being optimized/cached by the C# compiler.
You may want to adjust your code to prevent these allocations.
Maybe related: dotnet/roslyn#5835 and linked issues
The text was updated successfully, but these errors were encountered: