From f701b9f4fdaad0d782976c8cede2c27237962115 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Wed, 11 Jan 2023 20:31:39 +0000 Subject: [PATCH 1/2] Remove per-context delegate allocations in netcoreapp targets --- .../Serialization/JsonSerializerOptions.Caching.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs index f4e02f566e5b42..d258dbcd6e4705 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs @@ -156,14 +156,17 @@ internal void ClearCaches() internal sealed class CachingContext { private readonly ConcurrentDictionary _jsonTypeInfoCache = new(); +#if !NETCOREAPP private readonly Func _jsonTypeInfoFactory; +#endif public CachingContext(JsonSerializerOptions options, int hashCode) { Options = options; HashCode = hashCode; - +#if !NETCOREAPP _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; +#endif } public JsonSerializerOptions Options { get; } @@ -172,7 +175,12 @@ public CachingContext(JsonSerializerOptions options, int hashCode) // If changing please ensure that src/ILLink.Descriptors.LibraryBuild.xml is up-to-date. public int Count => _jsonTypeInfoCache.Count; - public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => +#if NETCOREAPP + _jsonTypeInfoCache.GetOrAdd(type, static (type, options) => options.GetTypeInfoNoCaching(type), Options); +#else + _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); +#endif public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo); From 2d56aeeaf4cc82b4afc8393a116c93869137f2c5 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 12 Jan 2023 14:22:09 +0000 Subject: [PATCH 2/2] Ensure Options property doesn't get trimmed. --- .../Text/Json/Serialization/JsonSerializerOptions.Caching.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs index d258dbcd6e4705..550261363f6d1d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs @@ -165,7 +165,7 @@ public CachingContext(JsonSerializerOptions options, int hashCode) Options = options; HashCode = hashCode; #if !NETCOREAPP - _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; + _jsonTypeInfoFactory = Options.GetTypeInfoNoCaching; #endif }