GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-119118: Fix performance regression in tokenize module by lysnikolaou · Pull Request #119615 · python/cpython

Conversation

@lysnikolaou

@lysnikolaou lysnikolaou commented May 27, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Cache line object to avoid creating a Unicode object for all of the tokens in the same line.
  • Speed up byte offset to column offset conversion by using the smallest buffer possible to measure the difference.
  • Issue: tokenize.generate_tokens() performance regression in 3.12 #119118

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@lysnikolaou
pythongh-119118: Fix performance regression in tokenize module
66f9385
- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.
@bedevere-app bedevere-app Bot added the awaiting core review label May 27, 2024
@bedevere-app bedevere-app Bot mentioned this pull request May 27, 2024
tokenize.generate_tokens() performance regression in 3.12 #119118
Closed
@devdanzin devdanzin mentioned this pull request May 28, 2024
tokenize in 3.12 makes copies of each line, 3.11 does not #119654
Closed
@lysnikolaou
lysnikolaou requested a review from pablogsal May 28, 2024 12:12
@blurb-it
📜🤖 Added by blurb_it.
143a420
pablogsal
pablogsal reviewed May 28, 2024
View reviewed changes
Comment thread Python/Python-tokenize.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@lysnikolaou
Update Python/Python-tokenize.c
8318ab8
pablogsal
pablogsal reviewed May 28, 2024
View reviewed changes
Comment thread Python/Python-tokenize.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

pablogsal
pablogsal reviewed May 28, 2024
View reviewed changes
Comment thread Python/Python-tokenize.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented May 28, 2024

Copy link
Copy Markdown
Member

Hummm, seems also that this solution fails with test_tokenize with -uall:

======================================================================
ERROR: test_random_files (test.test_tokenize.TestRoundtrip.test_random_files) (file='/Users/pgalindo3/github/python/main/Lib/test/test_difflib.py')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1959, in test_random_files
    self.check_roundtrip(f)
    ~~~~~~~~~~~~~~~~~~~~^^^
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1827, in check_roundtrip
    tokens2_from5 = [tok[:2] for tok in tokenize.tokenize(readline5)]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 484, in tokenize
    yield from _generate_tokens_from_c_tokenizer(rl_gen.__next__, encoding, extra_tokens=True)
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 578, in _generate_tokens_from_c_tokenizer
    raise e from None
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 574, in _generate_tokens_from_c_tokenizer
    for info in it:
        yield TokenInfo._make(info)
  File "<string>", line 467
    dateb = '3 fév'
                   ^
IndentationError: unindent does not match any outer indentation level

======================================================================
ERROR: test_random_files (test.test_tokenize.TestRoundtrip.test_random_files) (file='/Users/pgalindo3/github/python/main/Lib/test/test_html.py')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1959, in test_random_files
    self.check_roundtrip(f)
    ~~~~~~~~~~~~~~~~~~~~^^^
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1827, in check_roundtrip
    tokens2_from5 = [tok[:2] for tok in tokenize.tokenize(readline5)]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 484, in tokenize
    yield from _generate_tokens_from_c_tokenizer(rl_gen.__next__, encoding, extra_tokens=True)
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 578, in _generate_tokens_from_c_tokenizer
    raise e from None
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 574, in _generate_tokens_from_c_tokenizer
    for info in it:
        yield TokenInfo._make(info)
  File "<string>", line 85
    check('&notin;', '∉')
                         ^
IndentationError: unindent does not match any outer indentation level

======================================================================
ERROR: test_random_files (test.test_tokenize.TestRoundtrip.test_random_files) (file='/Users/pgalindo3/github/python/main/Lib/test/test_str.py')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1959, in test_random_files
    self.check_roundtrip(f)
    ~~~~~~~~~~~~~~~~~~~~^^^
  File "/Users/pgalindo3/github/python/main/Lib/test/test_tokenize.py", line 1827, in check_roundtrip
    tokens2_from5 = [tok[:2] for tok in tokenize.tokenize(readline5)]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 484, in tokenize
    yield from _generate_tokens_from_c_tokenizer(rl_gen.__next__, encoding, extra_tokens=True)
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 578, in _generate_tokens_from_c_tokenizer
    raise e from None
  File "/Users/pgalindo3/github/python/main/Lib/tokenize.py", line 574, in _generate_tokens_from_c_tokenizer
    for info in it:
        yield TokenInfo._make(info)
  File "<string>", line 1008
    try:
        ^
IndentationError: unindent does not match any outer indentation level

----------------------------------------------------------------------
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lysnikolaou
WIP: strcmp lines (need to fix) and use line buffer for end col offset
566adc3
@pablogsal

pablogsal commented May 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Another idea: we do have the token already as unicode (str) so if the line has not changed we can keep adding the size of the token itself to our state and take into account the number of whitespace characters between the tokens (but this is always ASCII so we can basically just calculate as the diff of two pointers).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal

