Skip to content

Speed up Path.iter_segments() #13039

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

Merged
merged 2 commits into from
Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2018-12-23-path-code-types.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Path code types like ``Path.MOVETO`` are now ``np.uint8`` instead of ``int``
````````````````````````````````````````````````````````````````````````````

``Path.STOP``, ``Path.MOVETO``, ``Path.LINETO``, ``Path.CURVE3``,
``Path.CURVE4`` and ``Path.CLOSEPOLY`` are now of the type ``Path.code_type``
(``np.uint8`` by default) instead of plain ``int``. This makes their type
match the array value type of the ``Path.codes`` array.
38 changes: 18 additions & 20 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ class Path(object):

"""

code_type = np.uint8

# Path codes
STOP = 0 # 1 vertex
MOVETO = 1 # 1 vertex
LINETO = 2 # 1 vertex
CURVE3 = 3 # 2 vertices
CURVE4 = 4 # 3 vertices
CLOSEPOLY = 79 # 1 vertex
STOP = code_type(0) # 1 vertex
MOVETO = code_type(1) # 1 vertex
LINETO = code_type(2) # 1 vertex
CURVE3 = code_type(3) # 2 vertices
CURVE4 = code_type(4) # 3 vertices
CLOSEPOLY = code_type(79) # 1 vertex

#: A dictionary mapping Path codes to the number of vertices that the
#: code expects.
Expand All @@ -91,8 +93,6 @@ class Path(object):
CURVE4: 3,
CLOSEPOLY: 1}

code_type = np.uint8

def __init__(self, vertices, codes=None, _interpolation_steps=1,
closed=False, readonly=False):
"""
Expand Down Expand Up @@ -399,24 +399,22 @@ def iter_segments(self, transform=None, remove_nans=True, clip=None,
snap=snap, stroke_width=stroke_width,
simplify=simplify, curves=curves,
sketch=sketch)
vertices = cleaned.vertices
codes = cleaned.codes
len_vertices = vertices.shape[0]

# Cache these object lookups for performance in the loop.
NUM_VERTICES_FOR_CODE = self.NUM_VERTICES_FOR_CODE
STOP = self.STOP

i = 0
while i < len_vertices:
code = codes[i]
vertices = iter(cleaned.vertices)
codes = iter(cleaned.codes)
for curr_vertices, code in zip(vertices, codes):
if code == STOP:
return
else:
num_vertices = NUM_VERTICES_FOR_CODE[code]
curr_vertices = vertices[i:i+num_vertices].flatten()
yield curr_vertices, code
i += num_vertices
break
extra_vertices = NUM_VERTICES_FOR_CODE[code] - 1
if extra_vertices:
for i in range(extra_vertices):
next(codes)
curr_vertices = np.append(curr_vertices, next(vertices))
yield curr_vertices, code

def cleaned(self, transform=None, remove_nans=False, clip=None,
quantize=False, simplify=False, curves=False,
Expand Down