Avoid intermediate bitmap, filter, and wide index buffers in BoolSparseScheme

perfloop-oss/vortex · Avoidable Copy

https://perfloop.ai/t/oss/case_3na6gd1tea

Verdict

VERIFIED · settled 2026-11-30

What happened: The paired measurements met the required improvement.

Hypothesis

When BtrBlocks compresses a sparse non-nullable boolean array, `BoolSparseScheme::encode` delegates to the generic `Sparse::encode` helper and then narrows the resulting indices. That generic path allocates a full negated bitmap when the dominant value is `true`, filters out a throwaway exception-value array that `BoolSparseScheme::encode` immediately discards and replaces with a constant, materializes wide 64-bit index buffers twice, validates a throwaway `SparseArray`, and then scans and copies those indices again to narrow them to `u8`, `u16`, or `u32`. Reach is default: `BoolSparseScheme` is enabled in the default `CORE_2026_08_3` edition and compresses both sparse boolean columns and the sparse mostly-valid validity bitmaps of nullable columns across all column types. Engineering prior: in write workloads with nullable columns containing a few percent nulls or sparse boolean flags, eliminating this full-bitmap copy, discarded filter pass, and multi-stage index conversion removes the majority of sparse-boolean encoding allocations and a meaningful share of boolean compression CPU time, assuming production chunks match the ~64 KiB–256 KiB chunk sizes used by Vortex writers and benchmarks; production column-nullability mix and end-to-end file-write share remain unmeasured until benchmarked on representative datasets.

In `vortex-btrblocks/src/schemes/boolean.rs:148-184`, `BoolSparseScheme::encode` receives a canonical non-nullable `BoolArray` along with precomputed `BoolStats` (`true_count` and `value_count`), which already fix the fill value `fill` and exact exception count `K`. Instead of scanning the underlying `BitBuffer` directly, line 156 calls `Sparse::encode(data.array(), Some(Scalar::from(fill)), exec_ctx)`. Inside `encodings/sparse/src/lib.rs:285-336`, `Sparse::encode` runs a `NotEq` compare against `ConstantArray::new(fill, len)` (allocating an `N/8`-byte inverted `BitBuffer` whenever `fill == true`), wraps it in a `Mask`, calls `array.filter(&non_fill_mask)` to materialize `K` boolean exception values, extracts `MaskValues::indices()` (`Arc<[usize]>`, `8*K` bytes), copies them into `Buffer<u64>` (`8*K` bytes), and constructs a validated `SparseArray` via `Sparse::try_new`. `BoolSparseScheme::encode` then calls `.execute::<PrimitiveArray>(exec_ctx)?.narrow(exec_ctx)?` (lines 160-165), scanning all `K` `u64` indices for min/max and copying into a narrowed `Buffer<u8/u16/u32>`, discards the filtered values in favor of `ConstantArray::new(!fill, num_patches)` (line 176), and validates a second `SparseArray` via `Sparse::try_new` (line 174).

In a sandbox 36-case sweep (`N` in `{16384, 65536, 262144}`, exception strides `{100, 500, 2000}`, `fill` in `{false, true}`, slice offsets `{0, 5}`) using a thread-local counting `GlobalAlloc` over `BtrBlocksCompressor::compress`, every case was selected as `Sparse` by the default `CORE_2026_08_3` compressor and every case violated the `2 * (K * idx_width) + 8192` byte bound. On the sandbox workload of `N = 65,536`, `K = 132`, `fill = true` (narrowed `u16` index payload = 264 bytes), isolated `BoolSparseScheme` allocated 21,732 bytes across 100 allocations (74.2% of the 29,284 total bytes allocated by full default `BtrBlocksCompressor::compress`); at `N = 262,144`, `K = 525`, `fill = true` (2,100-byte `u32` payload), isolated `BoolSparseScheme` allocated 54,480 bytes across 100 allocations (69.7% of the 78,216 total bytes). An experimental direct bit-extraction edit in the sandbox reduced `N = 65,536, K = 132, fill = true` isolated allocations to 5,252 bytes (57 allocations; full default compression dropped from 29,284 to 12,804 bytes and `ci`-profile wall time across 200 iterations dropped from 294.5 ms to 209.7 ms) and `N = 262,144, K = 525, fill = true` isolated allocations to 7,088 bytes (57 allocations; full compression 78,216 to 30,824 bytes), with 0 of 36 cases violating the bound. These are sandbox harness results, not production telemetry.

