-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Make Collection.set_paths even faster #16793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1070,6 +1070,10 @@ def __init__(self, verts, sizes=None, closed=True, **kwargs): | |
self.set_verts(verts, closed) | ||
self.stale = True | ||
|
||
# This function creates a lot of Path object, which is pretty much | ||
# guaranteed to trigger the GC multiple times. None of them will be garbage | ||
# just yet, so all those runs are completely unnecessary. | ||
@cbook._without_gc | ||
def set_verts(self, verts, closed=True): | ||
""" | ||
Set the vertices of the polygons. | ||
|
@@ -1094,15 +1098,19 @@ def set_verts(self, verts, closed=True): | |
return | ||
|
||
# Fast path for arrays | ||
if isinstance(verts, np.ndarray): | ||
verts_pad = np.concatenate((verts, verts[:, :1]), axis=1) | ||
if isinstance(verts, np.ndarray) and len(verts): | ||
verts_pad = (np.concatenate((verts, verts[:, :1]), axis=1) | ||
.astype(mpath.Path.vert_type)) | ||
# Creating the codes once is much faster than having Path do it | ||
# separately each time by passing closed=True. | ||
codes = np.empty(verts_pad.shape[1], dtype=mpath.Path.code_type) | ||
codes[:] = mpath.Path.LINETO | ||
codes[0] = mpath.Path.MOVETO | ||
codes[-1] = mpath.Path.CLOSEPOLY | ||
self._paths = [mpath.Path(xy, codes) for xy in verts_pad] | ||
example_path = mpath.Path(verts_pad[0], closed=True) | ||
# Looking up the values once speeds up the iteration a bit | ||
_make_path = mpath.Path._fast_from_codes_and_verts | ||
codes = example_path.codes | ||
self._paths = [_make_path(xy, codes, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out that the Python function call overheads become quite significant here, and so inlining the body of |
||
internals_from=example_path, | ||
unmask_verts=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
for xy in verts_pad] | ||
return | ||
|
||
self._paths = [] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,8 +74,12 @@ class Path: | |
made up front in the constructor that will not change when the | ||
data changes. | ||
""" | ||
__slots__ = ('_vertices', '_codes', '_readonly', | ||
'_should_simplify', '_simplify_threshold', | ||
'_interpolation_steps', '__weakref__') | ||
|
||
code_type = np.uint8 | ||
vert_type = float | ||
|
||
# Path codes | ||
STOP = code_type(0) # 1 vertex | ||
|
@@ -175,7 +179,7 @@ def _fast_from_codes_and_verts(cls, verts, codes, internals_from=None): | |
never copied, and always set to ``False`` by this constructor. | ||
""" | ||
pth = cls.__new__(cls) | ||
pth._vertices = _to_unmasked_float_array(verts) | ||
pth._vertices = verts | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a note to the docstring that |
||
pth._codes = codes | ||
pth._readonly = False | ||
if internals_from is not None: | ||
|
@@ -269,16 +273,6 @@ def readonly(self): | |
""" | ||
return self._readonly | ||
|
||
def __copy__(self): | ||
""" | ||
Returns a shallow copy of the `Path`, which will share the | ||
vertices and codes with the source `Path`. | ||
""" | ||
import copy | ||
return copy.copy(self) | ||
|
||
copy = __copy__ | ||
|
||
def __deepcopy__(self, memo=None): | ||
""" | ||
Returns a deepcopy of the `Path`. The `Path` will not be | ||
|
Uh oh!
There was an error while loading. Please reload this page.