From a89adc79113bc7f46dab8220538230dcd90420e3 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sat, 31 Oct 2026 19:20:48 -0400 Subject: [PATCH] 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). --- .../NestingEngines/IrregularPairPoolTests.cs | 32 +++++++++++++++++++ .../Jobs/Placement/PrivatePlateFill.cs | 4 ++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/OpenNest.Engine.Tests/NestingEngines/IrregularPairPoolTests.cs b/OpenNest.Engine.Tests/NestingEngines/IrregularPairPoolTests.cs index 1498ce2d..fa545475 100644 --- a/OpenNest.Engine.Tests/NestingEngines/IrregularPairPoolTests.cs +++ b/OpenNest.Engine.Tests/NestingEngines/IrregularPairPoolTests.cs @@ -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(() => 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() { diff --git a/OpenNest.Engine/Jobs/Placement/PrivatePlateFill.cs b/OpenNest.Engine/Jobs/Placement/PrivatePlateFill.cs index 6138eb9e..b2b46223 100644 --- a/OpenNest.Engine/Jobs/Placement/PrivatePlateFill.cs +++ b/OpenNest.Engine/Jobs/Placement/PrivatePlateFill.cs @@ -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();