perf(engine): key fill caches by source drawing so they hit across trials

Every DefaultPlateFiller.Fill makes a fresh canonical copy of the drawing,
and BestFitCache/FillResultCache keyed by drawing reference, so fills never
shared results and the static caches grew without bound.

CanonicalFrame now records which drawing each canonical copy came from.
Both caches key weakly on that source drawing, so every canonical copy
shares one entry and released drawings can be collected. An entry is
dropped when the drawing's Program instance or canonical angle changes.
Best-fit candidates are computed once per (drawing, spacing) through
BestFitFinder.FindCandidates and filtered per plate size with the same
filter FindBestFits uses. FillResultCache keeps canonical and
non-canonical callers apart.

Adds Debug-only PerfCounters for best-fit runs, offset perimeter builds
and Part.Intersects calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
aj
2026-09-23 14:29:43 -04:00
co-authored by Claude Opus 5.5
parent dc9e83ef17
commit 1c363504f5
8 changed files with 525 additions and 233 deletions
+18
View File
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using OpenNest.CNC;
using OpenNest.Geometry;
using OpenNest.Math;
@@ -11,6 +12,22 @@ namespace OpenNest.Engine
/// </summary>
public static class CanonicalFrame
{
// Maps each canonical copy to the drawing it was ultimately copied from, so caches keyed
// by drawing identity hit across the transient copies each fill makes.
private static readonly ConditionalWeakTable<Drawing, Drawing> sourceOf = new();
/// <summary>
/// Returns the drawing a canonical copy was made from (following copies of copies back to
/// the root), or <paramref name="drawing"/> itself when it is not a canonical copy.
/// </summary>
internal static Drawing SourceOf(Drawing drawing)
{
if (drawing == null)
return null;
return sourceOf.TryGetValue(drawing, out var root) ? root : drawing;
}
/// <summary>
/// Returns a new Drawing whose Program geometry is rotated to the canonical frame.
/// The source drawing is not mutated.
@@ -44,6 +61,7 @@ namespace OpenNest.Engine
Angle = 0.0,
},
};
sourceOf.AddOrUpdate(copy, SourceOf(drawing));
return copy;
}