Skip to content

Faster offsets of pours: clusters by edge distance, repeated clusters computed once, Memo - #7

Merged
MagicalTux merged 3 commits into
masterfrom
offset-clusters
Nov 24, 2026
Merged

MagicalTux merged 3 commits into
masterfrom
offset-clusters

Conversation

@MagicalTux

Copy link
Copy Markdown
Member

Round-join offsets of pour-shaped input (the openings a PCB tool runs for the minimum copper width) spend 92 % of their time in the union of the raw offset curves, and that union's profile is flat (noding 46 %, merging 13-17 %, sweep 9-11 %, assembly 7 %); building the raw curves is 5 %. What can be saved without changing a vertex of the output is how much of the union is computed at all.

Clusters by the distance of the edges (perf). Rings were in one cluster when the boxes of some of their edges came within a unit of each other, tested by a scan of both rings' chunks for every pair of rings with overlapping boxes. A pour's outer ring was compared whole with every hole, the search ran out of its budget and the union ran in one piece; long diagonal edges joined rings that only shared a box. Rings are now in one cluster when some of their edges come within three units of each other (snap rounding relates nothing farther apart than 2.4, argued in cluster.rs), found by a descent of a tree of boxes over each ring's chunks of edges.

Clusters that are another one moved are computed once, and Memo keeps the results of clusters from one operation to the next (feat): Boolean::memo, Memo::offset, Memo::offset_tagged, Memo::opening, Memo::closing. A cluster's result depends on its edges, the winding of the other rings around it, the fill rules and the operation, and moves with the cluster. A memo holds at most the bytes it is created with and drops what was used least recently. The output is the same with and without one.

Bench (examples/offset_bench.rs): recorded calls or generated pours, with a hash of every vertex of every result.

Openings of the pours of three routed boards, recorded while tracks were added (391, 19 and 51 calls; 3.3 M, 0.8 M and 1.5 M vertices), best of 3, ms:

0.0.6 this, no memo this, memo of 64 MiB 0.0.6, rayon 4 this, rayon 4 this, rayon 4, memo
board 1 8212 5641 3518 3474 2485 2003
board 2 2299 1666 1248 1018 716 579
board 3 3584 3112 1775 1695 1374 1021
generated pours 652 594 431 291 215 171

Same output: the hash of every vertex of every result (shrink and opening of every call) is unchanged on all four inputs, without rayon and with it on one and four threads, with and without a memo; with set_memo_verify every reused result is also computed and compared, on the recorded calls and in tests/memo.rs. The existing tests that compare clustered booleans and offsets with the one-piece computation pass; new unit tests compare the tree descent with all pairs of edges and clusters with one piece at every distance round the reach, and a property test checks that a boolean of moved input is the result moved.

Checked here: cargo fmt --check, clippy with all features and with none (-D warnings), tests in debug and release with all features and in release with none, rustdoc with -D warnings, cargo bench --no-run, cargo +1.89 check --all-features --lib. Not run here: the i686 target, Windows and macOS (CI), the fuzz targets, the Clipper2 oracle.

🤖 Generated with Claude Code

MagicalTux and others added 3 commits November 25, 2026 04:14
… boxes

Two rings were in one cluster when the boxes of some of their edges came
within a unit of each other, tested for every pair of rings with
overlapping boxes by a scan of both rings' chunks of edges. A pour's
outer ring was so compared whole with every hole inside it, the search
ran out of its budget and the union was computed in one piece; and long
diagonal edges put rings in one cluster that only shared a box.

Rings are now in one cluster when some of their edges come within three
units of each other (snap rounding relates nothing farther apart than
2.4, see the module documentation), found by a descent of a binary tree
of boxes over each ring's chunks of sixteen edges and then by the
distance of the edges whose boxes come within reach.

On the openings of the pours of a routed board recorded while tracks
were added (cynthion: 391 openings, 3.3 M vertices, 18.6 M edges of raw
offset curves), the unions computed in one piece for want of budget go
from 1.9 M edges to 0.05 M. The output is the same (tests compare
clusters with one piece at every distance round the reach; the hash of
every vertex of the recorded openings is unchanged).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
What a cluster of rings gives depends on its edges, on the winding of
the other rings around it and on the fill rules and operation, and it
moves with the cluster: every predicate is exact and takes differences
of coordinates, and new vertices are exact roundings of crossings.

Within one boolean, clusters that are an earlier cluster moved (the same
edges relative to their first vertex, the same base winding; found by a
hash and then compared) are computed once and the result moved.

A Memo keeps the results of clusters from one operation to the next
(Boolean::memo, Memo::offset, Memo::offset_tagged, Memo::opening,
Memo::closing): a zone poured again after a few tracks moved has most of
its clusters as they were. Entries hold the cluster's points and the
result's relative to the first vertex, in 32 bits a coordinate where
they fit; a memo holds at most the bytes it was created with and drops
what was used least recently. The output is the same with and without a
memo; lone simple rings are not kept (they are passed through anyway).

Openings of the pours of three routed boards recorded while tracks were
added, one thread, best of 3, with the clusters by distance of the
commit before (ms):

                     0.0.6   no memo   memo of 64 MiB
  cynthion            8212     5641      3518
  glasgow-revD1       2299     1666      1248
  tinytapeout-demo    3584     3112      1775

56 % of the edges of cynthion's lone rings that have to be noded are in
a ring that is an earlier ring of the same union moved, and 80 % in a
ring an earlier opening had. The hash of every vertex of every result is
unchanged, without rayon and with it on one and four threads; with
set_memo_verify every reused result is computed as well and compared
(tests/memo.rs, and the recorded openings).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
….rs)

Times shrink, grow and opening on recorded calls (a file of the inputs
of cadlab's openings) or on generated pours with their statistics: two
layers of a board of 100 mm, pads of footprints, vias and tracks grown
by their clearance taken from the pour, each poured four times while
tracks are added. Prints a hash of every vertex of every result, to
compare two builds; OFFSET_BENCH_MEMO runs the openings with a Memo kept
over the calls and OFFSET_BENCH_VERIFY checks every reused result.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@MagicalTux
MagicalTux merged commit 997d0f4 into master Nov 24, 2026
8 checks passed
@MagicalTux
MagicalTux deleted the offset-clusters branch November 24, 2026 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant