Skip to content

Improve speed of Json escaping - #1363

Open
tkaitchuck wants to merge 2 commits into
serde-rs:masterfrom
tkaitchuck:master
Open

tkaitchuck wants to merge 2 commits into
serde-rs:masterfrom
tkaitchuck:master

Conversation

@tkaitchuck

Copy link
Copy Markdown

Faster json escaping

When writing strings into json, it preforms a byte by byte scan to determine if characters need to be escaped.
This accelerates that process by using a bit twiddling implementation of the scan.

The code is written to operate on u32s (scanning 4 bytes at a time) but does 4 iterations at a time.
This is done deliberately to induce the compiler to optimize the loop into SIMD statements, without depending on any arch specific, unsafe, or non-portable code.

At -O2/-O3 this correctly vectorizes into SIMD instructions on i686, x64, and ARM64.
Even on platforms without SIMD instructions it will still operate 4 bytes at a time instead of 1, so there will be a minor perf gain.

Benchmark results

DOM struct
canada −5.9% -2.1%
citm_catalog −5.5% −7.4%
twitter −29.7% −34.8%

(canada is almost all numbers)

Instruction counts for different inputs:

String main This PR (x86-64)
2 B key 89 88
10 B key 164 131
17 B name 232 103
24 B timestamp 302 144
129 B ASCII 1,371 290
4 KiB ASCII 41,424 6,690
4 KiB UTF-8 44,008 7,098
4 KiB, every 16th byte escaped 52,035 27,729
4 KiB, every 64th byte escaped 43,688 11,540
4 KiB, all quotes 118,824 114,721

No input is slower.

Tests & correctness

I have added a test to cover all branches to validate it. (This is somewhat abbreviated on miri for time)

Every byte that gets escaped is still decided by the original loop's ESCAPE lookup. The existing unsafe UTF-8 splits still happen only at ASCII bytes the table chose. So skip_to_escape can break output in only one way: by returning an index past a byte that needs escaping. Returning one too early (a false flag, or a position below the real one) costs speed, not correctness.

This was manually tested with two fuzzers compare output with serde_json's original per-byte algorithm

Explanation of algorithm

The core of the algorithm builds on the idea that it is possible to efficiently check if each byte in an integer is zero. This is accomplished though a standard trick. We can leverage this to compare bytes against known fixed characters (by xoring) and those less than a given value. By:

  • seting the high bit of the first byte of word that is less than n. (When subtracting no bit below it; a borrow out of that byte may flag bytes after it.)
  • A byte below 0x80 minus n wraps around to 0x80 or more, setting its high bit and borrowing from the next byte, exactly when it is less than n.
  • A byte of 0x80 or more, such as any byte of a multi-byte UTF-8 char, is at least n, so it borrows nothing from the next byte. Its high bit may still be set after the subtraction, but & !word clears it, since the byte's own high bit is set. Such a byte is never flagged, even after a borrow.

Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant