From a4d585eb374724b79676e9cceaa9e9a1a4358565 Mon Sep 17 00:00:00 2001
From: Guillaume Ayoub <guillaume@courtbouillon.org>
Date: Wed, 12 Aug 2026 09:18:02 +0200
Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20use=20O(n)=20list.pop(0)=20for?=
 =?UTF-8?q?=20vertices,=20use=20O(1)=20deque.popleft()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 cairosvg/path.py   | 7 ++++---
 cairosvg/shapes.py | 5 +++--
 2 files changed, 7 insertions(+), 5 deletions(-)

Index: CairoSVG-2.7.1/cairosvg/path.py
===================================================================
--- CairoSVG-2.7.1.orig/cairosvg/path.py
+++ CairoSVG-2.7.1/cairosvg/path.py
@@ -3,7 +3,9 @@ Paths manager.
 
 """
 
+from collections import deque
 from math import pi, radians
+from re import finditer
 
 from .bounding_box import calculate_bounding_box
 from .helpers import (
@@ -31,8 +33,8 @@ def draw_markers(surface, node):
 
     while node.vertices:
         # Calculate position and angle
-        point = node.vertices.pop(0)
-        angles = node.vertices.pop(0) if node.vertices else None
+        point = node.vertices.popleft()
+        angles = node.vertices.popleft() if node.vertices else None
         if angles:
             if position == 'start':
                 angle = pi - angles[0]
@@ -122,7 +124,7 @@ def path(surface, node):
     """Draw a path ``node``."""
     string = node.get('d', '')
 
-    node.vertices = []
+    node.vertices = deque()
 
     for letter in PATH_LETTERS:
         string = string.replace(letter, f' {letter} ')
@@ -138,17 +140,15 @@ def path(surface, node):
         surface.context.move_to(0, 0)
         current_point = 0, 0
 
-    while string:
+    matches = deque(match.groups() for match in finditer(
+        f'([{PATH_LETTERS}])([^{PATH_LETTERS}]*)', string.strip()))
+    while matches:
+        letter, string = matches.popleft()
         string = string.strip()
-        if string.split(' ', 1)[0] in PATH_LETTERS:
-            letter, string = (string + ' ').split(' ', 1)
-            if last_letter in (None, 'z', 'Z') and letter not in 'mM':
-                node.vertices.append(current_point)
-                first_path_point = current_point
-        elif letter == 'M':
-            letter = 'L'
-        elif letter == 'm':
-            letter = 'l'
+
+        if last_letter in (None, 'z', 'Z') and letter not in 'mM':
+            node.vertices.append(current_point)
+            first_path_point = current_point
 
         if last_letter in (None, 'm', 'M', 'z', 'Z'):
             first_path_point = None
@@ -186,14 +186,12 @@ def path(surface, node):
 
             # rx=0 or ry=0 means straight line
             if not rx or not ry:
-                if string and string[0] not in PATH_LETTERS:
+                if string := string.strip():
                     # As we replace the current operation by l, we must be sure
                     # that the next letter is set to the real current letter (a
                     # or A) in case it’s omitted
-                    next_letter = f'{letter} '
-                else:
-                    next_letter = ''
-                string = f'l {x3} {y3} {next_letter}{string}'
+                    matches.appendleft((letter, string))
+                matches.appendleft(('l', f'{x3} {y3}'))
                 continue
 
             radii_ratio = ry / rx
@@ -448,5 +446,7 @@ def path(surface, node):
         if letter not in 'zZ':
             node.vertices.append(current_point)
 
-        string = string.strip()
+        if string := string.strip():
+            next_letter = {'m': 'l', 'M': 'L'}.get(letter, letter)
+            matches.appendleft((next_letter, string))
         last_letter = letter
Index: CairoSVG-2.7.1/cairosvg/shapes.py
===================================================================
--- CairoSVG-2.7.1.orig/cairosvg/shapes.py
+++ CairoSVG-2.7.1/cairosvg/shapes.py
@@ -3,6 +3,7 @@ Shapes drawers.
 
 """
 
+from collections import deque
 from math import pi
 
 from .helpers import normalize, point, point_angle, size
@@ -45,7 +46,7 @@ def line(surface, node):
     surface.context.move_to(x1, y1)
     surface.context.line_to(x2, y2)
     angle = point_angle(x1, y1, x2, y2)
-    node.vertices = [(x1, y1), (pi - angle, angle), (x2, y2)]
+    node.vertices = deque([(x1, y1), (pi - angle, angle), (x2, y2)])
 
 
 def polygon(surface, node):
@@ -60,7 +61,7 @@ def polyline(surface, node):
     if points:
         x, y, points = point(surface, points)
         surface.context.move_to(x, y)
-        node.vertices = [(x, y)]
+        node.vertices = deque([(x, y)])
         while points:
             x_old, y_old = x, y
             x, y, points = point(surface, points)
