From 4186b7d74c2804ace80c98ce5bc3201f34058954 Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sun, 15 Oct 2023 12:29:13 +0200 Subject: [PATCH] Update TypeProvider.cs --- .../Internal/Symbols/Metadata/TypeProvider.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs index f8b4093b0..317b1acbe 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/TypeProvider.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -21,7 +22,7 @@ internal sealed class TypeProvider : ISignatureTypeProvider, private IntrinsicSymbols IntrinsicSymbols => this.compilation.IntrinsicSymbols; private readonly Compilation compilation; - private readonly Dictionary cache = new(); + private readonly ConcurrentDictionary cache = new(); public TypeProvider(Compilation compilation) { @@ -94,12 +95,7 @@ public TypeSymbol GetGenericTypeParameter(Symbol genericContext, int index) public TypeSymbol GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) { var key = new CacheKey(reader, handle); - if (!this.cache.TryGetValue(key, out var type)) - { - type = this.BuildTypeFromDefinition(reader, handle, rawTypeKind); - this.cache.Add(key, type); - } - return type; + return this.cache.GetOrAdd(key, _ => this.BuildTypeFromDefinition(reader, handle, rawTypeKind)); } private TypeSymbol BuildTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind) @@ -137,12 +133,7 @@ private TypeSymbol BuildTypeFromDefinition(MetadataReader reader, TypeDefinition public TypeSymbol GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind) { var key = new CacheKey(reader, handle); - if (!this.cache.TryGetValue(key, out var type)) - { - type = this.BuildTypeFromReference(reader, handle, rawTypeKind); - this.cache.Add(key, type); - } - return type; + return this.cache.GetOrAdd(key, _ => this.BuildTypeFromReference(reader, handle, rawTypeKind)); } private TypeSymbol BuildTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind)