Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel
B4: cache boolean judgment candidates and reduce success-path allocations
Yuechen Li6e27e8d371fa
diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.csindex 83a6f038..22f7f253 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBoolean.cs@@ -141,6 +141,12 @@ public static class BrepBoolean{internal const string BoxBoxUnionOrthogonalCellsStrictCandidate = "box_box_union_orthogonal_cells_strict";internal const string BoxBoxUnionFaceContactCellsCandidate = "box_box_union_face_contact_cells";+ private static readonly JudgmentEngine<BooleanCaseContext> BooleanCaseJudgmentEngine = new();+ private static readonly JudgmentEngine<BooleanSubtractIntersectContext> BooleanSubtractIntersectJudgmentEngine = new();+ private static readonly JudgmentEngine<OrthogonalUnionClassificationContext> OrthogonalUnionJudgmentEngine = new();+ private static readonly IReadOnlyList<JudgmentCandidate<BooleanCaseContext>> BooleanCaseCandidates = BuildBooleanCaseCandidates();+ private static readonly IReadOnlyList<JudgmentCandidate<BooleanSubtractIntersectContext>> BooleanSubtractIntersectCandidates = BuildBooleanSubtractIntersectCandidates();+ private static readonly IReadOnlyList<JudgmentCandidate<OrthogonalUnionClassificationContext>> OrthogonalUnionCandidates = BuildOrthogonalUnionCandidates();public static KernelResult<BrepBody> Union(BrepBody left, BrepBody right)=> Execute(new BooleanRequest(left, right, BooleanOperation.Union));@@ -228,8 +234,7 @@ public static class BrepBooleanpublic static BooleanCaseClassification ClassifyBooleanCase(BrepBody leftBody, BrepBody rightBody, BooleanOperation operation, ToleranceContext? tolerance = null){var context = BuildBooleanCaseContext(leftBody, rightBody, operation, tolerance);- var candidateResult = new JudgmentEngine<BooleanCaseContext>()- .Evaluate(context, BuildBooleanCaseCandidates());+ var candidateResult = BooleanCaseJudgmentEngine.Evaluate(context, BooleanCaseCandidates);if (!candidateResult.IsSuccess){return CreateUnsupportedGeneralClassification(context);@@ -423,8 +428,7 @@ public static class BrepBooleanprivate static BooleanCaseClassification RouteRecognizedSafeRootSubtractIntersectCase(BooleanCaseContext context){var subtractIntersectContext = BuildBooleanSubtractIntersectContext(context);- var selection = new JudgmentEngine<BooleanSubtractIntersectContext>()- .Evaluate(subtractIntersectContext, BuildBooleanSubtractIntersectCandidates());+ var selection = BooleanSubtractIntersectJudgmentEngine.Evaluate(subtractIntersectContext, BooleanSubtractIntersectCandidates);if (!selection.IsSuccess){return new BooleanCaseClassification(@@ -1049,7 +1053,7 @@ public static class BrepBooleancells,isConnectedCellUnion);- var classification = new JudgmentEngine<OrthogonalUnionClassificationContext>().Evaluate(candidateContext, BuildOrthogonalUnionCandidates());+ var classification = OrthogonalUnionJudgmentEngine.Evaluate(candidateContext, OrthogonalUnionCandidates);if (!classification.IsSuccess){var closestRejection = classification.Rejections.FirstOrDefault();diff --git a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.csindex 73e9386f..5ef8965e 100644--- a/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs+++ b/Aetheris.Kernel.Core/Brep/Boolean/BrepBooleanSafeCompositionGraphValidator.cs@@ -7,6 +7,7 @@ namespace Aetheris.Kernel.Core.Brep.Boolean;public static class BrepBooleanSafeCompositionGraphValidator{+ private static readonly JudgmentEngine<BooleanContinuationContext> ContinuationJudgmentEngine = new();private static readonly IReadOnlyList<JudgmentCandidate<BooleanContinuationContext>> ContinuationCandidates = BuildContinuationCandidates();public static bool TryValidateNextSubtract(@@ -643,7 +644,7 @@ public static class BrepBooleanSafeCompositionGraphValidatorSubtractStackDiagnostic: stackDiagnostic,SupportsIndependentContinuationOnAdditiveRoot: SupportsBoundedIndependentHoleContinuationOnRecognizedOrthogonalAdditiveRoot(composition, nextHole, tolerance));- var result = new JudgmentEngine<BooleanContinuationContext>().Evaluate(context, ContinuationCandidates);+ var result = ContinuationJudgmentEngine.Evaluate(context, ContinuationCandidates);if (!result.IsSuccess){diagnostic = BuildUnsupportedBlindContinuationDiagnostic(diff --git a/Aetheris.Kernel.Core/Judgment/JudgmentEngine.cs b/Aetheris.Kernel.Core/Judgment/JudgmentEngine.csindex 545502ef..16460273 100644--- a/Aetheris.Kernel.Core/Judgment/JudgmentEngine.cs+++ b/Aetheris.Kernel.Core/Judgment/JudgmentEngine.cs@@ -29,12 +29,14 @@ public readonly record struct JudgmentRejection(string CandidateName, string Reapublic readonly record struct JudgmentSelection<TContext>(JudgmentCandidate<TContext> Candidate, double Score);-public sealed class JudgmentResult<TContext>+public readonly record struct JudgmentResult<TContext>{- private JudgmentResult(JudgmentSelection<TContext>? selection, IReadOnlyList<JudgmentRejection> rejections)+ private static readonly IReadOnlyList<JudgmentRejection> EmptyRejections = Array.Empty<JudgmentRejection>();++ private JudgmentResult(JudgmentSelection<TContext>? selection, IReadOnlyList<JudgmentRejection>? rejections){Selection = selection;- Rejections = rejections;+ Rejections = rejections ?? EmptyRejections;}public bool IsSuccess => Selection.HasValue;@@ -44,7 +46,7 @@ public sealed class JudgmentResult<TContext>public IReadOnlyList<JudgmentRejection> Rejections { get; }public static JudgmentResult<TContext> Success(JudgmentSelection<TContext> selection)- => new(selection, []);+ => new(selection, null);public static JudgmentResult<TContext> Failure(IReadOnlyList<JudgmentRejection> rejections)=> new(null, rejections);@@ -62,24 +64,30 @@ public sealed class JudgmentEngine<TContext>return JudgmentResult<TContext>.Failure([new JudgmentRejection("<none>", "No candidates were supplied.")]);}- var rejections = new List<JudgmentRejection>();+ List<JudgmentRejection>? rejections = null;JudgmentSelection<TContext>? best = null;var bestIndex = -1;+ static void AddRejection(ref List<JudgmentRejection>? buffer, string candidateName, string reason)+ {+ buffer ??= new List<JudgmentRejection>();+ buffer.Add(new JudgmentRejection(candidateName, reason));+ }+for (var i = 0; i < candidates.Count; i++){var candidate = candidates[i];if (!candidate.IsAdmissible(context)){var reason = candidate.RejectionReason?.Invoke(context) ?? "Candidate predicates were not satisfied.";- rejections.Add(new JudgmentRejection(candidate.Name, reason));+ AddRejection(ref rejections, candidate.Name, reason);continue;}var score = candidate.Score(context);if (!double.IsFinite(score)){- rejections.Add(new JudgmentRejection(candidate.Name, "Candidate produced a non-finite score."));+ AddRejection(ref rejections, candidate.Name, "Candidate produced a non-finite score.");continue;}@@ -92,7 +100,7 @@ public sealed class JudgmentEngine<TContext>return best.HasValue? JudgmentResult<TContext>.Success(best.Value)- : JudgmentResult<TContext>.Failure(rejections);+ : JudgmentResult<TContext>.Failure(rejections is null ? Array.Empty<JudgmentRejection>() : rejections);}private static bool IsBetter(