This repository was archived by the owner on Oct 28, 2026. It is now read-only.
Repository navigation
Pool OutPt/OutRec/Active allocations and avoid repeated laps in CleanCollinear - #1117
Open
xaviergonz wants to merge 10 commits into
Open
xaviergonz wants to merge 10 commits into
xaviergonz wants to merge 10 commits into
Conversation
xaviergonz
force-pushed
the
perf/outpt-active-arena
branch
from
October 15, 2026 18:15
9e6cb12 to
dd30e75
Compare
Clipper2 is now maintained as a C++ library only. Remove the CSharp and Delphi trees, the C#/Delphi DLL consumer apps, and the C# CI workflow. The C# and Delphi sources remain in the archived upstream repository.
Emptied OutRecs can reference each other through their splits, and the branch that descends into them had no recursive_split guard. PolyTree output then overflowed the stack, reachable from a 13 vertex path.
BuildIntersectList's bottom-up merge sort always makes about log2(n) passes over the active edge list, even when no edges cross in the scanbeam, which is the usual case. AdjustCurrXAndCopyToSEL now also checks whether the edges are still in order, and if so there's nothing to find, so the merge sort is skipped. In that case every edge's curr_x is already TopX(top_y), so DoTopOfScanbeam doesn't recompute it either. Output is unchanged Fixes PolyClipping#3
The local minima list is already sorted by descending y, so Reset() no longer pushes every minima's y into the scanline queue. PopScanline() now returns the larger of the next local minima's y and the queue's top. The sequence of scanbeams is unchanged, so output is identical, but the queue stays much smaller. Fixes PolyClipping#2
PopScanline and PopLocalMinima both read the next local minima's y, so move that into PeekLocMinY, which also stops PopScanline reading it twice. Merge the two sources in one path and document that a y taken from a local minima stays pending until PopLocalMinima.
Every OutPt and Active was a separate heap allocation that was deleted individually, and malloc/free ended up being a big chunk of the time spent in offsets (lots of short lived OutPts) and open path clipping. Now they're allocated from chunks owned by ClipperBase and all released together in CleanUp, so deleting an OutPt is just unlinking it, and deleted Actives go on a free list and get reused (the names follow the old C# port's NewActive/PoolDeletedActive). Output is unchanged.
OutRecs are created for every local minima, so with lots of short output paths (open path offsets especially) allocating and deleting them was still a noticeable cost. They're now allocated from an ObjectPool like OutPts, destroyed in DisposeAllOutRecs and freed together.
Each time CleanCollinear removed a vertex, it restarted its "full lap" end condition, so on paths with lots of collinear vertices it ended up doing about two full laps. But removing a vertex can only make its two neighbours removable, and the next one is checked straight away. So after the first lap, only the vertices before removed ones are rechecked, in the same order that further full laps would reach them. The result is exactly the same.
Every pool used to start with a 32 object chunk, so even a 4 vertex union allocated (and freed) about 10KB, which made very small clipping operations slower than before pooling. The first chunks are now sized from the number of local minima (2 actives and 1 outrec each, and 4 outpts as a starting guess), and the chunks are linked through a small header instead of a list, so small operations now allocate about as much as they did without the pools.
xaviergonz
force-pushed
the
perf/outpt-active-arena
branch
from
October 31, 2026 06:53
516c108 to
aa5bf6a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Angus,
This is another one from profiling Clipper2 for a WebAssembly build. It's independent of #1115 and #1116, but it builds on the same work. After those two, offsets (open paths especially) were spending most of their remaining time on the allocator, plus one avoidable extra lap in
CleanCollinear. There are three commits, which can be reviewed separately.1. Allocate
OutPtandActiveobjects from a per-clipper pool (C++, Delphi)Every
OutPtand everyActivewas its own heap allocation and got deleted one at a time. In an open-path offset, about 30% of the time was going to malloc/free.The C# port already avoids this. It has
OutPtPoolListforOutPts, andNewActive/PoolDeletedActivewith a free list forActives. This commit does the same in C++ and Delphi, using the same names where it makes sense:OutPts come from a pool owned byClipperBase(outpt_pool_/FOutPtPool). They're all released together inCleanUp, so disposing of one during the sweep is just unlinking it.Actives come fromNewActive().DeleteFromAELhands them toPoolDeletedActive(), which clears them and puts them on a free list (free_actives_/FFreeActives) for reuse. As in C#, that's a separate list, so the "already deleted" check inDeleteFromAELstill works.ObjectPool<T>inclipper.engine.handTObjectPoolin Delphi. Chunks grow from 32 to 4096 objects. I didn't usestd::dequebecause MSVC's deque allocates objects this size one at a time, so it would gain nothing on Windows.Nothing is global or shared between instances, and nothing is kept once
Executefinishes.2. Allocate
OutRecs from a pool too (C++ only)There's an
OutRecfor every local minima, so with lots of short output paths these allocations were still noticeable. They now come from anObjectPoolas well.DisposeAllOutRecsdestroys each one (they all live inoutrec_list_) before the pool is released. C# already pools these (OutRecPoolList). I tried the same in Delphi, but FPC's allocator is already fast for records this size and it made no measurable difference, so I left Delphi as it was.3. Recheck only the affected vertices in
CleanCollinear(all ports)Each time
CleanCollinearremoved a vertex, it restarted its "full lap" end condition. On paths with lots of collinear vertices it ended up doing about two full laps; an open-path offset of a 5000-point circle walks around 33k vertices to remove 7.7k. But removing a vertex can only make its two neighbours removable, and the next one is checked straight away. So after the first lap, only the vertices before removed ones are rechecked, in the same order that further full laps would reach them. The result is exactly the same.Numbers
These are on an M1 Max. Times are ms per operation, best of 5. The C++ numbers were measured with #1115 and #1116 already applied, and the Delphi numbers (FPC 3.2.2) against plain
main.In C++, about 25% of the open-path improvement comes from commit 1, 10% from commit 2 and 3% from commit 3. Together with #1115 and #1116, C++ offsets are now about as fast as Clipper1's (the open-path square case is 0.98× of Clipper1, and the polygon cases are faster). In C#, commit 3 is within noise, since C# spends its time elsewhere.
The trade-off is that disposed
OutPts aren't returned to the heap until the clip finishes. That's the same as C#'sOutPtPoolList, and they're a small fraction of the total.Testing
maingives identical output in C++, C# and Delphi, including after each commit on its own. It runs 20k random cases with all clip types, fill rules,PreserveCollinearon and off, open paths, PolyTree output and offsets. To check that it actually exercises theCleanCollinearchange, I also tried a version that never rechecks, and the fuzz caught it.