PERF: Speed up add_patch#32026
Conversation
d45c072 to
93c76f8
Compare
| vertices = np.vstack(vertices) | ||
| if p.codes is None: | ||
| vertices = p.vertices | ||
| elif not ((p.codes == p.CURVE3) | (p.codes == p.CURVE4)).any(): |
There was a problem hiding this comment.
Would this be faster with np.isin?
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).
There was a problem hiding this comment.
Actually, let me roll that into Path.get_extents as well
| 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(): |
There was a problem hiding this comment.
This is ~40x faster than the np.intersect1d method, and path.codes is always 1d anyways.
| 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()]]) |
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.
|
Consolidated the shared behavior into a new Path method with tests. |
|
It would be nice if the commit messages mentioned what kind of speedup was achieved, instead of in GitHub comments. |
Speed up path.get_extents comparison as well Faster get_extents
7defac6 to
06d1ae0
Compare
|
Sure thing @QuLogic, updated the commit message |
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