Skip to content

Commit 4881062

Browse files
authored
Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80437)
* Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo * Address PR feedback
1 parent 368a064 commit 4881062

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,14 @@ internal void ClearCaches()
156156
internal sealed class CachingContext
157157
{
158158
private readonly ConcurrentDictionary<Type, JsonTypeInfo?> _jsonTypeInfoCache = new();
159+
private readonly Func<Type, JsonTypeInfo?> _jsonTypeInfoFactory;
159160

160161
public CachingContext(JsonSerializerOptions options, int hashCode)
161162
{
162163
Options = options;
163164
HashCode = hashCode;
165+
166+
_jsonTypeInfoFactory = options.GetTypeInfoNoCaching;
164167
}
165168

166169
public JsonSerializerOptions Options { get; }
@@ -169,7 +172,8 @@ public CachingContext(JsonSerializerOptions options, int hashCode)
169172
// If changing please ensure that src/ILLink.Descriptors.LibraryBuild.xml is up-to-date.
170173
public int Count => _jsonTypeInfoCache.Count;
171174

172-
public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, Options.GetTypeInfoNoCaching);
175+
public JsonTypeInfo? GetOrAddJsonTypeInfo(Type type) => _jsonTypeInfoCache.GetOrAdd(type, _jsonTypeInfoFactory);
176+
173177
public bool TryGetJsonTypeInfo(Type type, [NotNullWhen(true)] out JsonTypeInfo? typeInfo) => _jsonTypeInfoCache.TryGetValue(type, out typeInfo);
174178

175179
public void Clear()

0 commit comments

Comments
 (0)