Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Revert "Use bounded ordinal string caches for syntax documents"
Yuechen Li80985d38a00f
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs b/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.csindex 5928d219..c82823c4 100644--- a/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs+++ b/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs@@ -9,31 +9,35 @@ public static class FirmamentSyntaxParser{ArgumentNullException.ThrowIfNull(source);ArgumentNullException.ThrowIfNull(sourceFile);+ var key = (sourceFile, source);lock (ParseCache)- if (ParseCache.TryGetValue(sourceFile, out var documents) && documents.TryGetValue(source, out var cached)) return cached;+ if (ParseCache.TryGetValue(key, out var cached)) return cached;var result = new Parser(source, sourceFile).Parse();lock (ParseCache){// The trees are immutable, so identical text can share one parse. Compilation// helpers re-parse the same document many times (13,600 parses in one guitar// build); clearing on overflow keeps memory bounded without LRU bookkeeping.- if (parseCacheCount >= ParseCacheCapacity)- {- ParseCache.Clear();- parseCacheCount = 0;- }- if (!ParseCache.TryGetValue(sourceFile, out var documents))- ParseCache[sourceFile] = documents = new(StringComparer.Ordinal);- if (documents.TryAdd(source, result)) parseCacheCount++;+ if (ParseCache.Count >= ParseCacheCapacity) ParseCache.Clear();+ ParseCache[key] = result;}return result;}private const int ParseCacheCapacity = 256;- // Keep document and text identities ordinal without a value-type key: Mono- // WASM AOT can abort in shared value-type dispatch while hashing a tuple key.- private static readonly Dictionary<string, Dictionary<string, SyntaxParseResult>> ParseCache = new(StringComparer.Ordinal);- private static int parseCacheCount;+ // Cache identity is ordinal for both strings. An explicit comparer also avoids+ // ValueTuple<TRef,TRef>.GetHashCode's shared virtual-dispatch path, which can+ // abort the Mono WASM AOT runtime while tracing a valid IntelliDraft outline.+ private static readonly Dictionary<(string File, string Source), SyntaxParseResult> ParseCache = new(new ParseCacheKeyComparer());++ private sealed class ParseCacheKeyComparer : IEqualityComparer<(string File, string Source)>+ {+ public bool Equals((string File, string Source) x, (string File, string Source) y)+ => StringComparer.Ordinal.Equals(x.File, y.File) && StringComparer.Ordinal.Equals(x.Source, y.Source);++ public int GetHashCode((string File, string Source) key)+ => HashCode.Combine(StringComparer.Ordinal.GetHashCode(key.File), StringComparer.Ordinal.GetHashCode(key.Source));+ }private sealed class ParseFailure : Exception;private sealed class Parser