mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-25 02:37:39 -05:00
perf(fill): skip copy-distance searches whose result is thrown away
One Irregular decode of a small job took seconds because each block in the catalog is built by the full Fill pipeline (PrivatePlateFill), and inside it StripeFiller's estimate rows spent about 85% of the CPU in FillLinear's perpendicular TilePattern. A stripe estimate is exactly one pattern tall, so the pattern's copy distance, an all-pairs slide search, always exceeded the room left and the copies were discarded after the search had run in full. TilePattern now passes FindPatternCopyDistance the largest distance that could still place a copy: for a whole copy, the room past the pattern; for a pattern that may tile partially, the most room past any one of its parts. The distance is the maximum over every pair, so the search stops as soon as it passes that bound and the tile returns empty, which is what the full search led to. The public CopyDistance is unchanged. StripeFiller's angle searches also reuse each rotated pattern's bounding box per angle instead of cloning and rotating the parts again on every probe. Output is identical: the layouts of every benchmark job that finishes match the previous build. Measured A/B on this machine, alternating builds per job (Benchmark --engines Irregular --parallel 1, benchmark ms): - N26-2A5: 15.5 s -> 8.8 s (12.6 s -> ~7.2 s in quieter runs) - 73 of the 80 bench jobs (the other 7 time out at 5 minutes in both builds): 1,990 s -> 1,578 s total (-21%), process CPU 6,486 s -> 3,842 s (-41%), median job 0.91x. N26-295 now finishes in 242 s with the same layout where it hit the 5-minute limit before. - Problem-nest corpus auto-nest and fill areas: all at baseline. A parallel herringbone pair build was tried and dropped: it saved 1.2 s on N26-2A5 but cost 10 s and 45 s more CPU on P241017-01. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6867ae2ee4
commit
beccbae106
2 files changed
+92
-16
No files matched your search
@@ -128,7 +128,11 @@ namespace OpenNest.Engine.Fill
|
||||
/// geometry inflated by half-spacing — same primitive the Compactor uses — so arcs
|
||||
/// are exact and no bbox clamp is needed.
|
||||
/// </summary>
|
||||
private double FindPatternCopyDistance(Pattern patternA, NestDirection direction, OffsetPerimeterCache cache)
|
||||
/// <param name="useful">Farthest distance the caller can use. The search stops with
|
||||
/// positive infinity as soon as one part pair proves the copy must move farther; the
|
||||
/// exact distance is returned whenever it is within this bound.</param>
|
||||
private double FindPatternCopyDistance(Pattern patternA, NestDirection direction, OffsetPerimeterCache cache,
|
||||
double useful = double.PositiveInfinity)
|
||||
{
|
||||
if (patternA.Parts.Count == 1)
|
||||
return FindCopyDistance(patternA.Parts[0], direction, cache);
|
||||
@@ -205,6 +209,10 @@ namespace OpenNest.Engine.Fill
|
||||
|
||||
if (copyDist > maxCopyDistance)
|
||||
maxCopyDistance = copyDist;
|
||||
|
||||
// The copy distance is the maximum over every pair, so it can only grow.
|
||||
if (maxCopyDistance > useful)
|
||||
return double.PositiveInfinity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,9 +240,10 @@ namespace OpenNest.Engine.Fill
|
||||
int maxParts = 0
|
||||
)
|
||||
{
|
||||
var copyDistance = FindPatternCopyDistance(basePattern, direction, cache);
|
||||
var copyDistance = FindPatternCopyDistance(basePattern, direction, cache,
|
||||
UsefulCopyDistance(basePattern, direction, maxParts));
|
||||
|
||||
if (copyDistance <= 0)
|
||||
if (copyDistance <= 0 || double.IsPositiveInfinity(copyDistance))
|
||||
return new List<Part>();
|
||||
|
||||
var dim = GetDimension(basePattern.BoundingBox, direction);
|
||||
@@ -291,6 +300,27 @@ namespace OpenNest.Engine.Fill
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Farthest copy distance at which <see cref="TilePattern"/> can still add a part: the
|
||||
/// first whole copy must end within the limit, or, for a multi-part pattern, one part of
|
||||
/// it within the work area. Padded by <see cref="Tolerance.Epsilon"/> beyond the
|
||||
/// placement checks' own tolerance, so rounding in the clones' bounds never matters.
|
||||
/// A row tiled across a strip it already fills (Stripe's estimate rows) needs no exact
|
||||
/// copy distance: one slide past this bound settles that nothing fits.
|
||||
/// </summary>
|
||||
private double UsefulCopyDistance(Pattern basePattern, NestDirection direction, int maxParts)
|
||||
{
|
||||
var box = basePattern.BoundingBox;
|
||||
var useful = GetLimit(direction) - GetStart(box, direction) - GetDimension(box, direction);
|
||||
var partial = basePattern.Parts.Count > 1 && !(maxParts > 0 && basePattern.Parts.Count >= maxParts);
|
||||
if (partial)
|
||||
foreach (var part in basePattern.Parts)
|
||||
useful = System.Math.Max(useful, direction == NestDirection.Horizontal
|
||||
? WorkArea.Right - part.BoundingBox.Right
|
||||
: WorkArea.Top - part.BoundingBox.Top);
|
||||
return useful + 2 * Tolerance.Epsilon;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fallback tiling using bounding-box spacing when geometry-aware tiling
|
||||
/// produces overlapping parts.
|
||||
|
||||
@@ -181,9 +181,10 @@ public class StripeFiller
|
||||
var spacing = _context.Plate.PartSpacing;
|
||||
var sheetSpan = GetDimension(_context.WorkArea, axis);
|
||||
|
||||
var bounds = new RotatedBounds(pairParts);
|
||||
var (angle, waste, count) = variant == 0
|
||||
? ConvergeStripeAngle(pairParts, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity)
|
||||
: ConvergeStripeAngleShrink(pairParts, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity);
|
||||
? ConvergeStripeAngle(bounds, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity)
|
||||
: ConvergeStripeAngleShrink(bounds, sheetSpan, spacing, axis, _context.Token, _context.MaxQuantity);
|
||||
|
||||
var layout = new StripeLayout { Angle = angle, Waste = waste, Count = count };
|
||||
if (count > 0)
|
||||
@@ -392,10 +393,38 @@ public class StripeFiller
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bounding boxes of one pattern's parts rotated about their common center, by angle. A
|
||||
/// box is a pure function of the parts and the angle, and the angle searches sample the
|
||||
/// same angles over and over (every <see cref="FindAngleForTargetSpan"/> scans the same
|
||||
/// grid), so each is built once. The parts must not change while it is in use.
|
||||
/// </summary>
|
||||
private sealed class RotatedBounds
|
||||
{
|
||||
private readonly Dictionary<double, Box> boxes = new();
|
||||
|
||||
public RotatedBounds(List<Part> parts) => Parts = parts;
|
||||
|
||||
public List<Part> Parts { get; }
|
||||
|
||||
public Box At(double angle)
|
||||
{
|
||||
if (!boxes.TryGetValue(angle, out var box))
|
||||
boxes[angle] = box = FillHelpers.BuildRotatedPattern(Parts, angle).BoundingBox;
|
||||
return box;
|
||||
}
|
||||
}
|
||||
|
||||
public static double FindAngleForTargetSpan(
|
||||
List<Part> patternParts,
|
||||
double targetSpan,
|
||||
NestDirection axis
|
||||
) => FindAngleForTargetSpan(new RotatedBounds(patternParts), targetSpan, axis);
|
||||
|
||||
private static double FindAngleForTargetSpan(
|
||||
RotatedBounds patternParts,
|
||||
double targetSpan,
|
||||
NestDirection axis
|
||||
)
|
||||
{
|
||||
var bestAngle = 0.0;
|
||||
@@ -445,9 +474,9 @@ public class StripeFiller
|
||||
/// Returns the rotation angle that orients the pair with its short side
|
||||
/// along the given axis. Returns 0 if already oriented, PI/2 if rotated.
|
||||
/// </summary>
|
||||
private static double OrientShortSideAlong(List<Part> patternParts, NestDirection axis)
|
||||
private static double OrientShortSideAlong(RotatedBounds patternParts, NestDirection axis)
|
||||
{
|
||||
var box = FillHelpers.BuildRotatedPattern(patternParts, 0).BoundingBox;
|
||||
var box = patternParts.At(0);
|
||||
var span0 = GetDimension(box, axis);
|
||||
var perpSpan0 = axis == NestDirection.Horizontal ? box.Width : box.Length;
|
||||
|
||||
@@ -470,6 +499,15 @@ public class StripeFiller
|
||||
NestDirection axis,
|
||||
CancellationToken token = default,
|
||||
int maxParts = 0
|
||||
) => ConvergeStripeAngle(new RotatedBounds(patternParts), sheetSpan, spacing, axis, token, maxParts);
|
||||
|
||||
private static (double Angle, double Waste, int Count) ConvergeStripeAngle(
|
||||
RotatedBounds patternParts,
|
||||
double sheetSpan,
|
||||
double spacing,
|
||||
NestDirection axis,
|
||||
CancellationToken token,
|
||||
int maxParts
|
||||
)
|
||||
{
|
||||
var startAngle = OrientShortSideAlong(patternParts, axis);
|
||||
@@ -487,11 +525,19 @@ public class StripeFiller
|
||||
NestDirection axis,
|
||||
CancellationToken token = default,
|
||||
int maxParts = 0
|
||||
) => ConvergeStripeAngleShrink(new RotatedBounds(patternParts), sheetSpan, spacing, axis, token, maxParts);
|
||||
|
||||
private static (double Angle, double Waste, int Count) ConvergeStripeAngleShrink(
|
||||
RotatedBounds patternParts,
|
||||
double sheetSpan,
|
||||
double spacing,
|
||||
NestDirection axis,
|
||||
CancellationToken token,
|
||||
int maxParts
|
||||
)
|
||||
{
|
||||
var baseAngle = OrientShortSideAlong(patternParts, axis);
|
||||
var naturalPattern = FillHelpers.BuildRotatedPattern(patternParts, baseAngle);
|
||||
var naturalSpan = GetDimension(naturalPattern.BoundingBox, axis);
|
||||
var naturalSpan = GetDimension(patternParts.At(baseAngle), axis);
|
||||
|
||||
if (naturalSpan + spacing <= 0)
|
||||
return (0, double.MaxValue, 0);
|
||||
@@ -508,7 +554,7 @@ public class StripeFiller
|
||||
}
|
||||
|
||||
private static (double Angle, double Waste, int Count) ConvergeFromAngle(
|
||||
List<Part> patternParts,
|
||||
RotatedBounds patternParts,
|
||||
double startAngle,
|
||||
double sheetSpan,
|
||||
double spacing,
|
||||
@@ -527,7 +573,7 @@ public class StripeFiller
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
var rotated = FillHelpers.BuildRotatedPattern(patternParts, currentAngle);
|
||||
var rotated = FillHelpers.BuildRotatedPattern(patternParts.Parts, currentAngle);
|
||||
var pairSpan = GetDimension(rotated.BoundingBox, axis);
|
||||
var perpDim =
|
||||
axis == NestDirection.Horizontal
|
||||
@@ -587,7 +633,7 @@ public class StripeFiller
|
||||
}
|
||||
|
||||
private static double BisectForTarget(
|
||||
List<Part> patternParts,
|
||||
RotatedBounds patternParts,
|
||||
double lo,
|
||||
double hi,
|
||||
double targetSpan,
|
||||
@@ -625,12 +671,12 @@ public class StripeFiller
|
||||
return bestAngle;
|
||||
}
|
||||
|
||||
private static double GetRotatedSpan(List<Part> patternParts, double angle, NestDirection axis)
|
||||
private static double GetRotatedSpan(RotatedBounds patternParts, double angle, NestDirection axis)
|
||||
{
|
||||
var rotated = FillHelpers.BuildRotatedPattern(patternParts, angle);
|
||||
var box = patternParts.At(angle);
|
||||
return axis == NestDirection.Horizontal
|
||||
? rotated.BoundingBox.Length
|
||||
: rotated.BoundingBox.Width;
|
||||
? box.Length
|
||||
: box.Width;
|
||||
}
|
||||
|
||||
private static double GetDimension(Box box, NestDirection axis)
|
||||
|
||||
Reference in new issue
Block a user