Upstream check: open PR #12258 (`https://github.com/vortex-data/vortex/pull/12258`) updates `BoolRunEndScheme` and `BoolSparseScheme` cost estimation and adds `descendant_exclusions()`, but does not modify `BoolSparseScheme::encode`. This change is distinct and complementary.

Independent Case proof target: verify in release-mode (`--profile bench`) benchmarks of `BtrBlocksCompressor::compress` on sparse boolean columns and nullable-column validity bitmaps (covering both `fill = true` and `fill = false`, sliced and unsliced `BitBuffer`s, `u8`/`u16`/`u32` index widths, and `K = 0` constant inputs) that direct exception-index extraction preserves exact decoded output (`assert_arrays_eq!`), passes the deterministic allocation-bound check `bool_sparse_alloc_check` (provisional placement), and delivers a measurable end-to-end compression throughput and allocation improvement without regressing dense or non-sparse boolean compression. The claim is rejected if release-mode end-to-end `BtrBlocksCompressor::compress` time does not improve on sparse mostly-true and mostly-false boolean arrays of 65,536–262,144 rows.

Change to test: Extract narrowed exception indices directly from the input BitBuffer in a single word-level pass sized to the known exception count and last exception position, bypassing Sparse::encode, filtered-value materialization, and PrimitiveArray::narrow. Handle the zero-exception case directly with ConstantArray::new(fill, len).

Where it lives

perfloop-oss/vortex · vortex-btrblocks/src/schemes/boolean.rs

Evidence

Scattered boolean compression · 15 sample pairs

metric baseline candidate paired median change confidence range required result
scattered_fixture_ns 33054 23037 −31.1% (−10276) −15529 to −8445 gain > 0 PASSED
scattered_fixture_bytes 67284 46828 −30.4% (−20456) −20456 to −20456 gain > 0 PASSED

Sparse boolean compression · 15 sample pairs

metric baseline candidate paired median change confidence range required result
bool_65536_false_ns 17465 11614 −35% (−6108) −11800 to −3533 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_65536_false_bytes 19696 13216 −32.9% (−6480) −6480 to −6480 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_65536_true_ns 18670 12725 −30.8% (−5752) −7836 to −2430 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_65536_true_bytes 28183 13214 −53.1% (−14969) −14969 to −14969 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_262144_false_ns 36481 24388 −28.6% (−10418) −19004 to −7782 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_262144_false_bytes 44032 31232 −29.1% (−12800) −12800 to −12800 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_262144_true_ns 36609 26408 −9981 −18009 to +1292 no regression (noise ≤ 0.05 × |baseline median|) PASSED
bool_262144_true_bytes 77132 31236 −59.5% (−45896) −45896 to −45896 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Nullable boolean compression · 16 sample pairs

metric baseline candidate paired median change confidence range required result
validity_65536_false_ns 34938 24968 −28.7% (−10012) −13210 to −4301 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_65536_false_bytes 44006 29094 −33.9% (−14912) −14912 to −14912 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_65536_true_ns 30818 24526 −23.1% (−7106) −13679 to −2417 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_65536_true_bytes 52536 29096 −44.6% (−23440) −23440 to −23440 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_262144_false_ns 74423 66973 −8964 −19617 to +1227 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_262144_false_bytes 144304 97672 −32.3% (−46632) −46632 to −46632 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_262144_true_ns 91628 69085 −27.7% (−25384) −35760 to −9257 no regression (noise ≤ 0.05 × |baseline median|) PASSED
validity_262144_true_bytes 177357 97668 −44.9% (−79689) −79689 to −79689 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Dense and run-end boolean compression · 161 sample pairs

metric baseline candidate paired median change confidence range required result
dense_ns 3564 3604 +39.51 −77.24 to +157.5 no regression (noise ≤ 0.05 × |baseline median|) PASSED
dense_bytes 2256 2256 0 0 to 0 no regression (noise ≤ 0.05 × |baseline median|) PASSED
runs_ns 11724 11600 −50.39 −633.6 to +365.2 no regression (noise ≤ 0.05 × |baseline median|) PASSED
runs_bytes 20420 20420 0 0 to 0 no regression (noise ≤ 0.05 × |baseline median|) PASSED

Checks: 7 of 7 passed. Verification: no defect found.

Timeline