Skip to content

gh-163144: Speed up UTF-8 decoding for single characters and multibyte runs - #163145

Open
methane wants to merge 1 commit into
python:mainfrom
methane:optimize-utf8-decode
Open

methane wants to merge 1 commit into
python:mainfrom
methane:optimize-utf8-decode

Conversation

@methane

@methane methane commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Closes #163144.

Decode short non-ASCII inputs into a stack buffer so the result can be allocated at its final width. Return single characters directly. For longer inputs, skip the known ASCII prefix when counting code points and decode runs of two- or three-byte UTF-8 characters in groups.

Measured bytes.decode() speedup relative to the PR base (f31608088bb). Values are the median of 12 paired runs on one x86-64 core with GCC 14.2, -O3 -fno-tree-vectorize, and PGO and LTO disabled.

UTF-8 input Default Free-threaded
One Japanese character (3 bytes) 1.27× 1.08×
Accented Latin letters (32 KiB) 4.56× 4.25×
Japanese prose (3,968 characters) 1.95× 1.63×
JSON lines with Japanese text (178 KiB) 1.80× 1.56×
ASCII (16 KiB) 1.03× 0.99×
Repeating 日a🙂éΩb (16,384 characters) 0.67× 0.66×

The alternating-width input takes about 50% longer in both builds.

Strict UTF-8 decoding raises UnicodeDecodeError directly even when an application has registered a replacement handler named strict. For mutable inputs, the sizing pass assumes the buffer stays unchanged until decoding finishes.

Decode short UTF-8 strings directly and use code point counts and
character widths to size longer results.
@methane methane added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 4, 2026 — with ChatGPT Codex Connector
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @methane for commit f38f5a5 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F163145%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Dec 4, 2026
@methane
methane requested review from cmaloney and vstinner December 4, 2026 14:25
@StanFromIreland

Copy link
Copy Markdown
Member

I'm worried the 0.67x case is not rare. The new code tries the fast grouped path on every 2- and 3-byte character, and when the neighbours are a different width that attempt fails and is wasted work, which seems like something that happens all the time in real text (e.g. Vietnamese, Latin text with accents, short Cyrillic or Greek words). The benchmarks mostly show long runs of same-width characters, and they were built with -fno-tree-vectorize and without PGO or LTO, so they don't tell us how a normal build behaves on normal text.

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.

Optimize UTF-8 decode

3 participants