Skip to content

Improving scaling for tokenize.tokenize and tokenize.generate_tokens on FT build #163220

Backlog

Description

@johng

Currently tokenize.generate_tokens does not scale in free threaded builds.

With ftscalingbench we have the following results from

ftscalingbench.py -t 4 tokenize_source => 1.1–1.2x slower
TOKENIZE_SOURCE = '''\
def f(a, b=1, *args, c: int = 2, **kwargs):
    """Docstring."""
    x = [i * 2 for i in range(10) if i % 3]
    return {"a": a, "b": b, "x": x}  # comment
'''

@register_benchmark
def tokenize_source():
    for _ in range(10 * WORK_SCALE):
        for _ in tokenize.generate_tokens(io.StringIO(TOKENIZE_SOURCE).readline):
            pass

This seems to stem from constructing TokenInfo with TokenInfo._make which results in updating various shared reference counts. Directly constructing via tuple.__new__(TokenInfo, info) given TokenInfo is a subclass of a tuple results much better scaling and even faster for GIL builds too. Another option is to optimise the _make itself.

Improving the scaling would enable various introspection such as linters to better utilise threads on FT build.

Linked PRs

Activity

  1. changed the title [-]Improving scaling for `an` on FT build[/-] [+]Improving scaling for `tokenize.tokenize` and `tokenize.generate_tokens` on FT build[/+] on Dec 5, 2026
  2. added
    performance
    stdlib
    type-feature
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancestdlibtopic-free-threadingtopic-parsertype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions