PERF: Speed up add_patch#32026
Open
scottshambaugh wants to merge 3 commits into
Open
Conversation
b3f86e3 to
d45c072
Compare
d45c072 to
93c76f8
Compare
QuLogic
reviewed
Jul 9, 2026
| vertices = np.vstack(vertices) | ||
| if p.codes is None: | ||
| vertices = p.vertices | ||
| elif not ((p.codes == p.CURVE3) | (p.codes == p.CURVE4)).any(): |
Member
There was a problem hiding this comment.
Would this be faster with np.isin?
Contributor
Author
There was a problem hiding this comment.
I had tried that out, and it was ~20x slower than this direct comparison (for two comparisons, I imagine it probably scales better if you add more).
Contributor
Author
There was a problem hiding this comment.
Actually, let me roll that into Path.get_extents as well
scottshambaugh
commented
Jul 9, 2026
| xys = self.vertices | ||
| elif len(np.intersect1d(self.codes, [Path.CURVE3, Path.CURVE4])) == 0: | ||
| elif not ((self.codes == Path.CURVE3) | ||
| | (self.codes == Path.CURVE4)).any(): |
Contributor
Author
There was a problem hiding this comment.
This is ~40x faster than the np.intersect1d method, and path.codes is always 1d anyways.
scottshambaugh
commented
Jul 10, 2026
| return Bbox([xys.min(axis=0), xys.max(axis=0)]) | ||
| x = xys[:, 0] | ||
| y = xys[:, 1] | ||
| return Bbox([[x.min(), y.min()], [x.max(), y.max()]]) |
Contributor
Author
There was a problem hiding this comment.
This ended up being a hot path after the other changes, and is ~20x faster for a n=100000 point line. Surprised the numpy reduction is so slow tbh.
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.
PR summary
add_patchis currently very slow since it does bezier checks on all lines, but we can fast-path this for the common straight line case. In the below test script,add_patchdropped from 139 seconds to 50 milliseconds (~2700x improvement).AI Disclosure
Manually identified, claude suggested edits, manually reviewed / edited.
PR checklist