Skip to content

Fix encodeInto() stopping short under ENABLE_FAST_TEXTENCODER - #8224

Merged
nathanwhit merged 1 commit into
mainfrom
nathanwhit/text-encoder-stop-short
Nov 5, 2026
Merged

nathanwhit merged 1 commit into
mainfrom
nathanwhit/text-encoder-stop-short

Conversation

@nathanwhit

@nathanwhit nathanwhit commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

findBestFit(), which decides how much of the input encodeInto() converts under the ENABLE_FAST_TEXTENCODER autogate, could stop before code points that fit, for two reasons:

  • It measures the input in chunks with simdutf and adds their lengths. A chunk could end between the halves of a surrogate pair, and measured apart, each half counts as U+FFFD: six bytes instead of four. '\u{1F639}'.repeat(1000) into 1001 bytes read 498 code units, not 500. A chunk no longer ends on a lead surrogate.
  • simpleUtfEncodingLength(), which measures the last few code units, counted U+0400 to U+07FF (Cyrillic, Hebrew, Arabic, ...) as three bytes instead of two. 'ж'.repeat(10) into 2 bytes read nothing, so a caller encoding into a 2-byte buffer in a loop made no progress.

The result was always valid UTF-8, just shorter than the Encoding Standard requires. The new bestFit tests check long runs of pairs and of two-byte characters at every buffer size, and
encodeIntoFillsTheDestination checks encodeInto() itself.

Compatibility: no compatibility flag. The change affects only the autogated path, and makes it return what the default path and the standard do.

I've confirmed the autogate is not enabled, so users can't be relying on this buggy behavior yet.

@nathanwhit
nathanwhit requested review from a team as code owners November 4, 2026 18:11

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 2f854ce5.

This review, of e55a3783

✅ Approved — This is a small, correct fix to the autogated encodeInto() fit calculation, and the tests cover it well. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The surrogate-boundary and two-byte-range fixes in findBestFit() are correct, and the tests are thorough.


📝 Documentation: ✅ LGTM

The updated comments describe the code as it is now.


Reviewed commit: e55a3783

@nathanwhit
nathanwhit force-pushed the nathanwhit/text-encoder-stop-short branch from e55a378 to 2f854ce Compare November 4, 2026 20:58

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of 3dfda325.

This review, of 2f854ce5

✅ Approved — This is a small, correct fix to the autogated encodeInto() fit calculation, and the tests cover it well. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The surrogate-boundary and two-byte-range fixes in findBestFit() are correct, and the tests are thorough.


📝 Documentation: ✅ LGTM

The updated comments describe the code as it is now.


Reviewed commit: 2f854ce5 — same changes as e55a3783, so this is that commit's review (no new review was run; line numbers are as of e55a3783)

@nathanwhit
nathanwhit enabled auto-merge November 4, 2026 21:52
findBestFit(), which decides how much of the input encodeInto() converts
under the ENABLE_FAST_TEXTENCODER autogate, could stop before code
points that fit, for two reasons:

- It measures the input in chunks with simdutf and adds their lengths.
  A chunk could end between the halves of a surrogate pair, and
  measured apart, each half counts as U+FFFD: six bytes instead of four.
  '\u{1F639}'.repeat(1000) into 1001 bytes read 498 code units, not 500.
  A chunk no longer ends on a lead surrogate.
- simpleUtfEncodingLength(), which measures the last few code units,
  counted U+0400 to U+07FF (Cyrillic, Hebrew, Arabic, ...) as three
  bytes instead of two. 'ж'.repeat(10) into 2 bytes read nothing, so
  a caller encoding into a 2-byte buffer in a loop made no progress.

The result was always valid UTF-8, just shorter than the Encoding
Standard requires. The new bestFit tests check long runs of pairs and
of two-byte characters at every buffer size, and
encodeIntoFillsTheDestination checks encodeInto() itself.

Compatibility: no compatibility flag. The change affects only the
autogated path, and makes it return what the default path and the
standard do.
@nathanwhit
nathanwhit force-pushed the nathanwhit/text-encoder-stop-short branch from 2f854ce to 3dfda32 Compare November 5, 2026 03:01

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — This is a small, correct fix to the autogated encodeInto() fit calculation, and the tests cover it well. No issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

📋 Code Quality: ✅ LGTM

The surrogate-boundary and two-byte-range fixes in findBestFit() are correct, and the tests are thorough.


📝 Documentation: ✅ LGTM

The updated comments describe the code as it is now.


Reviewed commit: 3dfda325 — same changes as e55a3783, so this is that commit's review (no new review was run; line numbers are as of e55a3783)

@nathanwhit
nathanwhit disabled auto-merge November 5, 2026 04:25
@nathanwhit
nathanwhit merged commit 38f06eb into main Nov 5, 2026
23 of 26 checks passed
@nathanwhit
nathanwhit deleted the nathanwhit/text-encoder-stop-short branch November 5, 2026 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants