8000 Speed up Path.iter_segments() · matplotlib/matplotlib@dcb5fbb · GitHub
[go: up one dir, main page]

Skip to content

Commit dcb5fbb

Browse files
committed
Speed up Path.iter_segments()
Using the Gtk3Cairo backend with wire3d_animation_sgskip.py: Before: 9.16 fps After: 11.36 fps The main speedup is from iterating and keeping the common non-curve case simple and from converting the codes to int before comparing/hashing them as using uint8 there is much slower for some reason.
1 parent cdc3ab1 commit dcb5fbb

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

lib/matplotlib/path.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,24 +399,26 @@ def iter_segments(self, transform=None, remove_nans=True, clip=None,
399399
snap=snap, stroke_width=stroke_width,
400400
simplify=simplify, curves=curves,
401401
sketch=sketch)
402-
vertices = cleaned.vertices
403-
codes = cleaned.codes
404-
len_vertices = vertices.shape[0]
405402

406403
# Cache these object lookups for performance in the loop.
407404
NUM_VERTICES_FOR_CODE = self.NUM_VERTICES_FOR_CODE
408405
STOP = self.STOP
409406

410-
i = 0
411 E540 -
while i < len_vertices:
412-
code = codes[i]
413-
if code == STOP:
414-
return
407+
vertices = iter(cleaned.vertices)
408+
codes = iter(cleaned.codes)
409+
for curr_vertices, code in zip(vertices, codes):
410+
# int is a lot faster than uint8 when comparing/hashing
411+
code_int = int(code)
412+
if code_int == STOP:
413+
break
414+
extra_vertices = NUM_VERTICES_FOR_CODE[code_int] - 1
415+
if extra_vertices:
416+
for i in range(extra_vertices):
417+
next(codes)
418+
curr_vertices = np.append(curr_vertices, next(vertices))
419+
yield curr_vertices, code
415420
else:
416-
num_vertices = NUM_VERTICES_FOR_CODE[code]
417-
curr_vertices = vertices[i:i+num_vertices].flatten()
418421
yield curr_vertices, code
419-
i += num_vertices
420422

421423
def cleaned(self, transform=None, remove_nans=False, clip=None,
422424
quantize=False, simplify=False, curves=False,

0 commit comments

Comments
 (0)
0