Skip to content

Commit 5a6489b

Browse files
committed
Avoid constructing temporary strings in type1font parsing.
Just avoiding to construct a bunch of temporary strings (by instead asking regexes to start their search at a given position) speeds up ``` python -c 'from pylab import *; mpl.use("pdf"); rcParams["text.usetex"] = True; plot(); [savefig("/tmp/test.pdf", backend="pdf") for _ in range(100)]' ``` by ~5%.
1 parent cc8669f commit 5a6489b

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

lib/matplotlib/type1font.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,20 @@ def _tokens(cls, text):
148148
"""
149149
pos = 0
150150
while pos < len(text):
151-
match = (cls._comment_re.match(text[pos:]) or
152-
cls._whitespace_re.match(text[pos:]))
151+
match = (cls._comment_re.match(text, pos) or
152+
cls._whitespace_re.match(text, pos))
153153
if match:
154154
yield (_TokenType.whitespace, match.group())
155-
pos += match.end()
155+
pos = match.end()
156156
elif text[pos] == b'(':
157157
start = pos
158158
pos += 1
159159
depth = 1
160160
while depth:
161-
match = cls._instring_re.search(text[pos:])
161+
match = cls._instring_re.search(text, pos)
162162
if match is None:
163163
return
164-
pos += match.end()
164+
pos = match.end()
165165
if match.group() == b'(':
166166
depth += 1
167167
elif match.group() == b')':
@@ -174,17 +174,17 @@ def _tokens(cls, text):
174174
pos += 2
175175
elif text[pos] == b'<':
176176
start = pos
177-
pos += text[pos:].index(b'>')
177+
pos = text.index(b'>', pos)
178178
yield (_TokenType.string, text[start:pos])
179179
else:
180-
match = cls._token_re.match(text[pos:])
180+
match = cls._token_re.match(text, pos)
181181
if match:
182182
try:
183183
float(match.group())
184184
yield (_TokenType.number, match.group())
185185
except ValueError:
186186
yield (_TokenType.name, match.group())
187-
pos += match.end()
187+
pos = match.end()
188188
else:
189189
yield (_TokenType.delimiter, text[pos:pos + 1])
190190
pos += 1

0 commit comments

Comments
 (0)