diff --git a/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs b/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs index 4e05eceb..ad99217e 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/DynamicCache{TKey,TValue}.cs @@ -21,6 +21,11 @@ public sealed class DynamicCache /// private readonly ConcurrentDictionary map = new(); + /// + /// The tracking dead entries to remove. + /// + private readonly ConditionalWeakTable table = new(); + /// /// Gets or creates a new value for a given key, using a supplied callback if needed. /// @@ -97,14 +102,12 @@ public sealed class DynamicCache return false; } - // As part of the fallback step, traverse all items and remove dead keys - foreach (Entry candidateKey in this.map.Keys) - { - if (!candidateKey.IsAlive) - { - _ = this.map.TryRemove(candidateKey, out _); - } - } + // We need to setup the removal of this key-value pair when the key is no longer + // referenced. To do this, we add a new EntryRemover instance to the table. When + // the key has no active references and is collected (which means the entry will + // also become invalid), the finalizer of the EntryRemover instance will run on + // a following GC, and remove that dead Entry instance from the map automatically. + this.table.Add(key, new EntryRemover(this.map, entry)); return true; } @@ -126,6 +129,11 @@ public sealed class DynamicCache /// private readonly WeakReference reference; + /// + /// The hashcode of the target key (so it's available even after the key is gone). + /// + private readonly int hashCode; + /// /// The last key matched from , if available. /// @@ -143,6 +151,7 @@ public sealed class DynamicCache public Entry(TKey key) { this.reference = new WeakReference(key); + this.hashCode = EqualityComparer.Default.GetHashCode(key); } /// @@ -185,6 +194,13 @@ public sealed class DynamicCache return false; } + // Special case matching on the entry identity directly. This is used + // by the finalizer of EntryRemover to find the entry to remove. + if (this == entry) + { + return true; + } + _ = this.reference.TryGetTarget(out TKey? left); _ = entry.reference.TryGetTarget(out TKey? right); @@ -208,12 +224,40 @@ public sealed class DynamicCache /// public override int GetHashCode() { - if (this.reference.TryGetTarget(out TKey? value)) - { - return EqualityComparer.Default.GetHashCode(value); - } + return this.hashCode; + } + } - return 0; + /// + /// An object reponsible for removing dead instance from the table. + /// + private sealed class EntryRemover + { + /// + private readonly ConcurrentDictionary map; + + /// + /// The target instance to remove from . + /// + private readonly Entry entry; + + /// + /// Creates a new instance with the specified parameters. + /// + /// + /// + public EntryRemover(ConcurrentDictionary map, Entry entry) + { + this.map = map; + this.entry = entry; + } + + /// + /// Removes from when the current instance is finalized. + /// + ~EntryRemover() + { + _ = this.map.TryRemove(this.entry, out _); } } }