Files
OpenNest/OpenNest.Engine/Strategies/PairsFillStrategy.cs
T
ajandClaude Opus 5.5 538ccebd6d perf(engine): reuse stripe convergences across a fill pipeline
The Row and Column strategies converge identical stripe layouts (both lay
rows along both axes), every pair remnant strip along a full side of the
work area repeats the Row strategy's spans, and the expanding and
shrinking variants revisit each other's angles, often cycling between
two. StripeConvergenceCache, kept in the pipeline's shared state and
handed to the pair remnant strips, stores each convergence, the pair's
turned bounds and each estimate row by angle; one thread tiles a row
while others wait. Every value is a pure function of its key, so layouts
are bit-identical. On a 30-copy fill of an 80-vertex bracket this cut
estimate-row tilings from 3,226 to 560.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 01:36:19 -05:00

54 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Threading;
using OpenNest.Engine.Fill;
namespace OpenNest.Engine.Strategies
{
public class PairsFillStrategy : IFillStrategy
{
private static readonly AsyncLocal<bool> active = new();
public string Name => "Pairs";
public NestPhase Phase => NestPhase.Pairs;
public int Order => 100;
public List<Part> Fill(FillContext context)
{
// Prevent recursive PairFiller — remnant fills within PairFiller
// create a new engine that runs the full pipeline, which would
// invoke PairsFillStrategy again, causing deep recursion.
if (active.Value)
return null;
if (context.PartType == PartType.Circle)
return null;
active.Value = true;
try
{
var comparer = context.Policy?.Comparer;
var dedup = GridDedup.GetOrCreate(context.SharedState);
var filler = new PairFiller(context.Plate, comparer, dedup)
{
StripeConvergence = StripeConvergenceCache.GetOrCreate(context.SharedState),
};
var result = filler.Fill(
context.Item,
context.WorkArea,
context.Token,
context.ReportProgress
);
if (result.BestFits != null)
context.SharedState["BestFits"] = result.BestFits;
return result.Parts;
}
finally
{
active.Value = false;
}
}
}
}