From e67eb7e171aaf673ee4405f6f0eb6b518d8c600c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 10 Jan 2023 10:48:54 -0500 Subject: [PATCH 1/2] Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo --- .../Serialization/JsonSerializerOptions.Caching.cs | 12 +++++++++++- 1 file changed, 11 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 b7461d1bacb9a0..762ca42a33ff6e 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 @@ -169,7 +169,17 @@ 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, Options.GetTypeInfoNoCaching); + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) + { +#if NETCOREAPP2_0_OR_GREATER + return _jsonTypeInfoCache.GetOrAdd(type, static (type, options) => options.GetTypeInfoNoCaching(type), Options); +#else + return _jsonTypeInfoCache.TryGetValue(type, out JsonTypeInfo? value) ? + value : + _jsonTypeInfoCache.GetOrAdd(type, Options.GetTypeInfoNoCaching(type)); +#endif + } + public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo); public void Clear() From a9b40265a4b6322f7ca6e361d01dd784e7cb6964 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 10 Jan 2023 12:17:07 -0500 Subject: [PATCH 2/2] Address PR feedback --- .../Serialization/JsonSerializerOptions.Caching.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 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 762ca42a33ff6e..f4e02f566e5b42 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,11 +156,14 @@ internal void ClearCaches() internal sealed class CachingContext { private readonly ConcurrentDictionary _jsonTypeInfoCache = new(); + private readonly Func _jsonTypeInfoFactory; public CachingContext(JsonSerializerOptions options, int hashCode) { Options = options; HashCode = hashCode; + + _jsonTypeInfoFactory = options.GetTypeInfoNoCaching; } public JsonSerializerOptions Options { get; } @@ -169,16 +172,7 @@ 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) - { -#if NETCOREAPP2_0_OR_GREATER - return _jsonTypeInfoCache.GetOrAdd(type, static (type, options) => options.GetTypeInfoNoCaching(type), Options); -#else - return _jsonTypeInfoCache.TryGetValue(type, out JsonTypeInfo? value) ? - value : - _jsonTypeInfoCache.GetOrAdd(type, Options.GetTypeInfoNoCaching(type)); -#endif - } + public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory); public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo);