Skip to content

Speed up UTF-8 validation in tokenizer and ASCII scanning in UTF-8 decoding #161145

Open

Description

@cmaloney

Feature or enhancement

  • The tokenizer checks source is utf-8 with _PyTokenizer_ensure_utf8 one character at a time. Reuse _Py_find_first_nonascii to scan faster. That makes compile() for Lib/typing.py about 1% faster.
  • Modify _Py_find_first_nonascii to test 64 bytes (32 on 32-bit platforms) of ASCII per branch. The code gets vectorized by some compilers and uses no intrinsics (https://godbolt.org/z/8bM83vx3a). (b'a' * 4096).decode() gets aout 1.5x faster.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. self-assigned this
    on Nov 1, 2026
  2. picnixz commented on Nov 1, 2026

    @picnixz
    Member

    We have a similar feature for stringlib with find_max_char. Possibility to reuse it maybe?

  3. cmaloney commented on Nov 1, 2026

    @cmaloney
    ContributorAuthor

    I looked at that but the unicodeobject.c code and stringlib/codecs.h assume copying the whole value to an output buffer in their API currently and would take a lot bigger refactor to deduplicate than I want to do currently.

  4. added 3 commits that reference this issue on Nov 1, 2026
  5. added a commit that references this issue on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions