buff-rs
A Rust implementation of BUFF: Decomposed Bounded Floats for Fast Compression and Queries.
Based on the VLDB 2021 paper: BUFF: Accelerating Queries in Memory through Decomposed Bounded Floats.
Overview
BUFF provides efficient compression and query execution for bounded floating-point data. Unlike general-purpose compression, BUFF is designed specifically for numeric data with known precision bounds (e.g., sensor readings, financial data), enabling:
- Precision Bounding - Determines minimum bits needed to represent values within a given tolerance
- Byte Slicing - Column-oriented storage where each bit position is stored contiguously
- Direct Queries - Aggregate and filter operations execute directly on compressed data
Features
- Encode arrays of
f64values with configurable precision - Decode back to
f64with controlled precision loss - Encode arrays of
decimal_bytes::Decimalvalues with configurable precision - Decode back to
decimal_bytes::Decimalwith controlled precision loss - Query compressed data directly (sum, max, count)
- Special value support: Infinity, -Infinity, NaN
- Columnar storage layout optimized for analytical queries
- Optional
decimal-bytesinterop for PostgreSQL NUMERIC compatibility - Zero required dependencies (only
thiserror)
Installation
Add to your Cargo.toml:
[]
= "0.1"
Optional Features
[]
# Enable decimal-bytes interop for PostgreSQL NUMERIC compatibility
= { = "0.1", = ["decimal"] }
Quick Start
use BuffCodec;
// Create a codec with 3 decimal places of precision (scale=1000)
let codec = new;
// Encode an array of f64 values
let data = vec!;
let encoded = codec.encode.unwrap;
// Decode back to f64
let decoded = codec.decode.unwrap;
// Query directly on compressed data
let sum = codec.sum.unwrap;
let max = codec.max.unwrap;
Choosing a Scale
The scale determines the precision of encoded values:
| Scale | Decimal Places | Example Value |
|---|---|---|
| 10 | 1 | 3.1 |
| 100 | 2 | 3.14 |
| 1000 | 3 | 3.142 |
| 10000 | 4 | 3.1416 |
| 100000 | 5 | 3.14159 |
Choose a scale that matches your data's required precision. Higher scales provide more precision but may reduce compression ratio.
Special Values (Infinity, NaN)
BUFF supports special floating-point values:
use BuffCodec;
let codec = new;
let data = vec!;
// Use encode_with_special for arrays containing special values
let encoded = codec.encode_with_special.unwrap;
let decoded = codec.decode.unwrap;
assert!;
assert!;
Decimal-bytes Interop (PostgreSQL NUMERIC)
Enable the decimal feature for decimal-bytes compatibility:
[]
= { = "0.1", = ["decimal"] }
use BuffCodec;
use Decimal;
let codec = new;
// Encode Decimal values (with precision loss)
let decimals: = vec!;
let encoded = codec.encode_decimals.unwrap;
// Decode back to Decimal
let decoded: = codec.decode_to_decimals.unwrap;
Warning: Converting between BUFF and Decimal involves precision loss because BUFF uses bounded floating-point representation while Decimal uses exact arbitrary-precision.
Performance
Key performance characteristics (run cargo bench locally for your hardware):
| Operation | Throughput | Notes |
|---|---|---|
| Encode (1K values) | ~430 Melem/s | 2.3 µs per 1000 floats |
| Encode (100K values) | ~305 Melem/s | 328 µs per 100K floats |
| Decode (1K values) | ~1.66 Gelem/s | 602 ns per 1000 floats |
| Decode (100K values) | ~750 Melem/s | 134 µs per 100K floats |
| Sum (compressed) | ~1.5 Gelem/s | Query without full decompression |
| Max (compressed) | ~880 Melem/s | Query without full decompression |
Compression Ratio
| Scale | Precision | Compressed Size | Ratio |
|---|---|---|---|
| 100 | 2 decimal places | 20 KB | 25% of original |
| 1000 | 3 decimal places | 30 KB | 37.5% of original |
| 10000 | 4 decimal places | 30 KB | 37.5% of original |
(For 10,000 f64 values = 80 KB uncompressed)
Comparison with decimal-bytes
For 1,000 values with 3 decimal places:
| Metric | buff-rs | decimal-bytes |
|---|---|---|
| Storage size | 2,020 bytes | 4,971 bytes |
| Decode array | 628 ns | 60.5 µs |
| Encode array | 2.6 µs | N/A (row-oriented) |
BUFF provides ~2.5x better compression and ~96x faster array decoding for columnar workloads, while decimal-bytes is optimized for individual value operations with lexicographic ordering.
When to Use BUFF vs decimal-bytes
| Aspect | decimal-bytes | buff-rs |
|---|---|---|
| Data Type | Single decimal values | Arrays of floats |
| Precision | Arbitrary (unlimited digits) | Bounded (fixed scale) |
| Storage Layout | Row-oriented | Column-oriented (byte-sliced) |
| Primary Use | Document storage | Columnar/time-series data |
| Query Style | Decode then compare | Query compressed data |
| Sortable Bytes | Yes (lexicographic) | No (optimized for compression) |
Use decimal-bytes when:
- You need exact arbitrary-precision decimals (like PostgreSQL NUMERIC)
- Values are stored individually in documents
- You need lexicographically sortable byte representation
Use buff-rs when:
- You have arrays of floating-point values with known precision
- You're building columnar storage or time-series databases
- You want to query compressed data without full decompression
How It Works
Precision Bounding
Given a precision tolerance (e.g., 0.001 for 3 decimal places), BUFF determines the minimum number of bits needed to represent each value. This is done by analyzing the IEEE 754 representation and finding the position where further bits don't affect precision.
Byte Slicing
Instead of storing values row-by-row, BUFF stores them column-by-column at the byte level:
Traditional: [v1_byte0, v1_byte1] [v2_byte0, v2_byte1] [v3_byte0, v3_byte1]
Byte-sliced: [v1_byte0, v2_byte0, v3_byte0] [v1_byte1, v2_byte1, v3_byte1]
This layout enables SIMD-accelerated comparisons for range queries.
Sign Flipping
To enable proper ordering of negative and positive values, the sign bit is flipped during encoding. This ensures that comparing encoded bytes yields correct numerical ordering.
Compression Performance
Compression ratio depends on data characteristics:
- Narrow value range: Better compression (fewer bits needed for delta)
- Wide value range: More bits needed, lower compression
- Repetitive values: Good compression (single base value)
Typical compression ratios range from 0.3x to 0.8x of the original size (8 bytes per f64).
License
MIT License - see LICENSE for details.
References
- Chunwei Liu, et al. "Decomposed Bounded Floats for Fast Compression and Queries." VLDB 2021. Paper
- Original implementation: Tranway1/buff