Skip to content

perf(spark): use 256-entry byte-pair table in hex encoding#21836

Open
Scolliq wants to merge 3 commits intoapache:mainfrom
Scolliq:perf/spark-hex-byte-table
Open

perf(spark): use 256-entry byte-pair table in hex encoding#21836
Scolliq wants to merge 3 commits intoapache:mainfrom
Scolliq:perf/spark-hex-byte-table

Conversation

@Scolliq
Copy link
Copy Markdown

@Scolliq Scolliq commented Apr 25, 2026

Refs #15986.

Why: spark_hex walked one nibble at a time — two HEX_CHARS[i] lookups and two Vec::push calls per input byte. The hot loop flattens into one indexed load and one extend_from_slice per byte with a precomputed table.

What changed: added HEX_LOOKUP_LOWER / HEX_LOOKUP_UPPER as [[u8; 2]; 256] const tables built at compile time. Bytes path now does a single lookup + 2-byte extend per input byte. The int64 path consumes two nibbles per iteration via the same table, with a fall-through for the high nibble. Behaviour for 0, i64::MAX, i64::MIN, -1 preserved.

Tests: extended test_hex_int64 to cover edge values; new test_hex_lookup_table_covers_all_bytes cross-checks every entry against format!("{:02X/x}"); new test_spark_hex_binary_round_trip_all_bytes feeds all 256 byte values through spark_hex and verifies the result.

cargo test -p datafusion-spark --lib hex → 8 pass. cargo clippy --all-features --all-targets clean. cargo bench --no-run builds — existing benches/hex.rs already covers Int64/Utf8/Utf8View/LargeUtf8/Binary/LargeBinary plus dict paths.

Not in this PR: the #15947 review also flagged Utf8View output and dictionary-key reuse — those felt worth their own PRs to keep this focused on the per-byte hot path.

The bytes path looked up two nibbles and pushed two bytes per input
byte. Replace it with a precomputed `[[u8; 2]; 256]` table built at
compile time, so each input byte becomes one indexed load and one
two-byte extend_from_slice. The int64 path now consumes two nibbles
per loop iteration via the same table, with a fall-through for the
single high nibble.

Existing benchmarks in `datafusion/spark/benches/hex.rs` cover the
hot paths (Int64, Utf8, Utf8View, LargeUtf8, Binary, LargeBinary,
plus dictionary variants).

Adds tests covering all 256 byte values against `format!("{:02X/x}")`
and i64 edge cases (`0`, `i64::MAX`, `i64::MIN`, `-1`).

Refs apache#15986
@github-actions github-actions Bot added the spark label Apr 25, 2026
@Jefffrey
Copy link
Copy Markdown
Contributor

run benchmark hex

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c4318059003-1840-bkjg5 6.12.55+ #1 SMP Sun Feb 1 08:59:41 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/spark-hex-byte-table (7fc5a40) to 794f30e (merge-base) diff
BENCH_NAME=hex
BENCH_COMMAND=cargo bench --features=parquet --bench hex
BENCH_FILTER=
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot
Copy link
Copy Markdown

🤖 Criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                       main                                   perf_spark-hex-byte-table
-----                       ----                                   -------------------------
hex_binary/size=1024        1.66     68.4±0.13µs        ? ?/sec    1.00     41.3±0.23µs        ? ?/sec
hex_binary/size=4096        1.57    280.4±0.42µs        ? ?/sec    1.00    178.3±0.45µs        ? ?/sec
hex_binary/size=8192        1.56    551.9±0.86µs        ? ?/sec    1.00    354.8±1.11µs        ? ?/sec
hex_int64/size=1024         1.15      9.6±0.14µs        ? ?/sec    1.00      8.3±0.27µs        ? ?/sec
hex_int64/size=4096         1.20     37.9±0.32µs        ? ?/sec    1.00     31.5±1.02µs        ? ?/sec
hex_int64/size=8192         1.14     86.1±0.61µs        ? ?/sec    1.00     75.8±0.82µs        ? ?/sec
hex_int64_dict/size=1024    1.17      9.0±0.32µs        ? ?/sec    1.00      7.7±0.73µs        ? ?/sec
hex_int64_dict/size=4096    1.22     35.6±0.75µs        ? ?/sec    1.00     29.2±2.49µs        ? ?/sec
hex_int64_dict/size=8192    1.21     77.4±0.70µs        ? ?/sec    1.00     63.9±2.15µs        ? ?/sec
hex_utf8/size=1024          1.66     68.5±0.13µs        ? ?/sec    1.00     41.2±0.16µs        ? ?/sec
hex_utf8/size=4096          1.57    280.2±0.39µs        ? ?/sec    1.00    178.2±0.65µs        ? ?/sec
hex_utf8/size=8192          1.57    554.9±0.79µs        ? ?/sec    1.00    354.1±1.13µs        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 130.0s
Peak memory 5.2 GiB
Avg memory 5.1 GiB
CPU user 155.2s
CPU sys 0.9s
Peak spill 0 B

branch

Metric Value
Wall time 130.0s
Peak memory 5.2 GiB
Avg memory 5.1 GiB
CPU user 155.4s
CPU sys 0.3s
Peak spill 0 B

File an issue against this benchmark runner

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants