Skip to content

Commit 84b1ddd

Browse files
[release/7.0] Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80513)
* Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80437) * Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo * Address PR feedback * Ensure CachingContext.Options isn't being trimmed away. * Update servicing version Co-authored-by: Stephen Toub <[email protected]>
1 parent cdc7e17 commit 84b1ddd

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/libraries/System.Text.Json/src/System.Text.Json.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
<NoWarn>CS8969</NoWarn>
99
<IncludeInternalObsoleteAttribute>true</IncludeInternalObsoleteAttribute>
1010
<IsPackable>true</IsPackable>
11-
<ServicingVersion>1</ServicingVersion>
11+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
12+
<ServicingVersion>2</ServicingVersion>
1213
<!-- This library has been annotated to be AOT safe -->
1314
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
1415
<PackageDescription>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.

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,18 +141,21 @@ internal void ClearCaches()
141141
internal sealed class CachingContext
142142
{
143143
private readonly ConcurrentDictionary<Type, JsonTypeInfo?> _jsonTypeInfoCache = new();
144+
private readonly Func<Type, JsonTypeInfo?> _jsonTypeInfoFactory;
144145

145146
public CachingContext(JsonSerializerOptions options)
146147
{
147148
Options = options;
149+
_jsonTypeInfoFactory = Options.GetTypeInfoNoCaching;
148150
}
149151

150152
public JsonSerializerOptions Options { get; }
151153
// Property only accessed by reflection in testing -- do not remove.
152154
// If changing please ensure that src/ILLink.Descriptors.LibraryBuild.xml is up-to-date.
153155
public int Count => _jsonTypeInfoCache.Count;
154156

155-
public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, Options.GetTypeInfoNoCaching);
157+
public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory);
158+
156159
public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo);
157160

158161
public void Clear()

0 commit comments

Comments
 (0)