-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Labels
Milestone
Description
Create 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 Func<Type, JsonTypeInfo> in System.Text.Json, specifically in GetOrAddJsonTypeInfo. It is allocated almost 900.000 times:
Allocated type : System.Func<Type, JsonTypeInfo>
Objects : 896629
Bytes : 57384256
Allocated by
100% GetOrAddJsonTypeInfo • 54,73 MB / 54,73 MB • System.Text.Json.JsonSerializerOptions+CachingContext.GetOrAddJsonTypeInfo(Type)
100% GetTypeInfoInternal • 54,73 MB / - • System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Boolean)
► 95,0% ResolvePolymorphicConverter • 51,99 MB / - • System.Text.Json.Serialization.JsonConverter.ResolvePolymorphicConverter(Object, JsonTypeInfo, JsonSerializerOptions, WriteStack)
► 5,00% InitializePolymorphicReEntry • 2,74 MB / - • System.Text.Json.WriteStackFrame.InitializePolymorphicReEntry(Type, JsonSerializerOptions)
► <0,01% get_ElementTypeInfo • 128 B / - • System.Text.Json.Serialization.Metadata.JsonTypeInfo.get_ElementTypeInfo()
► <0,01% GetTypeInfoForRootType • 64 B / - • System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type)
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
stephentoub