Skip to content

🐛 fix(parse): stop the SSE2 text scan at NUL - #2467

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/stream-null-error
Nov 18, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/stream-null-error

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

parse('a' * 16 + '\x00' + 'b' * 31).errors is empty on x86-64, while streaming the same string reports ('unexpected-null-character', 1, 16). The one-byte data scan's 32-byte SSE2 loop tested each block for & and < only, so a NUL inside a block was stepped over and the data state never reported it. The 16-byte loops and the NEON scan already stop at a zero byte, and a streamed parse reports the error because small chunks never reach the 32-byte loop.

html5lib-tests (tokenizer/test2.test, "Null Byte Replacement") expects the error for a NUL in the data state, so the one-shot side was wrong. The 32-byte loop now stops at a NUL as it does at & and <.

Tests cover a NUL in each scan block, the string a differential run found, and a seeded sample of 300 Latin-1 and mixed-width strings streamed at chunk sizes 1, 2, 3, 7 and 4096, asserting the stream reports exactly what parse() does. Eleven of them fail without the fix.

The one-byte data scan on SSE2 tested each 32-byte block for '&' and '<'
only, so a NUL inside such a block was stepped over. The data state never
saw it, and parse() dropped its unexpected-null-character error. The
16-byte loops and the NEON scan already stop at a zero byte, and a
streamed parse reported the error because a small chunk never reaches the
32-byte loop.

The smallest input that shows it is 48 bytes: 16 ordinary characters, a
NUL, then 31 more. html5lib-tests (tokenizer/test2.test, "Null Byte
Replacement") expects the error for a NUL in the data state, so the
streamed side was right.

Tests cover the NUL in each scan block, the string from the differential
run, and a seeded sample of 300 Latin-1 and mixed-width strings, streamed
at chunk sizes 1, 2, 3, 7 and 4096.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 18, 2026
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 2.83%

2 warnings

⚠️ 7 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 7 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
❌ 5 regressed benchmarks
✅ 573 untouched benchmarks
⏩ 91 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[canonicalize] 6 ms 6.5 ms -7.18%
❌ test_feature[encode-inner] 6.7 ms 7.2 ms -6.41%
❌ test_feature[encode-inner-indent] 7.1 ms 7.5 ms -6.2%
❌ test_feature[serialize-inner] 5.5 ms 5.8 ms -5.25%
❌ test_feature[serialize-inner-indent] 5.9 ms 6.2 ms -5.03%
⚡ test_feature[xpath-concat-nested] 5.4 ms 5.1 ms +6.2%
⚡ test_feature[transform-text-literal] 5.8 ms 5.6 ms +5.04%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/stream-null-error (0da8052) with main (01b4635)

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 453ec90 into tox-dev:main Nov 18, 2026
65 of 66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant