Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
BUILD-PERF-M2 (part 3): browser rebuild floor — catalog facts, scoped ports, regex cache
Claudee2c3895f05e2
diff --git a/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.cs b/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.csindex 99c8c7a5..32447d52 100644--- a/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.cs+++ b/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.cs@@ -149,3 +149,42 @@ internal static class AssemblyDeclarationScope}).ToArray();}}++/// <summary>+/// BUILD-PERF-M2: facts about one declaration catalog that every definition of a build needs+/// (InlineStep presence, Gear-blanked text, content hash). They were recomputed per definition,+/// which in the interpreted browser runtime cost hundreds of milliseconds per rebuild.+/// </summary>+internal sealed class DeclarationTextFacts+{+ private static readonly object Gate = new();+ private static DeclarationTextFacts? last;++ private readonly Lazy<bool> hasInlineStep;+ private readonly Lazy<string> ordinary;+ private readonly Lazy<string> hash;++ private DeclarationTextFacts(string text)+ {+ Text = text;+ hasInlineStep = new(() => System.Text.RegularExpressions.Regex.IsMatch(text, @"\bInlineStep\b", System.Text.RegularExpressions.RegexOptions.CultureInvariant));+ ordinary = new(() => AssemblyDefinitionMaterializer.OrdinaryDeclarations(text));+ hash = new(() => Convert.ToHexString(System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(text))));+ }++ public string Text { get; }+ public bool HasInlineStep => hasInlineStep.Value;+ /// <summary>The catalog with Gear roots blanked, as ordinary definitions compile it.</summary>+ public string Ordinary => ordinary.Value;+ /// <summary>SHA-256 (hex) of the UTF-8 text.</summary>+ public string Hash => hash.Value;++ public static DeclarationTextFacts Of(string text)+ {+ lock (Gate)+ {+ if (last is not null && (ReferenceEquals(last.Text, text) || string.Equals(last.Text, text, StringComparison.Ordinal))) return last;+ return last = new DeclarationTextFacts(text);+ }+ }+}diff --git a/Aetheris.Kernel.Firmament/Assembly/AssemblyM0Parser.cs b/Aetheris.Kernel.Firmament/Assembly/AssemblyM0Parser.csindex 6f7f419f..4b6efc11 100644--- a/Aetheris.Kernel.Firmament/Assembly/AssemblyM0Parser.cs+++ b/Aetheris.Kernel.Firmament/Assembly/AssemblyM0Parser.cs@@ -104,11 +104,17 @@ public sealed class AssemblyM0Parserinput = FirmamentSourceSpelling.Normalize(input);var watch = Stopwatch.StartNew();var diagnostics = new List<AssemblyDiagnostic>();+ var blankcommentsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.blank-comments");var source = Regex.Replace(input, "\"(?:\\\\.|[^\"\\\\])*\"|//[^\\r\\n]*", m => m.Value.StartsWith('"') ? m.Value : new string(' ',m.Length));+ blankcommentsPhase.Dispose();+ var pmiPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.pmi");var annotations = AssemblyPmiAuthoring.Parse(ref source, sourceIdentity, diagnostics);+ pmiPhase.Dispose();if (diagnostics.Count > 0) return new(null, diagnostics, watch.Elapsed.TotalMilliseconds);var patternDiagnostics = new List<string>();+ var patternsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.patterns");var expandedPatterns = CanonicalStaticAuthoring.ExpandAssemblyPatterns(source,patternDiagnostics);+ patternsPhase.Dispose();foreach (var error in patternDiagnostics) diagnostics.Add(new("assembly-pattern-invalid",error));if (expandedPatterns is null || patternDiagnostics.Count>0) return new(null,diagnostics,watch.Elapsed.TotalMilliseconds);source = expandedPatterns.Source;@@ -116,16 +122,24 @@ public sealed class AssemblyM0Parser// lowers through the existing, locally solved definition authority.source = Regex.Replace(source, @"\bSubassembly\s+(?<name>[A-Za-z_]\w*)\s*\{","Template < __Unit: __Unit > Assembly ${name} {", RegexOptions.CultureInvariant);+ var appearancePhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.appearance");var appearanceCatalog = AssemblyAppearanceAuthoring.Parse(ref source, diagnostics, out var appearanceSpans);+ appearancePhase.Dispose();var gearDocument = GearAuthoring.HasGearDefinitions(source)? GearAuthoring.ParseDefinitions(source): new GearAuthoringDocument("GearModel", [], [], []);var gearAuthorities = gearDocument.Gears.ToDictionary(gear => gear.Name, StringComparer.Ordinal);foreach (var diagnostic in gearDocument.Diagnostics.Where(item => item != GearAuthoring.Prefix + "declaration-missing"))diagnostics.Add(new("assembly-" + diagnostic.Split(':')[0], diagnostic));+ var conceptsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.concepts");var concepts = ParseAssemblyConcepts(source, sourceIdentity, diagnostics);+ conceptsPhase.Dispose();+ var interfacesPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.interfaces");var interfaces = ParseInterfaces(source, sourceIdentity, diagnostics);+ interfacesPhase.Dispose();+ var layoutsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.layouts");var layout = AssemblyDatumAuthoring.ParseLayouts(source, sourceIdentity, diagnostics, out var partDeclarations);+ layoutsPhase.Dispose();foreach (var contract in interfaces.Where(i => i.DatumContract is not null))if (!layout.Any(d => d.Identity == contract.DatumContract!.Datum && d.Plane == d.Identity))diagnostics.Add(new("assembly-datum-plane-unresolved", $"Interface '{contract.Name}' requires a declared Concept plane or Axis '{contract.DatumContract!.Datum}'."));@@ -143,11 +157,13 @@ public sealed class AssemblyM0ParserIReadOnlyList<SemanticValue> Ports(string identity){if (!portCache.TryGetValue(identity, out var ports))- portCache[identity] = ports = AssemblyPublishedPorts.BindAuthored(identity, definitionSource, sourceIdentity, diagnostics, project, portDependencies, PortCache);+ using (var portsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.ports")) portCache[identity] = ports = AssemblyPublishedPorts.BindAuthored(identity, definitionSource, sourceIdentity, diagnostics, project, portDependencies, PortCache);return ports;}var templateRanges = new List<(int Start, int Length)>();+ var assemblydefinitionsPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.assembly-definitions");var assemblyDefinitions = ParseAssemblyDefinitions(source, sourceIdentity, interfaces, concepts, gearAuthorities, diagnostics, templateRanges, Ports, layout);+ assemblydefinitionsPhase.Dispose();// Template-produced Assembly bodies are declarations, not exported roots.// Blank them without changing offsets, then locate the single root Assembly.var rootSearch = source.ToCharArray();@@ -159,7 +175,9 @@ public sealed class AssemblyM0Parservar body = BalancedBody(source, assemblyHeader.Index + assemblyHeader.Length - 1, diagnostics, "Assembly");if (body is null) return Done(null);var specializationCache = new Dictionary<string, AssemblyMemberSource>(StringComparer.Ordinal);+ var treePhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.tree");var tree = ParseTree(body, sourceIdentity, diagnostics, assemblyDefinitions, interfaces, specializationCache, gearAuthorities, Ports);+ treePhase.Dispose();if (tree is null) return Done(null);var mates = ParseMates(body, sourceIdentity, diagnostics);var relations = ParseRelations(body, diagnostics);@@ -167,7 +185,9 @@ public sealed class AssemblyM0Parservar anchorMatch = Regex.Match(RemoveBlocks(body, "Mate", @"Assert\s+ToleranceStackup"), @"\bAnchor\s*:\s*(?<path>[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)\s*;", RegexOptions.CultureInvariant);var anchor = anchorMatch.Success ? AssemblyPath.Parse(anchorMatch.Groups["path"].Value) : new AssemblyPath([tree.Name]);var frames = ParseFrameTransforms(body, sourceIdentity, diagnostics);+ var datumbindPhase = Aetheris.Kernel.Core.Diagnostics.BuildPerfTrace.Phase("m0.datum-bind");tree = AssemblyDatumAuthoring.Bind(body, tree, layout, interfaces, diagnostics, out var datumFrames, Ports);+ datumbindPhase.Dispose();frames = frames.Concat(datumFrames).ToArray();var result = new AssemblySource(assemblyHeader.Groups["name"].Value, tree, interfaces, mates, anchor, relations, asserts, sourceIdentity, definitionSource, assemblyDefinitions, SourceDependencies: portDependencies, FrameTransforms: frames, Patterns: expandedPatterns.Document?.Patterns, ConceptPoints: expandedPatterns.Document?.ConceptPoints, AppearanceCatalog: appearanceCatalog, Annotations: annotations);return Done(result);diff --git a/Aetheris.Kernel.Firmament/Assembly/AssemblyPublishedPorts.cs b/Aetheris.Kernel.Firmament/Assembly/AssemblyPublishedPorts.csindex 6d3aecc0..50f1e0e7 100644--- a/Aetheris.Kernel.Firmament/Assembly/AssemblyPublishedPorts.cs+++ b/Aetheris.Kernel.Firmament/Assembly/AssemblyPublishedPorts.cs@@ -63,8 +63,31 @@ internal static class AssemblyPublishedPorts}if (!identity.Contains('<')) return [];// A specialization's ports are a function of the identity, the declarations and the source name.- return Memo(cache, "template\0" + identity + "\0" + sourceIdentity + "\0" + Hash(declarations), diagnostics,- diagnostics => BindTemplate(identity, declarations, sourceIdentity, diagnostics));+ var wholeKey = "template\0" + identity + "\0" + sourceIdentity + "\0" + DeclarationTextFacts.Of(declarations).Hash;+ // BUILD-PERF-M2: like its definition, a specialization binds its ports from its declaration+ // closure when that expands on its own, otherwise from the whole catalog. Keys follow the+ // text actually bound (closure, or whole catalog after a failed closure), so a reused binding+ // is always the one a fresh build would make.+ if (AssemblyDefinitionMaterializer.ScopeDeclarations+ && AssemblyDeclarationScope.Closure(declarations, AssemblyDefinitionMaterializer.TemplateName(identity).Trim()) is { } scoped+ && !ReferenceEquals(scoped, declarations))+ {+ var fallbackKey = "template-scope-fallback\0" + wholeKey;+ if (cache is null || !cache.Contains(fallbackKey))+ {+ var scopedKey = "template-scoped\0" + identity + "\0" + sourceIdentity + "\0" + Hash(scoped);+ var attempt = new List<AssemblyDiagnostic>();+ var ports = Memo(cache, scopedKey, attempt, diagnostics => BindTemplate(identity, scoped, sourceIdentity, diagnostics));+ if (attempt.All(d => d.Severity != AssemblyDiagnosticSeverity.Error))+ {+ diagnostics.AddRange(attempt);+ return ports;+ }+ cache?.Forget(scopedKey);+ }+ return Memo(cache, fallbackKey, diagnostics, diagnostics => BindTemplate(identity, declarations, sourceIdentity, diagnostics));+ }+ return Memo(cache, wholeKey, diagnostics, diagnostics => BindTemplate(identity, declarations, sourceIdentity, diagnostics));}private static IReadOnlyList<SemanticValue> BindTemplate(string identity, string declarations, string sourceIdentity,diff --git a/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs b/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.csindex 8d914106..f6f47035 100644--- a/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs+++ b/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs@@ -82,6 +82,7 @@ internal sealed class DerivedValueCache<T>internal void BeginBuild() { previous = current; current = new(StringComparer.Ordinal); }internal void Clear() { current.Clear(); previous.Clear(); }internal void Forget(string key) { current.Remove(key); previous.Remove(key); }+ internal bool Contains(string key) => current.ContainsKey(key) || previous.ContainsKey(key);internal T GetOrAdd(string key, Func<T> create){if (current.TryGetValue(key, out var value)) return value;@@ -122,19 +123,20 @@ internal sealed class AssemblyDefinitionCache(int capacity)// BUILD-PERF-M2: a template definition compiles from its declaration closure when that closure// compiles alone, otherwise from the whole catalog (see AssemblyDefinitionMaterializer). The// build is deterministic, so an entry stored under the closure key (closure compiled) or under- // the whole-catalog key (closure failed for exactly that catalog) is what a fresh build of the- // current declarations would produce.- string? key, scopedKey;+ // the fallback key (closure failed for exactly this catalog) is what a fresh build of the+ // current declarations would produce. Without a closure the whole-catalog key applies.+ string? key, scopedKey, fallbackKey;using (BuildPerfTrace.Phase("assembly.cache-key")){key = Key(identity, declarations, sourceIdentity, project);scopedKey = key is null ? null : ScopedKey(identity, declarations, sourceIdentity);+ fallbackKey = scopedKey is null ? key : Hash(sourceIdentity, identity, "scope-fallback\0" + key);}var reason = key is null ? "unsupported-resource-dependency" : "first-build-or-evicted";if (key is not null && entries.TryGetValue(identity, out var entry)){reason = "definition-inputs-changed";- if (entry.Key == key || (scopedKey is not null && entry.Key == scopedKey))+ if (entry.Key == fallbackKey || (scopedKey is not null && entry.Key == scopedKey)){BrepBody body;using (BuildPerfTrace.Phase("assembly.cache-hit-clone")) body = entry.Metadata.Body.Clone();@@ -166,6 +168,7 @@ internal sealed class AssemblyDefinitionCache(int capacity)Evidence[^1] = new(identity, false, "resource-changed-during-build");return result;}+ else key = fallbackKey;// This bounded cache only admits immutable scalar/datum semantic bindings.// Domain materializers and geometry-valued ports stay on the uncached path.if (!result.Semantics.All(CanCopy) || result.CanonicalStep is null)@@ -207,9 +210,10 @@ internal sealed class AssemblyDefinitionCache(int capacity)private static string? ScopedKey(string identity, string? declarations, string sourceIdentity){if (declarations is null || !AssemblyDefinitionMaterializer.ScopeDeclarations || !identity.Contains('<', StringComparison.Ordinal)- || Regex.IsMatch(identity, "^(?:SectionChainFile|LoftFile|ExternalStep)<", RegexOptions.CultureInvariant)- || Regex.IsMatch(declarations, @"\bInlineStep\b", RegexOptions.CultureInvariant)) return null;- var ordinary = AssemblyDefinitionMaterializer.OrdinaryDeclarations(declarations);+ || Regex.IsMatch(identity, "^(?:SectionChainFile|LoftFile|ExternalStep)<", RegexOptions.CultureInvariant)) return null;+ var facts = DeclarationTextFacts.Of(declarations);+ if (facts.HasInlineStep) return null;+ var ordinary = facts.Ordinary;return AssemblyDeclarationScope.Closure(ordinary, AssemblyDefinitionMaterializer.TemplateName(identity)) is { } scoped && !ReferenceEquals(scoped, ordinary)? Hash(sourceIdentity, identity, "scoped\0" + scoped) : null;}@@ -249,10 +253,13 @@ internal sealed class AssemblyDefinitionCache(int capacity)}else{- if (declarations is null || Regex.IsMatch(declarations, @"\bInlineStep\b", RegexOptions.CultureInvariant)) return null;+ if (declarations is null) return null;+ var facts = DeclarationTextFacts.Of(declarations);+ if (facts.HasInlineStep) return null;// Keep exact text, including offsets, because cached semantic evidence- // carries source spans. The parser already excludes assembly bodies.- inputs = declarations;+ // carries source spans. The parser already excludes assembly bodies. The+ // catalog is shared by every definition of a build: hash it once.+ return Hash(sourceIdentity, identity, "declarations-sha256\0" + facts.Hash);}return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes("assembly-definition-cache-v1\0" + sourceIdentity + "\0" + identity + "\0" + inputs)));diff --git a/Aetheris.Web.Runtime/Program.cs b/Aetheris.Web.Runtime/Program.csindex 9d4c63e5..090bc279 100644--- a/Aetheris.Web.Runtime/Program.cs+++ b/Aetheris.Web.Runtime/Program.cs@@ -18,6 +18,11 @@ namespace Aetheris.Web.Runtime;public static partial class Program{+ // BUILD-PERF-M2: the compiler calls static Regex methods from ~860 sites. The runtime caches the+ // constructed patterns, 15 by default, so a build re-parsed most patterns on every call; in the+ // interpreted browser runtime that is a large share of every rebuild. Matching is unchanged.+ static Program() => System.Text.RegularExpressions.Regex.CacheSize = Math.Max(System.Text.RegularExpressions.Regex.CacheSize, 1024);+private const string ContractVersion = "aetheris/web-editor-contract/1";private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);private static readonly JsonSerializerOptions BridgeOptions = WebResponseBridge.Options(JsonOptions);