Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
BUILD-PERF-M2 (part 2): definitions compile from their declaration closure
Claude0dc53e6174c2
diff --git a/Aetheris.Kernel.Firmament.Tests/Assembly/AssemblyDeclarationScopeTests.cs b/Aetheris.Kernel.Firmament.Tests/Assembly/AssemblyDeclarationScopeTests.csnew file mode 100644index 00000000..5079bf2d--- /dev/null+++ b/Aetheris.Kernel.Firmament.Tests/Assembly/AssemblyDeclarationScopeTests.cs@@ -0,0 +1,114 @@+using Aetheris.Kernel.Firmament.Assembly;++namespace Aetheris.Kernel.Firmament.Tests.Assembly;++/// <summary>BUILD-PERF-M2: definitions compile from the declarations they depend on.</summary>+public sealed class AssemblyDeclarationScopeTests+{+ private const string Catalog = """+ Record Spec { Width: Length }+ Static Standard: Spec { Width: 4mm }+ Function twice(value: Length) -> Length =+ value * 2+ Template<H: Length> Struct Pad {+ Rect2 Outline { Center: [0mm,0mm]; Size: [twice(Standard.Width),10mm] }+ Profile P { Loop Outer { Outline |> TraceLoop } }+ Extrude Body { Profile: P From: 0mm To: H }+ }+ Template<R: Length> Struct Peg {+ Circle2 Outline { center: [0mm,0mm] radius: R }+ Profile P { Loop Outer { Outline |> TraceLoop } }+ Extrude Body { profile: P from: 0mm to: 5mm }+ }+ Concept Struct Layout {+ Axis Spindle { origin: [0mm, 0mm, 0mm]; direction: [0, 0, 1]; reference: [1, 0, 0]; }+ }+ """;++ [Fact]+ public void ClosureKeepsTransitiveDependenciesAndDropsUnrelatedDeclarations()+ {+ var pad = AssemblyDeclarationScope.Closure(Catalog, "Pad")!;+ Assert.Contains("Record Spec", pad, StringComparison.Ordinal);+ Assert.Contains("Static Standard", pad, StringComparison.Ordinal);+ // A multi-line expression stays with its declaration.+ Assert.Contains("value * 2", pad, StringComparison.Ordinal);+ Assert.DoesNotContain("Struct Peg", pad, StringComparison.Ordinal);+ Assert.DoesNotContain("Concept Struct Layout", pad, StringComparison.Ordinal);++ var peg = AssemblyDeclarationScope.Closure(Catalog, "Peg")!;+ Assert.Contains("Struct Peg", peg, StringComparison.Ordinal);+ Assert.DoesNotContain("Struct Pad", peg, StringComparison.Ordinal);+ Assert.DoesNotContain("Function twice", peg, StringComparison.Ordinal);++ Assert.Null(AssemblyDeclarationScope.Closure(Catalog, "Missing"));+ }++ [Fact]+ public void UnnamedDeclarationsFollowTheClosuresTheyMention()+ {+ var catalog = Catalog + "\nModify Pad {\n Hole<Shaft> Bore { On: +Z Center: Point2(0mm, 0mm) Diameter: 2mm End: ThroughAll }\n}\n";+ Assert.Contains("Modify Pad", AssemblyDeclarationScope.Closure(catalog, "Pad")!, StringComparison.Ordinal);+ Assert.DoesNotContain("Modify Pad", AssemblyDeclarationScope.Closure(catalog, "Peg")!, StringComparison.Ordinal);+ }++ public static TheoryData<string> CanonicalAssemblies()+ {+ var data = new TheoryData<string>();+ var root = FirmamentCorpusHarness.ResolveFixtureFullPath("fixtures/Canonical/AssemblyInterfaces");+ // The guitar (56 definitions) is covered by the showcase hash budgets (Aetheris.Web.Perf).+ foreach (var file in Directory.GetFiles(root, "*.firm*", SearchOption.AllDirectories)+ .Where(file => !file.Contains("GuitarX0", StringComparison.Ordinal)).Order(StringComparer.Ordinal))+ data.Add(Path.GetRelativePath(root, file).Replace('\\', '/'));+ return data;+ }++ [Theory]+ [MemberData(nameof(CanonicalAssemblies))]+ public void ScopedDeclarationsBuildTheSameAssemblyAsTheWholeCatalog(string fixture)+ {+ var path = FirmamentCorpusHarness.ResolveFixtureFullPath("fixtures/Canonical/AssemblyInterfaces/" + fixture);+ var scoped = Compile(path, scope: true);+ var whole = Compile(path, scope: false);+ Assert.Equal(whole.IsSuccess, scoped.IsSuccess);+ if (!whole.IsSuccess) return;+ Assert.Equal(Codes(whole), Codes(scoped));+ Assert.Equal(Definitions(whole), Definitions(scoped));+ Assert.Equal(whole.Ir!.Instances.Select(i => (i.StableId, string.Join(',', i.ResolvedTransform?.Matrix ?? []))),+ scoped.Ir!.Instances.Select(i => (i.StableId, string.Join(',', i.ResolvedTransform?.Matrix ?? []))));+ }++ [Fact]+ public void IncludeEditInvalidatesOnlyTheDefinitionsThatUseIt()+ {+ var path = FirmamentCorpusHarness.ResolveFixtureFullPath("fixtures/Canonical/AssemblyInterfaces/IndustrialAtlas/atlas-industrial.firmament");+ var directory = Path.GetDirectoryName(path)!;+ var documents = Directory.GetFiles(directory, "*.firmament")+ .ToDictionary(Path.GetFileName, file => File.ReadAllText(file).Replace("\r\n", "\n"), StringComparer.Ordinal)!;+ using var session = new FirmamentCompilationSession();+ FirmamentProjectSnapshot Project() => new("atlas-industrial.firmament", documents);+ var first = session.CompileProject(Project());+ Assert.True(first.IsSuccess, string.Join("\n", first.Diagnostics.Select(d => d.Message)));+ var gripper = documents["gripper.firmament"];+ documents["gripper.firmament"] = gripper + "\n// a comment only the gripper's definitions see\n";+ var edited = session.CompileProject(Project());+ Assert.True(edited.IsSuccess);+ Assert.True(edited.Reuse!.ReusedDefinitions > 0, "an edit to one include must not rebuild every definition");+ Assert.True(edited.Reuse.RebuiltDefinitions < edited.Reuse.Definitions.Count);+ var fresh = new AssemblyM1Pipeline().CompileProject(Project());+ Assert.Equal(Definitions(fresh), Definitions(edited));+ }++ private static AssemblyM1CompilationResult Compile(string path, bool scope)+ {+ AssemblyDefinitionMaterializer.ScopeDeclarations = scope;+ try { return new AssemblyM1Pipeline().CompileFile(path); }+ finally { AssemblyDefinitionMaterializer.ScopeDeclarations = true; }+ }++ private static string[] Codes(AssemblyM1CompilationResult result) =>+ result.Diagnostics.Select(d => d.Code).Order(StringComparer.Ordinal).ToArray();++ private static string[] Definitions(AssemblyM1CompilationResult result) =>+ result.Geometry!.Artifact.Definitions.Select(d => d.DefinitionIdentity + "=" + d.StepSha256).Order(StringComparer.Ordinal).ToArray();+}diff --git a/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.cs b/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.csnew file mode 100644index 00000000..99c8c7a5--- /dev/null+++ b/Aetheris.Kernel.Firmament/Assembly/AssemblyDeclarationScope.cs@@ -0,0 +1,151 @@+using Aetheris.Kernel.Firmament.FirmamentV2;++namespace Aetheris.Kernel.Firmament.Assembly;++/// <summary>+/// BUILD-PERF-M2: the declarations one assembly definition actually depends on.+///+/// An assembly carries one declaration catalog (every included document, concatenated). Each part+/// definition used to be compiled against the whole catalog and cached under its whole text, so+/// editing any included document recompiled every part. This splits the catalog into top-level+/// declarations and keeps the transitive closure of the ones the definition's template references.+/// The materializer compiles that closure, and the definition cache keys on it, so an edit only+/// invalidates the definitions whose closure it touches.+///+/// The split is deliberately conservative; a mistake can only make a closure larger, or make it+/// fail to compile, in which case the materializer falls back to the whole catalog:+/// <list type="bullet">+/// <item>A declaration starts only at a column-0 identifier, outside any bracket, directly after a+/// token that ends a statement ('}' or ';'). Anything else continues the previous declaration, so+/// multi-line expressions are never cut; a declaration merged this way keeps the names of every+/// statement in it.</item>+/// <item>A declaration's names are the identifiers in its header (before its first '{', '=', '('+/// or ':' outside '<…>'), except keywords: the first word of any declaration and a few+/// connective words. A name several declarations share pulls in all of them; a stray name only+/// enlarges closures.</item>+/// <item>Any identifier in a declaration's text references every declaration with that name.</item>+/// <item>A declaration without a name of its own (a modifier, an annotation block) joins every+/// closure whose names it mentions.</item>+/// </list>+/// The closure keeps the declarations in their original order and text, joined by newlines.+/// </summary>+internal static class AssemblyDeclarationScope+{+ private sealed record Declaration(int Start, int End, IReadOnlySet<string> Names, IReadOnlySet<string> References);++ // Words that join a declaration header without naming anything.+ private static readonly string[] Connectives = ["Struct", "Assembly", "Sites", "From", "using", "with", "Set", "Path", "Model", "Interface", "Part", "Template"];+ private static readonly object Gate = new();+ private static string? lastText;+ private static IReadOnlyList<Declaration>? lastDeclarations;++ /// <summary>The closure of <paramref name="name"/>'s declaration, or null when the catalog has no+ /// declaration that owns that name alone (the caller then uses the whole catalog).</summary>+ public static string? Closure(string declarations, string name)+ {+ ArgumentNullException.ThrowIfNull(declarations);+ IReadOnlyList<Declaration> split;+ lock (Gate)+ {+ if (!ReferenceEquals(lastText, declarations) && !string.Equals(lastText, declarations, StringComparison.Ordinal))+ {+ lastDeclarations = Split(declarations);+ lastText = declarations;+ }+ split = lastDeclarations!;+ }+ var owners = split.Where(d => d.Names.Contains(name)).ToArray();+ if (owners.Length == 0) return null;+ var included = new bool[split.Count];+ var names = new HashSet<string>(StringComparer.Ordinal);+ var pending = new Queue<int>();+ void Include(int index)+ {+ if (included[index]) return;+ included[index] = true;+ names.UnionWith(split[index].Names);+ pending.Enqueue(index);+ }+ foreach (var owner in owners) Include(IndexOf(split, owner));+ while (true)+ {+ while (pending.Count > 0)+ {+ var references = split[pending.Dequeue()].References;+ for (var i = 0; i < split.Count; i++)+ if (!included[i] && split[i].Names.Overlaps(references)) Include(i);+ }+ // Unnamed declarations act on what they mention; they follow every closure they touch.+ var joined = false;+ for (var i = 0; i < split.Count; i++)+ if (!included[i] && split[i].Names.Count == 0 && split[i].References.Overlaps(names)) { Include(i); joined = true; }+ if (!joined) break;+ }+ if (included.All(value => value)) return declarations;+ return string.Join("\n", split.Where((_, i) => included[i]).Select(d => declarations[d.Start..d.End]));+ }++ private static int IndexOf(IReadOnlyList<Declaration> split, Declaration declaration)+ {+ for (var i = 0; i < split.Count; i++) if (ReferenceEquals(split[i], declaration)) return i;+ throw new InvalidOperationException("declaration-scope-index");+ }++ private static IReadOnlyList<Declaration> Split(string text)+ {+ var tokens = FirmamentLanguageAnalysisService.Lex(text).Where(token => token.Shape != "comment").ToArray();+ // Candidates: column-0 identifiers outside brackets (statement beginnings). Starts: the+ // candidates that follow a finished statement; only those split declarations.+ var starts = new List<int>();+ var candidates = new List<int>();+ var depth = 0;+ for (var i = 0; i < tokens.Length; i++)+ {+ var token = tokens[i];+ var candidate = depth == 0 && token.Shape == "identifier" && (token.Start == 0 || text[token.Start - 1] == '\n');+ if (candidate) candidates.Add(i);+ if (i == 0) starts.Add(0);+ else if (candidate && tokens[i - 1].Text is "}" or ";") starts.Add(i);+ depth += token.Text switch { "{" or "(" or "[" => 1, "}" or ")" or "]" => -1, _ => 0 };+ if (depth < 0) depth = 0;+ }+ var headers = new List<HashSet<string>>();+ var bodies = new List<(int Start, int End, HashSet<string> References)>();+ for (var s = 0; s < starts.Count; s++)+ {+ var first = starts[s];+ var last = s + 1 < starts.Count ? starts[s + 1] : tokens.Length;+ // A merged declaration (statements without a terminator in between) has one header+ // per statement it contains.+ var header = new HashSet<string>(StringComparer.Ordinal);+ foreach (var headerStart in candidates.Where(c => c >= first && c < last).Prepend(first).Distinct())+ {+ var angle = 0;+ for (var i = headerStart; i < last; i++)+ {+ var t = tokens[i].Text;+ if (t is "{" or "=" or "(" or ":" && angle == 0) break;+ if (t == "<") angle++;+ else if (t == ">") angle = Math.Max(0, angle - 1);+ else if (angle == 0 && tokens[i].Shape == "identifier") header.Add(t);+ }+ }+ var references = new HashSet<string>(StringComparer.Ordinal);+ for (var i = first; i < last; i++) if (tokens[i].Shape == "identifier") references.Add(tokens[i].Text);+ headers.Add(header);+ // Text runs from the declaration's first token to the start of the next declaration's,+ // so trailing comments and whitespace stay with the declaration they follow.+ var start = s == 0 ? 0 : tokens[first].Start;+ var end = s + 1 < starts.Count ? tokens[starts[s + 1]].Start : text.Length;+ bodies.Add((start, end, references));+ }+ var keywords = new HashSet<string>(Connectives, StringComparer.Ordinal);+ foreach (var first in candidates.Concat(starts)) if (first < tokens.Length && tokens[first].Shape == "identifier") keywords.Add(tokens[first].Text);+ return bodies.Select((body, index) =>+ {+ var names = headers[index].Where(n => !keywords.Contains(n)).ToHashSet(StringComparer.Ordinal);+ var references = body.References.Where(r => !names.Contains(r)).ToHashSet(StringComparer.Ordinal);+ return new Declaration(body.Start, body.End, names, references);+ }).ToArray();+ }+}diff --git a/Aetheris.Kernel.Firmament/Assembly/AssemblyM1Execution.cs b/Aetheris.Kernel.Firmament/Assembly/AssemblyM1Execution.csindex 38696ea4..ee377ea5 100644--- a/Aetheris.Kernel.Firmament/Assembly/AssemblyM1Execution.cs+++ b/Aetheris.Kernel.Firmament/Assembly/AssemblyM1Execution.cs@@ -63,7 +63,8 @@ internal sealed record MaterializedAssemblyDefinition(AssemblyDefinitionArtifactIr Artifact,string? CanonicalStep = null,FirmamentCirRetention? Cir = null,- string? InputKey = null);+ string? InputKey = null,+ string? DeclarationScope = null);/// <summary>/// M1's definition seam: specialize through the ordinary Firmament compiler, then@@ -181,14 +182,7 @@ internal static class AssemblyDefinitionMaterializer// the standalone build route would otherwise select the Gear profile and// successfully return that gear for every shaft, support and wheel.// Use producer-owned declaration spans; retain offsets for diagnostics.- if (gearDocument.Gears.Count > 0)- {- var ordinaryDeclarations = definitionSource.ToCharArray();- foreach (var sibling in gearDocument.Gears)- for (var i = sibling.SourceSpan.Start; i < sibling.SourceSpan.Start + sibling.SourceSpan.Length; i++)- if (ordinaryDeclarations[i] is not ('\r' or '\n')) ordinaryDeclarations[i] = ' ';- definitionSource = new string(ordinaryDeclarations);- }+ definitionSource = WithoutGears(definitionSource, gearDocument);}var externalStep = System.Text.RegularExpressions.Regex.Match(definitionIdentity, "^ExternalStep<\\\"(?<path>[^\\\"]+)\\\">$", System.Text.RegularExpressions.RegexOptions.CultureInvariant);if (externalStep.Success)@@ -220,10 +214,56 @@ internal static class AssemblyDefinitionMaterializernew(externalStableId, definitionIdentity, "external-step:" + externalHash[..16], externalHash, Metrics(externalImport.Value), externalProvenance), stepText);}if (string.IsNullOrWhiteSpace(definitionSource) || !definitionIdentity.Contains('<', StringComparison.Ordinal)) return null;- // Assembly files keep reusable declarations beside relational assembly syntax.+ // BUILD-PERF-M2: compile from the declarations this definition depends on. If that closure+ // does not compile on its own (the scope analysis is conservative, but not a full binder),+ // the definition compiles against the whole catalog exactly as before.+ if (ScopeDeclarations && AssemblyDeclarationScope.Closure(definitionSource, TemplateName(definitionIdentity)) is { } scoped+ && !ReferenceEquals(scoped, definitionSource))+ {+ var attempt = new List<AssemblyDiagnostic>();+ MaterializedAssemblyDefinition? scopedResult = null;+ try { scopedResult = MaterializeOrdinary(definitionIdentity, scoped, sourceIdentity, attempt, importBody); }+ catch (Exception exception) when (exception is not OutOfMemoryException) { scopedResult = null; }+ if (scopedResult is not null && attempt.All(diagnostic => diagnostic.Severity != AssemblyDiagnosticSeverity.Error))+ {+ diagnostics.AddRange(attempt);+ return scopedResult with { DeclarationScope = scoped };+ }+ }+ return MaterializeOrdinary(definitionIdentity, definitionSource, sourceIdentity, diagnostics, importBody);+ }++ /// <summary>Compiles scoped declarations instead of the whole catalog. Tests switch it off for+ /// one execution flow to compare both; it is flow-local so parallel tests are unaffected.</summary>+ internal static bool ScopeDeclarations { get => !WholeCatalog.Value; set => WholeCatalog.Value = !value; }+ private static readonly AsyncLocal<bool> WholeCatalog = new();++ internal static string TemplateName(string definitionIdentity) => definitionIdentity[..definitionIdentity.IndexOf('<')];++ /// <summary>The declarations an ordinary (non-Gear) definition compiles against: Gear roots are+ /// blanked (offsets kept), so a template specialization never selects a sibling Gear profile.</summary>+ internal static string OrdinaryDeclarations(string declarations)+ {+ if (!GearAuthoring.HasGearDefinitions(declarations)) return declarations;+ return WithoutGears(declarations, GearAuthoring.ParseDefinitions(declarations));+ }++ private static string WithoutGears(string declarations, GearAuthoringDocument gearDocument)+ {+ if (gearDocument.Gears.Count == 0) return declarations;+ var ordinaryDeclarations = declarations.ToCharArray();+ foreach (var sibling in gearDocument.Gears)+ for (var i = sibling.SourceSpan.Start; i < sibling.SourceSpan.Start + sibling.SourceSpan.Length; i++)+ if (ordinaryDeclarations[i] is not ('\r' or '\n')) ordinaryDeclarations[i] = ' ';+ return new string(ordinaryDeclarations);+ }++ private static MaterializedAssemblyDefinition? MaterializeOrdinary(string definitionIdentity, string definitionSource, string sourceIdentity,+ List<AssemblyDiagnostic> diagnostics, Func<string, KernelResult<BrepBody>> importBody)+ { // Assembly files keep reusable declarations beside relational assembly syntax.// Wrap that declaration catalog in the ordinary V2 Model root for the exact// same compiler/build path used by standalone Firmament parts.- var templateName = definitionIdentity[..definitionIdentity.IndexOf('<')];+ var templateName = TemplateName(definitionIdentity);var inspectionDiagnostics = new List<string>();var targetKind = FirmamentV2TemplateExpansion.Inspect(definitionSource, inspectionDiagnostics).SingleOrDefault(template => string.Equals(template.Name, templateName, StringComparison.Ordinal))?.TargetKinddiff --git a/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs b/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.csindex a6d49821..8d914106 100644--- a/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs+++ b/Aetheris.Kernel.Firmament/Assembly/FirmamentCompilationSession.cs@@ -119,13 +119,22 @@ internal sealed class AssemblyDefinitionCache(int capacity)List<AssemblyDiagnostic> diagnostics, FirmamentProjectSnapshot? project,Func<MaterializedAssemblyDefinition?> build){- string? key;- using (BuildPerfTrace.Phase("assembly.cache-key")) key = Key(identity, declarations, sourceIdentity, project);+ // 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;+ using (BuildPerfTrace.Phase("assembly.cache-key"))+ {+ key = Key(identity, declarations, sourceIdentity, project);+ scopedKey = key is null ? null : ScopedKey(identity, declarations, sourceIdentity);+ }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)+ if (entry.Key == key || (scopedKey is not null && entry.Key == scopedKey)){BrepBody body;using (BuildPerfTrace.Phase("assembly.cache-hit-clone")) body = entry.Metadata.Body.Clone();@@ -141,7 +150,18 @@ internal sealed class AssemblyDefinitionCache(int capacity)return result;string? keyAfterBuild;using (BuildPerfTrace.Phase("assembly.cache-key")) keyAfterBuild = Key(identity, declarations, sourceIdentity, project);- if (keyAfterBuild != key)+ if (result.DeclarationScope is not null)+ {+ // Stored under the closure the build compiled; a scoped key that disagrees means the+ // materializer and the cache computed different closures: keep it out of the cache.+ if (scopedKey is null || scopedKey != Hash(sourceIdentity, identity, "scoped\0" + result.DeclarationScope))+ {+ Evidence[^1] = new(identity, false, "declaration-scope-mismatch");+ return result;+ }+ key = scopedKey;+ }+ else if (keyAfterBuild != key){Evidence[^1] = new(identity, false, "resource-changed-during-build");return result;@@ -184,6 +204,20 @@ internal sealed class AssemblyDefinitionCache(int capacity)return Step242Importer.ImportBody(stepText);}+ 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);+ return AssemblyDeclarationScope.Closure(ordinary, AssemblyDefinitionMaterializer.TemplateName(identity)) is { } scoped && !ReferenceEquals(scoped, ordinary)+ ? Hash(sourceIdentity, identity, "scoped\0" + scoped) : null;+ }++ private static string Hash(string sourceIdentity, string identity, string inputs) =>+ Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(+ "assembly-definition-cache-v1\0" + sourceIdentity + "\0" + identity + "\0" + inputs)));+private static string? Key(string identity, string? declarations, string sourceIdentity, FirmamentProjectSnapshot? project){var file = Regex.Match(identity, "^(?:SectionChainFile|LoftFile|ExternalStep)<\\\"(?<path>[^\\\"]+)\\\">$", RegexOptions.CultureInvariant);diff --git a/Aetheris.Web.Perf/PerfProgram.cs b/Aetheris.Web.Perf/PerfProgram.csindex 4bd808b2..77437d99 100644--- a/Aetheris.Web.Perf/PerfProgram.cs+++ b/Aetheris.Web.Perf/PerfProgram.cs@@ -1,6 +1,7 @@using System.Diagnostics;using System.Security.Cryptography;using System.Text.Json;+using System.Text.Json.Nodes;namespace Aetheris.Web.Perf;@@ -12,6 +13,7 @@ public static class PerfProgramif (args.Length > 0 && args[0] == "compare-step") return CompareStep.Run(args[1..]);if (args.Length > 0 && args[0] == "verify-bridge") return VerifyBridge.Run(args[1..]);if (args.Length > 0 && args[0] == "verify-incremental") return VerifyIncremental.Run(args[1..]);+ if (args.Length > 0 && args[0] == "profile") return ProfileOne(args[1..]);if (args.Length != 1) { Console.Error.WriteLine("Usage: Aetheris.Web.Perf <helix-fixture> | Aetheris.Web.Perf showcase --help | Aetheris.Web.Perf compare-step <a.step> <b.step>"); return 2; }var source = File.ReadAllText(args[0]);var variants = new[] { ("helix-1", source), ("helix-2", source.Replace("Turns: 1", "Turns: 2", StringComparison.Ordinal)),@@ -57,4 +59,33 @@ public static class PerfProgram}return 0;}++ /// <summary>One build of one document through the runtime entry point, with its top phases:+ /// <c>profile <file> [--project]</c> (--project sends the file's directory as project documents).</summary>+ private static int ProfileOne(string[] args)+ {+ if (args.Length < 1) { Console.Error.WriteLine("Usage: Aetheris.Web.Perf profile <file> [--project]"); return 2; }+ var path = Path.GetFullPath(args[0]);+ var root = Path.GetFileName(path);+ Dictionary<string, string>? documents = null;+ if (args.Contains("--project"))+ documents = Directory.GetFiles(Path.GetDirectoryName(path)!).Where(f => f.EndsWith(".firmament", StringComparison.Ordinal) || f.EndsWith(".firmasm", StringComparison.Ordinal))+ .ToDictionary(Path.GetFileName, f => File.ReadAllText(f).Replace("\r\n", "\n"), StringComparer.Ordinal)!;+ var started = Stopwatch.GetTimestamp();+ var response = JsonNode.Parse(Aetheris.Web.Runtime.Program.Invoke(JsonSerializer.Serialize(new+ {+ operation = "compile", source = File.ReadAllText(path).Replace("\r\n", "\n"), sourceName = documents is null ? path : root,+ projectDocuments = documents, performance = true+ })))!;+ var elapsed = Stopwatch.GetElapsedTime(started).TotalMilliseconds;+ var model = response["result"]?["model"];+ Console.WriteLine($"{root}: {elapsed:0} ms, success={response["result"]?["success"]}");+ foreach (var phase in (model?["timings"]?["profile"]?["phases"]?.AsArray() ?? [])+ .GroupBy(p => p!["name"]!.GetValue<string>())+ .Select(g => (Name: g.Key, Count: g.Count(), Ms: g.Sum(p => p!["exclusiveMilliseconds"]!.GetValue<double>())))+ .OrderByDescending(p => p.Ms).Take(20))+ Console.WriteLine($" {phase.Ms,9:0.0} ms {phase.Count,5}x {phase.Name}");+ if (model is null) Console.WriteLine(response.ToJsonString()[..Math.Min(2000, response.ToJsonString().Length)]);+ return 0;+ }}