Skip to content

byte_record: validate non-ASCII records in one pass - #441

Open
patrick-immometrica wants to merge 1 commit into
BurntSushi:masterfrom
patrick-immometrica:perf-utf8-validate
Open

patrick-immometrica wants to merge 1 commit into
BurntSushi:masterfrom
patrick-immometrica:perf-utf8-validate

Conversation

@patrick-immometrica

Copy link
Copy Markdown

Once a record has any non-ASCII byte, validate falls back to str::from_utf8 per field. For data that's mostly non-ASCII (names, addresses in non-Latin scripts) that's a lot of short calls per record.

This checks the whole buffer once and then makes sure every field end lands on a char boundary. If both hold, each field is valid UTF-8 on its own. Otherwise it falls through to the existing per-field loop, so error positions don't change. The ASCII fast path stays first, since dropping it made ASCII input about 15% slower.

Reading a 15 MB non-ASCII file with read_record into a StringRecord:

MB/s
master ~500
this PR ~610-630

ASCII files (game, worldcitiespop) show no change. Added a test for a codepoint split across two fields, which is the case the boundary check is there for.

Related: #187, #410.

`validate` ran `str::from_utf8` once per field as soon as the record had
a single non-ASCII byte. Now the whole buffer is checked once and the
field ends are checked to be on char boundaries, which is enough for
every field to be valid on its own. The per-field loop is still used to
build the error.

Reading 15 MB of mostly non-ASCII data into `StringRecord` goes from
~500 MB/s to ~615 MB/s here. ASCII input is unaffected.
@patrick-immometrica
patrick-immometrica marked this pull request as ready for review November 14, 2026 11:11
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.

1 participant