From 6884ceea9edded67c786c1532ef64f60026f42d8 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 11 Jan 2023 08:17:37 -0500 Subject: [PATCH 1/3] Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80437) * Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo * Address PR feedback --- .../Json/Serialization/JsonSerializerOptions.Caching.cs | 6 +++++- 1 file changed, 5 insertions(+), 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 f0a7e71dbc965f..708619914468ed 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 @@ -141,10 +141,13 @@ internal void ClearCaches() internal sealed class CachingContext { private readonly ConcurrentDictionary _jsonTypeInfoCache = new(); + private readonly Func _jsonTypeInfoFactory; public CachingContext(JsonSerializerOptions options) { Options = options; + + _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; } public JsonSerializerOptions Options { get; } @@ -152,7 +155,8 @@ public CachingContext(JsonSerializerOptions options) // 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, Options.GetTypeInfoNoCaching); + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); + public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo); public void Clear() From 4c8bc33b927332307a2e583da415c6290de61fbb Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 12 Jan 2023 13:40:58 +0000 Subject: [PATCH 2/3] Ensure CachingContext.Options isn't being trimmed away. --- .../Text/Json/Serialization/JsonSerializerOptions.Caching.cs | 3 +-- 1 file changed, 1 insertion(+), 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 708619914468ed..7a10c4d12c79b4 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 @@ -146,8 +146,7 @@ internal sealed class CachingContext public CachingContext(JsonSerializerOptions options) { Options = options; - - _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; + _jsonTypeInfoFactory = Options.GetTypeInfoNoCaching; } public JsonSerializerOptions Options { get; } From 81a735863d920450b5716a314e55f61b98a78fcc Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 12 Jan 2023 13:42:04 +0000 Subject: [PATCH 3/3] Update servicing version --- src/libraries/System.Text.Json/src/System.Text.Json.csproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 6f35ce4718eb5b..4fe3ab530933a8 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -8,7 +8,8 @@ CS8969 true true - 1 + true + 2 true Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data.