fix(engine): stop waiting for the block fill's pair search on cancel

Irregular's block catalog fills a private plate per part type, and that
fill searched best-fit pairs through the overload that ignores the
token. On one CPU the search can take minutes, so a time limit waited
for it: the converted Nestbolt m-public-gear-1 job under a 60 s limit
ran past 180 s. PrivatePlateFill now passes its token, as PairCatalog
does since 9431006e; the search still finishes in the background and
fills the shared cache.

gear-1, Irregular, one CPU, 60 s limit: killed after 180 s -> stops at
60.0 s (no layout yet; its no-fit polygons are the next bottleneck).
This commit is contained in:
aj committed 2026-11-01 16:28:42 -05:00
1 parent 8a5f16069d
commit a89adc7911
2 files changed
+35 -1

No files matched your search

@@ -1,6 +1,7 @@
using OpenNest.Engine.BestFit;
using OpenNest.Engine.Jobs;
using OpenNest.Engine.Jobs.Adapters;
using OpenNest.Engine.Jobs.Placement;
using OpenNest.Engine.NestingEngines.Irregular;
using static OpenNest.Engine.Tests.NestingEngines.JobBuilder;
using static OpenNest.Engine.Tests.NestingEngines.Shapes;
@@ -128,6 +129,37 @@ public class IrregularPairPoolTests
}
}
[Fact]
public void PrivatePlateFill_CancelingStopsTheWaitForASlowSearch()
{
// Irregular's block catalog fills a private plate per type, which searches best-fit pairs
// too: a time limit must not wait for that search either.
BestFitCache.Clear();
var drawing = new Drawing("wedge", Wedge(6.3));
using var release = new ManualResetEventSlim();
var previous = BestFitCache.CreateEvaluator;
BestFitCache.CreateEvaluator = (_, _) => new BlockedEvaluator(release);
try
{
using var limit = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
var clock = System.Diagnostics.Stopwatch.StartNew();
Assert.ThrowsAny<OperationCanceledException>(() => PrivatePlateFill.Run(
drawing, RotationPolicy.Fixed(0), Spacing, 31.7, 17.3, 4, limit.Token));
Assert.True(
clock.Elapsed < TimeSpan.FromSeconds(5),
$"waited {clock.Elapsed.TotalSeconds:0.0} s"
);
}
finally
{
release.Set();
BestFitCache.CreateEvaluator = previous!;
BestFitCache.Clear();
}
}
[Fact]
public void IrregularEngine_FreshInstanceAndJob_PlacesIdenticallyWithoutSearchingAgain()
{
@@ -24,7 +24,9 @@ internal static class PrivatePlateFill
{
var plate = new Plate(new Size(width, length)) { PartSpacing = spacing };
// The drawing is private: stabilize this cache entry before Fill's candidate pruning.
var fits = BestFitCache.GetOrCompute(drawing, plate.Size.Length, plate.Size.Width, spacing);
// Cancelling stops the wait; the search finishes in the background and fills the cache.
var fits = BestFitCache.GetOrCompute(drawing, plate.Size.Length, plate.Size.Width, spacing,
token);
var sorted = fits.OrderBy(f => f.RotatedArea).ThenBy(f => f.Candidate.StrategyIndex)
.ThenBy(f => f.Candidate.Part2Rotation).ThenBy(f => f.Candidate.Part2Offset.X)
.ThenBy(f => f.Candidate.Part2Offset.Y).ThenBy(f => f.OptimalRotation).ToArray();