Skip to content

Commit a4d585e

Browse files
committed
Don’t use O(n) list.pop(0) for vertices, use O(1) deque.popleft()
1 parent 026fbce commit a4d585e

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

‎cairosvg/path.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
"""
55

6+
from collections import deque
67
from math import copysign, hypot, pi, radians
78

89
from .bounding_box import calculate_bounding_box
@@ -31,8 +32,8 @@ def draw_markers(surface, node):
3132

3233
while node.vertices:
3334
# Calculate position and angle
34-
point = node.vertices.pop(0)
35-
angles = node.vertices.pop(0) if node.vertices else None
35+
point = node.vertices.popleft()
36+
angles = node.vertices.popleft() if node.vertices else None
3637
if angles:
3738
if position == 'start':
3839
angle = pi - angles[0]
@@ -122,7 +123,7 @@ def path(surface, node):
122123
"""Draw a path ``node``."""
123124
string = node.get('d', '')
124125

125-
node.vertices = []
126+
node.vertices = deque()
126127

127128
for letter in PATH_LETTERS:
128129
string = string.replace(letter, f' {letter} ')

‎cairosvg/shapes.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
"""
55

6+
from collections import deque
67
from math import pi
78

89
from .helpers import normalize, point, point_angle, size
@@ -47,7 +48,7 @@ def line(surface, node):
4748
surface.context.move_to(x1, y1)
4849
surface.context.line_to(x2, y2)
4950
angle = point_angle(x1, y1, x2, y2)
50-
node.vertices = [(x1, y1), (pi - angle, angle), (x2, y2)]
51+
node.vertices = deque([(x1, y1), (pi - angle, angle), (x2, y2)])
5152

5253

5354
def polygon(surface, node):
@@ -62,7 +63,7 @@ def polyline(surface, node):
6263
if points:
6364
x, y, points = point(surface, points)
6465
surface.context.move_to(x, y)
65-
node.vertices = [(x, y)]
66+
node.vertices = deque([(x, y)])
6667
while points:
6768
x_old, y_old = x, y
6869
x, y, points = point(surface, points)

0 commit comments

Comments
 (0)