pablogsal commented May 28, 2024

Copy link
Copy Markdown
Member

I'm discussing another fix with @lysnikolaou offline

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lysnikolaou and others added 2 commits May 28, 2024 16:19
@lysnikolaou @pablogsal
Get rid of strcmp
858eff1
Co-authored-by: Pablo Galindo <[email protected]>
@lysnikolaou
Make NEWS linter happy
ab34370
@pablogsal

pablogsal commented May 28, 2024

Copy link
Copy Markdown
Member

The docs failure seems unrelated. @hugovk do we know what may be happening here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@lysnikolaou

lysnikolaou commented May 28, 2024

Copy link
Copy Markdown
Member Author

The latest results are:

cpython on  performance-tokenize [$] via C v15.0.0-clang via 🐍 v3.11.3 
❯ python tmp/t.py
cpython darwin 3.11.3 (main, May  8 2023, 13:16:43) [Clang 14.0.3 (clang-1403.0.22.14.1)]
Time taken: 0.5428769588470459

cpython on  performance-tokenize [$] via C v15.0.0-clang via 🐍 v3.11.3 
❯ ./python.exe tmp/t.py
cpython darwin 3.14.0a0 (heads/performance-tokenize-dirty:ab3437096a7, May 28 2024, 19:26:19) [Clang 15.0.0 (clang-1500.3.9.4)]
Time taken: 0.4570140838623047

The test failures appear to be unrelated. We can probably merge this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hugovk

hugovk commented May 28, 2024

Copy link
Copy Markdown
Member

The docs failure seems unrelated. @hugovk do we know what may be happening here?

Yep, need to merge in latest main, the new option has been added there (re: #119221).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal
pablogsal approved these changes May 28, 2024
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 28, 2024
@pablogsal pablogsal added needs backport to 3.12 only security fixes needs backport to 3.13 only security fixes labels May 28, 2024
pablogsal
pablogsal approved these changes May 28, 2024
View reviewed changes
@pablogsal
Merge branch 'main' into performance-tokenize
b97c51e
@pablogsal
pablogsal enabled auto-merge (squash) May 28, 2024 18:19
@pablogsal
pablogsal merged commit d87b015 into python:main May 28, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label May 28, 2024
@miss-islington-app

miss-islington-app Bot commented May 28, 2024

Copy link
Copy Markdown

Thanks @lysnikolaou for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 28, 2024
@lysnikolaou @pablogsal @miss-islington
pythongh-119118: Fix performance regression in tokenize module (pytho…
64be1b7
…nGH-119615)

* pythongh-119118: Fix performance regression in tokenize module

- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.

(cherry picked from commit d87b015)

Co-authored-by: Lysandros Nikolaou <[email protected]>
Co-authored-by: Pablo Galindo <[email protected]>
@bedevere-app

bedevere-app Bot commented May 28, 2024

Copy link
Copy Markdown

GH-119682 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label May 28, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 28, 2024
@lysnikolaou @pablogsal @miss-islington
pythongh-119118: Fix performance regression in tokenize module (pytho…
8e935c9
…nGH-119615)

* pythongh-119118: Fix performance regression in tokenize module

- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.

(cherry picked from commit d87b015)

Co-authored-by: Lysandros Nikolaou <[email protected]>
Co-authored-by: Pablo Galindo <[email protected]>
@bedevere-app

bedevere-app Bot commented May 28, 2024

Copy link
Copy Markdown

GH-119683 is a backport of this pull request to the 3.12 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.12 only security fixes label May 28, 2024
lysnikolaou added a commit that referenced this pull request May 28, 2024
@miss-islington @lysnikolaou @pablogsal
[3.13] gh-119118: Fix performance regression in tokenize module (GH-1…
0d0be6b
…19615) (#119682)

- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.

(cherry picked from commit d87b015)

Co-authored-by: Lysandros Nikolaou <[email protected]>
Co-authored-by: Pablo Galindo <[email protected]>
lysnikolaou added a commit that referenced this pull request May 28, 2024
@miss-islington @lysnikolaou @pablogsal
[3.12] gh-119118: Fix performance regression in tokenize module (GH-1…
4a0af0c
…19615) (#119683)

- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.

(cherry picked from commit d87b015)

Co-authored-by: Lysandros Nikolaou <[email protected]>
Co-authored-by: Pablo Galindo <[email protected]>
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
@lysnikolaou @pablogsal @estyxx
pythongh-119118: Fix performance regression in tokenize module (pytho…
2b2bd11
…n#119615)

* pythongh-119118: Fix performance regression in tokenize module

- Cache line object to avoid creating a Unicode object
  for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
  smallest buffer possible to measure the difference.

Co-authored-by: Pablo Galindo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@devdanzin devdanzin devdanzin left review comments
@pablogsal pablogsal pablogsal approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@lysnikolaou @pablogsal @hugovk @devdanzin
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.