Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo (#80437)
* Fix delegate allocation in CachingContext.GetOrAddJsonTypeInfo

* Address PR feedback
  • Loading branch information
stephentoub authored and eiriktsarpalis committed Jan 11, 2023
commit 6884ceea9edded67c786c1532ef64f60026f42d8
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,22 @@ internal void ClearCaches()
internal sealed class CachingContext
{
private readonly ConcurrentDictionary<Type, JsonTypeInfo?> _jsonTypeInfoCache = new();
private readonly Func<Type, JsonTypeInfo?> _jsonTypeInfoFactory;

public CachingContext(JsonSerializerOptions options)
{
Options = options;

_jsonTypeInfoFactory = options.GetTypeInfoNoCaching;
}

public JsonSerializerOptions Options { get; }
// Property only accessed by reflection in testing -- do not remove.
// 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()
Expand Down