Skip to content

Vectorize JSON escaping#64

Merged
xosnrdev merged 4 commits into
masterfrom
vectorize-a-few
Feb 24, 2026
Merged

Vectorize JSON escaping#64
xosnrdev merged 4 commits into
masterfrom
vectorize-a-few

Conversation

@xosnrdev

@xosnrdev xosnrdev commented Feb 23, 2026

Copy link
Copy Markdown
Owner

Summary

Vectorize JSON byte escaping with NEON, AVX2, and AVX-512BW; eliminate per-entry heap allocation in SSTable reads; replace linear memtable scan with binary search.

Latency result (x86_64 AVX2)

Scalar is the byte-at-a-time lookup table from master. Vectorized is the SIMD scanner on this branch.

Workload Size Scalar Vectorized Speedup
clean (0% escapes) 64 B 46.2 ns 20.6 ns 2.2x
256 B 146.8 ns 23.1 ns 6.4x
1 KiB 585.8 ns 83.2 ns 7.0x
4 KiB 2.18 µs 160.9 ns 13.5x
16 KiB 8.62 µs 455.9 ns 18.9x
mixed (2% escapes) 64 B 48.3 ns 26.5 ns 1.8x
256 B 153.4 ns 63.3 ns 2.4x
1 KiB 607.5 ns 270.4 ns 2.2x
4 KiB 2.38 µs 933.7 ns 2.5x
16 KiB 9.10 µs 3.61 µs 2.5x

Core Components

  • simd crate (new): write_escaped_json_bytes dispatches to find_next_escape_avx512bw, find_next_escape_avx2, find_next_escape_neon, or find_next_escape_fallback based on arch/CPU features
  • FIND_NEXT_ESCAPE_DISPATCH: self-patching static mut function pointer -- CPU detection runs once, subsequent calls pay one indirect jump
  • SstableReader: gains data_buf: Vec<u8> field; reused across entries via resize instead of allocating per read
  • ReadSnapshot::get: linear scan over memtable_entries replaced with binary_search_by_key

Platform Support

  • aarch64: NEON 16-byte lanes via vcltq_u8 / vcgtq_u8 / vceqq_u8, bitmask extracted with vpadd_u8 + vreinterpret_u16_u8
  • x86 / x86_64: runtime dispatch via is_x86_feature_detected!; AVX-512BW processes 64 bytes/iter using __mmask64 OR, AVX2 processes 32 bytes/iter using vpmovmskb; LLVM lowers both to a vpaddb + vpminub/vpcmpltub saturation range check
  • all: SWAR fallback processes 8/4/2/1 bytes per step using the Falk Hüffner has-zero-byte identity

Helpers

  • find_next_escape_raw: selects arch path at compile time--falls through to find_next_escape_fallback on unsupported targets

Tests

  • Add invariant test for simd::json

@xosnrdev xosnrdev changed the title Vectorize JSON and CSV escaping Vectorize JSON escaping Feb 24, 2026
@xosnrdev xosnrdev marked this pull request as ready for review February 24, 2026 08:37
@xosnrdev xosnrdev merged commit 31620d1 into master Feb 24, 2026
9 checks passed
@xosnrdev xosnrdev deleted the vectorize-a-few branch February 24, 2026 08:40
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