Repository navigation
tokenize.generate_tokens() performance regression in 3.12 #119118
Copy link
Copy link
Closed
Labels
performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
There seems to be a significant performance regression in tokenize.generate_tokens() between 3.11 and 3.12 when tokenizing a (very) large dict on a single line. I searched the existing issues but couldn't find anything about this.
To reproduce, rename the file largedict.py.txt to largedict.py in the same directory as the script below, then run the script. That file comes from coveragepy/coveragepy#1785.
import io, time, sys, tokenize
import largedict
text = largedict.d
readline = io.StringIO(text).readline
glob_start = start = time.time()
print(f"{sys.implementation.name} {sys.platform} {sys.version}")
for i, (ttype, ttext, (sline, scol), (_, ecol), _) in enumerate(tokenize.generate_tokens(readline)):
if i % 500 == 0:
print(i, ttype, ttext, sline, scol, time.time() - start)
start = time.time()
if i % 5000 == 0:
print(time.time() - glob_start)
print(f"Time taken: {time.time() - glob_start}")For Python 3.12, this results in:
cpython linux 3.12.3 (main, May 17 2024, 07:19:22) [GCC 11.4.0]
0 1 a_large_dict_literal 1 0 0.04641866683959961
0.046633005142211914
500 3 ':tombol_a_(golongan_darah):' 1 2675 9.689745903015137
1000 3 ':flagge_anguilla:' 1 5261 9.767053604125977
1500 3 ':флаг_Армения:' 1 7879 9.258271932601929
[...]
For Python 3.11, this results in:
cpython linux 3.11.0rc1 (main, Aug 12 2022, 10:02:14) [GCC 11.2.0]
0 1 a_large_dict_literal 1 0 0.013637304306030273
0.013663768768310547
500 3 ':tombol_a_(golongan_darah):' 1 2675 0.002939462661743164
1000 3 ':flagge_anguilla:' 1 5261 0.0028715133666992188
1500 3 ':флаг_Армения:' 1 7879 0.002806425094604492
[...]
352500 3 'pt' 1 2589077 0.003370046615600586
Time taken: 2.1244866847991943
That is, each 500 tokens in Python 3.12 is taking over 9 seconds to process, while the 352500 tokens in Python 3.11 is taking a bit over 2 seconds to process.
I can reproduce this on Linux (WSL) and Windows. Also seems to affect 3.13.
CPython versions tested on:
3.9, 3.10, 3.11, 3.12
Operating systems tested on:
Linux, Windows
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
performancePerformance or resource usagePerformance or resource usagetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Activity
erlend-aasland commentedon May 17, 2024
Suspecting it could be related to the work done on PEP-701, which landed in 3.12.
pablogsal commentedon May 17, 2024
Yeah we are now using a completely different backend (the actual parser) so we need to investigate a bit what's going on here.
devdanzin commentedon May 17, 2024
A data point that might help: when calling
list(tokenize.generate_tokens(readline))(ortuple(), memory blows out very fast with a large single line dict, while the same dict broken into lines doesn't cause memory issues.pablogsal commentedon May 17, 2024
Oh wow that's very intriguing. I may be able to investigate this next week after PyCon but thanks for the hint!
pablogsal commentedon May 17, 2024
Ok, I think I have a guess (i need time to test my theory) but I am 99% sure I know the problem. The problem is this line:
cpython/Python/Python-tokenize.c
Line 213 in 31a28cb
We are creating unique strings per token with the entire line. If the dict spans the entire line this is a very expensive operation that we are doing over and over and over with the same result. The fix is to keep reusing the same result until there is a new line.
erlend-aasland commentedon May 17, 2024
Confirming that, Pablo; if I cache the line1 on the tokenizer iter object instance, I no longer see the performance penalty.Forget that, it's not that simple.Footnotes
the
PyObject *lineobject ↩lysnikolaou commentedon May 17, 2024
Well, yeah, it's not that simple. Doing some scrappy caching seems to have brought down the performance penalty, but it's much worse than 3.11.
So we get about 0.1-0.5 seconds per 500 tokens, instead of the original ~9 seconds, but it's still very far away.
lysnikolaou commentedon May 17, 2024
Oh found it, it's not just that line, but the calls to
_PyPegen_byte_offset_to_character_offsetthat are made a bit further below that eventually callPyUnicode_*APIs as well. After additionally caching those, I get:I'll push a PR shortly.
nedbat commentedon May 25, 2024
@lysnikolaou any news on this? We have other people reporting a problem like this in 3.12.
lysnikolaou commentedon May 25, 2024
Thanks for the reminder @nedbat! Turns out that this was even trickier than I though and the fix I showed in #119118 (comment) was buggy. My patch is almost there, but I didn't get the time to fix some final details. I'll have a PR up by early next week.
pablogsal commentedon May 25, 2024
Tell me if you want me to help or pick it up if you done have time @lysnikolaou
pythongh-119118: Fix performance regression in tokenize module
gh-119118: Fix performance regression in tokenize module (#119615)
pythongh-119118: Fix performance regression in tokenize module (pytho…
pythongh-119118: Fix performance regression in tokenize module (pytho…
[3.13] gh-119118: Fix performance regression in tokenize module (GH-1…
[3.12] gh-119118: Fix performance regression in tokenize module (GH-1…
lysnikolaou commentedon May 28, 2024
Fixed in #119615.
pythongh-119118: Fix performance regression in tokenize module (pytho…
pythongh-119118: Fix performance regression in tokenize module (pytho…