Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
Use bounded ordinal string caches for syntax documents
Yuechen Li8fa2e89567a5
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs b/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.csindex c82823c4..5928d219 100644--- a/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs+++ b/Aetheris.Kernel.Firmament/FirmamentV2/Syntax/FirmamentSyntaxParser.cs@@ -9,35 +9,31 @@ public static class FirmamentSyntaxParser{ArgumentNullException.ThrowIfNull(source);ArgumentNullException.ThrowIfNull(sourceFile);- var key = (sourceFile, source);lock (ParseCache)- if (ParseCache.TryGetValue(key, out var cached)) return cached;+ if (ParseCache.TryGetValue(sourceFile, out var documents) && documents.TryGetValue(source, 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 (ParseCache.Count >= ParseCacheCapacity) ParseCache.Clear();- ParseCache[key] = result;+ 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++;}return result;}private const int ParseCacheCapacity = 256;- // 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));- }+ // 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;private sealed class ParseFailure : Exception;private sealed class Parser