Skip to content

Blocked BitPacked: encode blocked bitwidths - #10203

Merged
joseph-isaacs merged 12 commits into
developfrom
mk/bitpacked-variable-encode
Oct 6, 2026
Merged

joseph-isaacs merged 12 commits into
developfrom
mk/bitpacked-variable-encode

Conversation

@mhk197

@mhk197 mhk197 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Tracking Issue: #10167

Summary

Add per-block bit-packing, mirroring the single-width API:

Choose widths Encode at given widths
single width bitpack_to_best_bit_width(array, ctx) bitpack_encode(array, bit_width, bit_width_freq, ctx)
blocked bitpack_blocked_to_best_bit_widths(array, ctx) bitpack_encode_blocked(array, bit_widths, num_exceptions, ctx)

BitPackedData::encode_blocked(array, bit_widths, ctx) sits alongside encode. Blocked output always stores its block offsets as a primitive array (BitWidths::Blocked). Choosing between uniform and blocked output is left to the caller.

The single-width encoder is unchanged apart from sharing the non-negative check with the blocked encoder.

Design

Each block's width minimizes its packed size plus the cost of its exceptions, using the existing best_bit_width cost model on a per-block histogram. The zero padding of a partial last block and null values count as zero-width values, so every block is charged for its full 128 * width bytes. Each block's cost depends only on its own width, and blocked output always pays for its offsets, so choosing every block independently is optimal under this model.

Block offsets use the narrowest unsigned type that holds the final offset.

Additionally, I made the choice to allow blocks that are not packed at all -- but only if there exists at least one block that IS packed. Otherwise, we could encounter a scenario where one block is unpacked and the rest are, and we reject on the basis of one block out of many. There are definitely some improvements that can be made in the future.

Scope

Nothing calls the new encoder yet. fastlanes.bitpacked serialization requires a constant width, so wiring it into BtrBlocks waits for the variable-width wire format.

Results

Measured locally on 64K u32 values with synthetic distributions, comparing bitpack_to_best_bit_widths with bitpack_to_best_bit_width. Sizes are uncompressed nbytes, from 262,144 raw bytes:

Distribution Single width Blocked Change
uniform (7-bit) 57,344 (w=7) 57,474 +130 B (+0.2%)
drift (blocks cycle 1–16 bits) 131,072 (w=16) 69,892 −46.7%
outliers (7-bit, 66 values at 21 bits) 58,384 (w=7, 66 patches) 58,514 +130 B (+0.2%)
zero_heavy (1 in 4 blocks 12-bit) 98,304 (w=12) 24,706 −74.9%

On uniform and outlier data both encoders pack and patch identically. The 130 bytes are the materialized offsets (65 u16 values).

Median encode time, including choosing the widths (median of three local runs):

Distribution Single width Blocked
uniform 74.2 µs 78.2 µs
drift 78.7 µs 83.5 µs
outliers 124.2 µs 127.8 µs
zero_heavy 98.2 µs 97.3 µs

@mhk197 mhk197 added the changelog/feature A new feature label Oct 2, 2026
@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch 2 times, most recently from ad528cd to 662ffac Compare October 2, 2026 18:30
@codspeed

codspeed Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 2101 untouched benchmarks
🆕 3 new benchmarks
⏩ 518 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
🆕 WallTime bitpack_blocked_compress_avx2 N/A 7.7 µs N/A
🆕 WallTime bitpack_blocked_compress_avx512 N/A 5.8 µs N/A
🆕 WallTime bitpack_blocked_compress_neon N/A 11.4 µs N/A

Comparing mk/bitpacked-variable-encode (3471916) with develop (d97fb1c)

Open in CodSpeed

Footnotes

  1. 518 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch 2 times, most recently from 438829b to 75752c3 Compare October 2, 2026 18:58
@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch from 75752c3 to 861873b Compare October 2, 2026 19:12
@mhk197
mhk197 marked this pull request as ready for review October 2, 2026 19:14
@mhk197
mhk197 added this pull request to stack #10243 October 2, 2026 21:14
@mhk197 mhk197 changed the title Blocked BitPacked: encode a bit width per block Blocked BitPacked: encode blocked bitwidths Oct 2, 2026
@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch from de28cfc to fbf3501 Compare October 5, 2026 15:45
@joseph-isaacs

Copy link
Copy Markdown
Contributor

bitpack_to_best_bit_width and bitpack_to_best_bit_widths should be more distinct

@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch from fbf3501 to f55497a Compare October 5, 2026 16:22
Base automatically changed from mk/bitpacked-stack-03-width-child to develop October 5, 2026 16:39
mhk197 added 9 commits October 5, 2026 12:39
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@mhk197
mhk197 force-pushed the mk/bitpacked-variable-encode branch from f55497a to 977cca1 Compare October 5, 2026 16:39
@mhk197

mhk197 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@joseph-isaacs can do bitpack_blocked_to_best_bit_widths

mhk197 added 2 commits October 5, 2026 13:33
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
Signed-off-by: Matt Katz <mhkatz97@gmail.com>
@joseph-isaacs
joseph-isaacs merged commit cc018ee into develop Oct 6, 2026
93 of 94 checks passed
@joseph-isaacs
joseph-isaacs deleted the mk/bitpacked-variable-encode branch October 6, 2026 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants