Files
OpenNest/OpenNest.Core/CNC/CuttingPlanning/LeadMaterialSet.cs
T
ajandClaude Opus 5.5 b97929a53d perf(planner): skip far parts on large plates without changing the plan
The 5k-washers corpus plate planned in about 90 s alone and timed out at the
corpus's 120 s limit whenever anything else ran, making the corpus fragile.
Profiling showed nearly all of it was linear scans over every part, repeated
per candidate: each lead check walked all 5,742 materials (and first re-tested
that all were complete), each rapid walked every completed contour, each
expansion rebuilt a HashSet of every cut part, each checker copy regrew its
obstacle list, and the nested-part search tested all 33 million part pairs.

A uniform grid over extents (ExtentGrid) now leaves out only parts whose
extents lie beyond the margin the existing skip tests use, and the rest are
visited in their original order, so every check returns exactly what the full
scan returned (same findings, same first obstacle, same budget charges):
- LeadPathValidator visits only materials not clear of the lead's queries or
  its midpoint's row, through a shared LeadMaterialSet built once per walk and
  replay; ContourEntryFeasibility reuses that set instead of copying it per part.
- ReleasedContourState keeps a read-only obstacle prefix with a grid, shared by
  copies, so copies stay cheap and rapids test only nearby contours.
- The search keeps one ordinal flag array per order instead of a HashSet per call.
- CuttingDependencyGraph tests each part only against hosts whose bounds can hold it.

Plate 1 alone: 93.3 s -> 12.3 s (capture 2.7 -> 1.2 s, plan 90.6 -> 11.1 s),
with the identical plan digest and 86,245 expansions; plate 2 also plans
identically. The problem-nest corpus (part-order,entries) gives every plate the
same outcome and expansion count as master, and the Collision kernel and every
tolerance are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 12:58:28 -05:00

45 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using OpenNest.Diagnostics;
namespace OpenNest.CNC.CuttingPlanning;
/// <summary>
/// An immutable list of placed materials with a grid over their extents, shared by every lead
/// check of one planning attempt. <see cref="LeadPathValidator"/> then visits only the materials
/// its full scan would query, in the same order, so a plate of thousands of parts does not walk
/// all of them for every lead. Its results are the full scan's.
/// </summary>
internal sealed class LeadMaterialSet : IReadOnlyList<LeadMaterialSnapshot>
{
private readonly LeadMaterialSnapshot[] materials;
private ExtentGrid grid;
internal LeadMaterialSet(IEnumerable<LeadMaterialSnapshot> materials)
{
this.materials = materials.ToArray();
HasNull = this.materials.Any(m => m == null);
AllComplete = !HasNull && this.materials.All(m => m.IsComplete);
}
/// <summary>Whether any material is missing.</summary>
internal bool HasNull { get; }
/// <summary>Whether every material is present and complete, as the validator requires.</summary>
internal bool AllComplete { get; }
public int Count => materials.Length;
public LeadMaterialSnapshot this[int index] => materials[index];
public IEnumerator<LeadMaterialSnapshot> GetEnumerator() => ((IEnumerable<LeadMaterialSnapshot>)materials).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>The grid over the materials' extents, built on first use; null unless every material is complete.</summary>
internal ExtentGrid Grid => AllComplete
? System.Threading.LazyInitializer.EnsureInitialized(ref grid, () => new ExtentGrid(materials.Select(m => m.Extent).ToArray()))
: null;
}