fix(irregular): stop at the deadline while preparing Lattice blocks

Irregular overran DeadlineNestingEngine's 30 s limit by up to 50x in the
ML flag-sweep pilot (1,537 s on a 450-per-sheet part). Stack samples put
every second of the overrun in BlockCatalog.Rectangles: after a sheet fill
places a Lattice block of hundreds of detailed parts, the next block
preparation builds the free region one Clipper difference per part and
then sorts out every maximal rectangle with a quadratic dominance pass,
and neither checked the token. It shows only once best fits are cached
(a warm app, worker or sweep process), which is when the fill gets there
within the limit.

MaximalRectangles.InRegion now takes a token and a `largest` count: it
stops dominance checks past that rank, keeping every result tied in area
with the last, so its results are exactly the full search's down to that
area and Rectangles' top two are unchanged. The free region checks the
token per part. Layouts do not change; the reproducing job's whole solve
now takes 34 s instead of over 1,000.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-21 19:21:09 -05:00
1 parent f28fb5adb6
commit aea032a24d
5 files changed
+209 -11

No files matched your search

@@ -201,13 +201,16 @@ internal sealed class BlockCatalog : IDisposable
/// <summary>
/// The free rectangles of a work area blocks are offered: the largest clear of the parts
/// placed, their spacing included.
/// placed, their spacing included. Only the largest are searched for: on a sheet of many
/// detailed parts, sorting out every maximal rectangle takes minutes.
/// </summary>
internal static IReadOnlyList<Box> Rectangles(Box work, IReadOnlyList<Placed> placed, double spacing) =>
MaximalRectangles.InRegion(FreeRegion(work, placed, spacing)).OrderByDescending(b => b.Area())
internal static IReadOnlyList<Box> Rectangles(Box work, IReadOnlyList<Placed> placed, double spacing,
CancellationToken token = default) =>
MaximalRectangles.InRegion(FreeRegion(work, placed, spacing, token), largest: MaxRectangles, token: token)
.OrderByDescending(b => b.Area())
.ThenBy(b => b.Left).ThenBy(b => b.Bottom).ThenBy(b => b.Length).Take(MaxRectangles).ToArray();
private static PathsD FreeRegion(Box work, IReadOnlyList<Placed> placed, double spacing)
private static PathsD FreeRegion(Box work, IReadOnlyList<Placed> placed, double spacing, CancellationToken token)
{
var free = new PathsD { new PathD
{
@@ -216,6 +219,7 @@ internal sealed class BlockCatalog : IDisposable
} };
foreach (var part in placed)
{
token.ThrowIfCancellationRequested();
// Physical occupied material, not a reference-point region for any moving part.
// The catalog currently prepares solid outlines; future profile preparation owns holes.
var blocked = Clipper.InflatePaths(new PathsD { part.Orientation.Outline },
@@ -336,7 +336,7 @@ internal sealed class FrontierPacker
if (blocks == null || sheet.BlockPreparations++ >= MaxBlockPreparations)
return;
var left = sheet.Left;
var rectangles = BlockCatalog.Rectangles(work, sheet.Placed, stock.PartSpacing);
var rectangles = BlockCatalog.Rectangles(work, sheet.Placed, stock.PartSpacing, sheet.Token);
foreach (var type in types.Where(t => left[t.Index] >= MinBlockCopies)
.OrderByDescending(t => t.Area * left[t.Index]).ThenBy(t => t.Index).Take(MaxBlockTypes))
foreach (var rectangle in rectangles)