Skip to content

perf(parquet): vectorize ARM64 NEON bool unpacking for ~4x throughput - #731

Merged
zeroshade merged 3 commits into
apache:mainfrom
zeroshade:neon-bool-optimize
Apr 2, 2026
Merged

zeroshade merged 3 commits into
apache:mainfrom
zeroshade:neon-bool-optimize

Conversation

@zeroshade

Copy link
Copy Markdown
Member

Rationale for this change

Improve the bytes_to_bools implementation on ARM64 NEON with actual SIMD instructions. The result is a ~4x throughput improvement for ARM64

What changes are included in this PR?

Rewrote the assembly using DUP + CMTST NEON pattern.

  1. ld1r {v2.8b}, [ptr] — broadcast one input byte across all 8 SIMD lanes
  2. cmtst v2.8b, v2.8b, v0.8b — parallel bit-test against mask [1,2,4,8,16,32,64,128]
  3. and v2.8b, v2.8b, v1.8b — normalize 0xFF → 0x01 for valid Go bool values
  4. st1 {v2.8b}, [ptr], Prepare Docker based test system #8 — store 8 output bools at once with post-increment
    A scalar tail handles the last few bits when fewer than 8 output slots remain.

Are these changes tested?

All existing tests continue to pass, new tests added to further validate

  • Added TestBytesToBoolsCorrectness — validates every bit position against the reference Go implementation for sizes 1–1024 bytes
  • Added TestBytesToBoolsOutlenSmaller — edge case where output is smaller than 8× input
  • Added BenchmarkBytesToBools — parametric benchmark at 64B, 256B, 1KB, 4KB, 16KB

Are there any user-facing changes?

No, this is purely a performance optimization:

Benchmark Results (Apple M4, darwin/arm64)

                               baseline (scalar)   optimized (NEON)
                                   sec/op              sec/op    vs base

BytesToBools/bytes=64-10           82.69n              21.57n     -73.91% (p=0.008)
BytesToBools/bytes=256-10         333.60n              86.43n     -74.09% (p=0.008)
BytesToBools/bytes=1K-10           1.322µ              327.4n     -75.23% (p=0.008)
BytesToBools/bytes=4K-10           5.293µ              1.297µ     -75.50% (p=0.008)
BytesToBools/bytes=16K-10         21.343µ              5.184µ     -75.71% (p=0.008)
geomean                            1.327µ              333.1n     -74.90%

Throughput: 735 MiB/s → 2,863 MiB/s (+298%)
Zero allocations in both versions. All results statistically significant.

@zeroshade
zeroshade requested review from kou and lidavidm March 27, 2026 16:45
@zeroshade
zeroshade force-pushed the neon-bool-optimize branch 2 times, most recently from 3f4c06d to 20159d9 Compare March 27, 2026 20:18
zeroshade added a commit that referenced this pull request Mar 29, 2026
### Rationale for this change
The SSE4 and AVX2 implementations of _bytes_to_bools in
parquet/internal/utils/ contain zero SIMD instructions. They completely
failed to auto-vectorize the C loop, producing purely scalar code
(movzx/shr/and/mov one bit at a time). The SSE4 and AVX2 .s files are
byte-for-byte identical — just scalar code with different labels.

This is the amd64 counterpart to #731 which fixed the same issue on
ARM64 NEON.

### What changes are included in this PR?

Rewrote both assembly implementations with actual SIMD vectorized code.
SSE4 (unpack_bool_sse4_amd64.s) — processes 2 input bytes → 16 output
bools per iteration:

1. MOVWLZX + MOVD — load 2 input bytes into XMM
2. PSHUFB — broadcast byte 0 → lanes 0-7, byte 1 → lanes 8-15
3. PAND + PCMPEQB — parallel bit-test against mask
[1,2,4,8,16,32,64,128] × 2
4. PAND — normalize 0xFF → 0x01 for valid Go bool values
5. MOVOU — store 16 output bools at once

AVX2 (unpack_bool_avx2_amd64.s) — processes 4 input bytes → 32 output
bools per iteration:

1. MOVL + MOVD + VPBROADCASTD — load and broadcast 4 bytes across all 32
YMM lanes
2. VPSHUFB — distribute each byte to its 8 corresponding lanes
3. VPAND + VPCMPEQB + VPAND — parallel bit-test + normalize to 0/1
4. VMOVDQU — store 32 output bools at once
5. VZEROUPPER — avoid SSE-AVX transition penalties on return

Both include scalar tails for when fewer than vector-width output slots
remain.

### Are these changes tested?

All existing tests continue to pass, new tests added to further
validate:

- TestBytesToBoolsCorrectness — validates every bit position against the
reference Go implementation for sizes 1–1024 bytes
- TestBytesToBoolsOutlenSmaller — edge case where output is smaller than
8× input
- BenchmarkBytesToBools — parametric benchmark at 64B, 256B, 1KB, 4KB,
16KB


### Are there any user-facing changes?
No, this is purely a performance optimization:

*Benchmark Results (AMD Ryzen 7 7800X3D, linux/amd64)*

```
                               baseline (scalar)   optimized (AVX2)
                                   sec/op              sec/op       vs base
BytesToBools/bytes=64-16           146.0n              15.60n     -89.32% (p=0.008)
BytesToBools/bytes=256-16          562.3n              63.36n     -88.73% (p=0.008)
BytesToBools/bytes=1K-16           2.247µ              253.9n     -88.70% (p=0.008)
BytesToBools/bytes=4K-16           8.970µ              1.018µ     -88.65% (p=0.008)
BytesToBools/bytes=16K-16         35.798µ              4.044µ     -88.70% (p=0.008)
geomean                            2.262µ              252.8n     -88.82%
```

Throughput: 432 MiB/s → 3,853 MiB/s (+795%)
Zero allocations in both versions. All results statistically
significant.
Matt Topol and others added 3 commits April 2, 2026 14:12
Replace the scalar bit-by-bit implementation of _bytes_to_bools_neon
with actual NEON SIMD using a DUP+CMTST pattern that processes one
input byte into 8 output bools per vector iteration.

The previous "NEON" implementation used no SIMD instructions at all—it
loaded each byte with ldrb, extracted individual bits with ubfx, and
stored them one at a time with strb. The new version broadcasts each
byte across 8 lanes via ld1r, tests all 8 bits in parallel with cmtst
against a [1,2,4,8,16,32,64,128] mask, normalizes to 0/1 with an AND,
and stores all 8 results at once via st1 with post-increment.

A scalar fallback handles the tail when fewer than 8 output slots remain.

Benchmarks on Apple M4 (darwin/arm64):

  BytesToBools/64B   82.69ns → 21.57ns  (3.83x, 738→2830 MiB/s)
  BytesToBools/256B  333.6ns → 86.43ns  (3.86x, 732→2825 MiB/s)
  BytesToBools/1KB   1322ns  → 327.4ns  (4.04x, 739→2983 MiB/s)
  BytesToBools/4KB   5293ns  → 1297ns   (4.08x, 738→3013 MiB/s)
  BytesToBools/16KB  21343ns → 5184ns   (4.12x, 732→3014 MiB/s)

  geomean: -74.9% latency, +298% throughput
@zeroshade
zeroshade force-pushed the neon-bool-optimize branch from 20159d9 to cf073c2 Compare April 2, 2026 18:16
@zeroshade
zeroshade merged commit 8c6539f into apache:main Apr 2, 2026
55 of 59 checks passed
@zeroshade
zeroshade deleted the neon-bool-optimize branch April 24, 2026 17:30
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