Skip to content

gh-153568: Skip newline translation for source without carriage returns#153584

Open
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-newlines
Open

gh-153568: Skip newline translation for source without carriage returns#153584
pablogsal wants to merge 1 commit into
python:mainfrom
pablogsal:gh-153568-newlines

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026

Copy link
Copy Markdown
Member

The tokenizer normalizes newlines by copying the whole input one byte at a time through a state machine, but virtually all source today has no carriage returns at all, in which case the translation is the identity. A single memchr decides that up front and replaces the byte loop with one memcpy; inputs containing carriage returns keep the existing path, including lone-CR and CRLF handling.

Benchmark (parsing 8 of the largest stdlib files, 1.3 MB, 20 times per run with _PyParser_ASTFromString — parser only, no AST-to-Python conversion; turbo disabled, performance governor, FIFO-pinned to a dedicated core, interleaved runs):

build time per run executed instructions
main 143.8 ms 23.16G
this PR 142.5 ms 22.74G (1.8% fewer)

About a 1% wall-clock win: small, but stable across runs and the diff is eight lines.

… returns

The tokenizer copied every input byte by byte to normalize newlines,
but source without a carriage return needs no translation and can be
copied verbatim.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant