Crate json_escape_simd

Crate json_escape_simd 

Source
Expand description

Optimized SIMD routines for escaping JSON strings.

§
Important

On aarch64 NEON hosts the available register width is 128 bits, which is narrower than the lookup table this implementation prefers. As a result the SIMD path may not outperform the generic fallback, which is reflected in the benchmark numbers below.

On some modern macOS devices with larger register numbers, the SIMD path may outperform the generic fallback, see the M3 max benchmark below.

§Note

The force_aarch64_neon feature flag can be used to force use of the neon implementation on aarch64. This is useful for the benchmark.

§Benchmarks

Numbers below come from cargo bench runs on GitHub Actions hardware. Criterion reports are summarized to make it easier to spot relative performance. “vs fastest” shows how much slower each implementation is compared to the fastest entry in the table (1.00× means fastest).

§GitHub Actions x86_64 (ubuntu-latest)

AVX2 enabled.

RxJS payload (~10k iterations)

ImplementationMedian timevs fastest
escape simd341.18 µs1.00×
escape v_jsonescape555.47 µs1.63×
escape generic656.85 µs1.93×
serde_json744.75 µs2.18×
json-escape777.15 µs2.28×

Fixtures payload (~300 iterations)

ImplementationMedian timevs fastest
escape simd12.67 ms1.00×
escape v_jsonescape20.58 ms1.62×
escape generic22.57 ms1.78×
serde_json24.52 ms1.94×
json-escape26.97 ms2.13×

§GitHub Actions aarch64 (ubuntu-24.04-arm)

Neon enabled.

RxJS payload (~10k iterations)

ImplementationMedian timevs fastest
escape generic546.89 µs1.00×
escape simd589.29 µs1.08×
serde_json612.33 µs1.12×
json-escape624.66 µs1.14×
escape v_jsonescape789.14 µs1.44×

Fixtures payload (~300 iterations)

ImplementationMedian timevs fastest
escape generic17.81 ms1.00×
serde_json19.77 ms1.11×
json-escape20.84 ms1.17×
escape simd21.04 ms1.18×
escape v_jsonescape25.57 ms1.44×

§GitHub Actions macOS (macos-latest)

Apple M1 chip

RxJS payload (~10k iterations)

ImplementationMedian timevs fastest
escape generic759.07 µs1.00×
escape simd764.98 µs1.01×
serde_json793.91 µs1.05×
json-escape868.21 µs1.14×
escape v_jsonescape926.00 µs1.22×

Fixtures payload (~300 iterations)

ImplementationMedian timevs fastest
serde_json26.41 ms1.00×
escape generic26.43 ms1.00×
escape simd26.42 ms1.00×
json-escape28.94 ms1.10×
escape v_jsonescape29.22 ms1.11×

§Apple M3 Max

RxJS payload (~10k iterations)

ImplementationMedian timevs fastest
escape simd307.20 µs1.00×
escape generic490.00 µs1.60×
serde_json570.35 µs1.86×
escape v_jsonescape599.72 µs1.95×
json-escape644.73 µs2.10×

Fixtures payload (~300 iterations)

ImplementationMedian timevs fastest
escape generic17.89 ms1.00×
escape simd17.92 ms1.00×
serde_json19.78 ms1.11×
escape v_jsonescape21.09 ms1.18×
json-escape22.43 ms1.25×

Functions§

escape
Main entry point for JSON string escaping with SIMD acceleration If the platform is supported, the SIMD path will be used. Otherwise, the generic fallback will be used.
escape_generic
escape_into
Main entry point for JSON string escaping with SIMD acceleration If the platform is supported, the SIMD path will be used. Otherwise, the generic fallback will be used.
escape_into_generic