Add allocation cache for ConvexConvexSutherlandHodgman clipping#414
Draft
asinghvi17 wants to merge 1 commit into
Draft
Add allocation cache for ConvexConvexSutherlandHodgman clipping#414asinghvi17 wants to merge 1 commit into
ConvexConvexSutherlandHodgman clipping#414asinghvi17 wants to merge 1 commit into
Conversation
Add an exported `SutherlandHodgmanCache` that holds reusable ping-pong buffers for the Sutherland-Hodgman edge-clipping loop, threaded through `intersection` via the `cache` keyword argument. With a cache, the only remaining allocation is the returned polygon itself. The default (`cache = nothing`) allocates fresh buffers per call and is always thread-safe; sharing an explicit cache across tasks is the caller's responsibility. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ConvexConvexSutherlandHodgmanis the fast path for convex-convex polygon intersection, the kind of routine that gets called in a hot loop (e.g. coverage/regridding over many cells). Previously every call allocated a fresh vector per clip edge — m+1 vector allocations plus growth reallocations for an m-gon clip polygon. Since the algorithm is purely iterative (one pass per clip edge), all intermediate state can live in reusable buffers.This PR adds an exported
SutherlandHodgmanCache{P}with two ping-pong buffers (shared by both manifolds) plusclip/subjectbuffers for the spherical path. It is threaded through as a keyword argument:cache = nothing(the default) allocates fresh buffers per call — identical behavior to before, always thread-safe.ArgumentError(constructorsSutherlandHodgmanCache(alg, T)/SutherlandHodgmanCache(manifold, T)get this right automatically).Octagon-octagon benchmark: 928 bytes/call uncached → 448 bytes/call cached (just the returned polygon).
Test plan
ConvexConvexSutherlandHodgmantests pass unchangedCachetestset: cached vs uncached result equality (planar + spherical), aliasing regression (reusing a cache must not corrupt previously returned polygons), wrong-cache-typeArgumentError, and a post-warm-up allocation bound🤖 Generated with Claude Code