vb
The fastest VByte encoding library in the Rust ecosystem.
Encode at 430M integers/sec, decode at 415M integers/sec — 2.4x faster encoding and 1.2x faster decoding than alternatives.
Table of Contents
Features
- Blazing Fast: Hand-optimized with loop unrolling, bounds check elimination, and CLZ instructions
- Variable Byte Encoding: Compresses
u64integers using 1-10 bytes based on magnitude - Differential Encoding: Optimizes strictly increasing sequences (requires
difffeature) - Zero-Copy Decoding: Decode directly from byte slices with offset tracking
- Minimal Dependencies: Only
thiserrorfor error handling
Installation
[]
= "0.2"
# With differential encoding support
= { = "0.2", = ["diff"] }
Usage
Basic Encoding
use ;
let numbers = vec!;
// Encode
let encoded = e_li;
println!;
// Decode
let decoded = d_li.unwrap;
assert_eq!;
Differential Encoding
Ideal for sorted sequences like timestamps, IDs, or offsets.
use ;
let timestamps = vec!;
// Stores only deltas: [1000000, 5, 5, 32]
let encoded = e_diff;
let decoded = d_diff.unwrap;
assert_eq!;
API Reference
| Function | Description |
|---|---|
e(value, buf) |
Encode single u64, append to buffer |
d(bytes) |
Decode single u64, return (value, bytes_consumed) |
e_li(iter) |
Encode iterator of u64 to Vec<u8> |
d_li(bytes) |
Decode bytes to Vec<u64> |
e_diff(slice) |
Encode increasing sequence with delta compression |
d_diff(bytes) |
Decode delta-compressed sequence |
Performance
Benchmarked with 10,000 integers (60% small, 30% medium, 10% large):
| Library | Encode (M/s) | Decode (M/s) |
|---|---|---|
| vb | 430 | 415 |
| leb128 | 289 | 213 |
| integer-encoding | 176 | 349 |
Run benchmarks yourself:
Design
VByte uses 7 bits per byte for data, with the MSB as continuation flag:
MSB = 0: Final byteMSB = 1: More bytes follow
Key optimizations:
- Fast path: Single-byte values (< 128) skip all loops
- Loop unrolling: 2-5 byte cases fully unrolled
- Bounds elimination: Unsafe pointer arithmetic when ≥10 bytes available
- CLZ instruction:
leading_zeros()calculates byte count in one CPU cycle
Bench
VByte Encoding Benchmark
Comparing varint encoding libraries with 10,000 integers (mixed distribution: 60% small, 30% medium, 10% large).
Results
| Library | Encode (M/s) | Decode (M/s) |
|---|---|---|
| vb | 430.5 | 414.9 |
| integer-encoding | 176.2 | 348.6 |
| leb128 | 289.2 | 212.9 |
Environment
macOS 26.1 (arm64) · Apple M2 Max · 12 cores · 64.0GB · rustc 1.94.0-nightly (21ff67df1 2025-12-15)
About
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
vb
Rust 生态中最快的 VByte 编码库。
编码速度 4.3 亿整数/秒,解码速度 4.15 亿整数/秒 — 编码比同类库快 2.4 倍,解码快 1.2 倍。
目录
功能特性
- 极致性能:手工优化,包括循环展开、边界检查消除、CLZ 指令加速
- 变长字节编码:根据数值大小,用 1-10 字节压缩
u64整数 - 差分编码:优化严格递增序列(需开启
diff特性) - 零拷贝解码:直接从字节切片解码,支持偏移量追踪
- 依赖精简:仅依赖
thiserror处理错误
安装
[]
= "0.2"
# 启用差分编码
= { = "0.2", = ["diff"] }
使用指南
基础编码
use ;
let numbers = vec!;
// 编码
let encoded = e_li;
println!;
// 解码
let decoded = d_li.unwrap;
assert_eq!;
差分编码
适用于时间戳、ID、偏移量等有序序列。
use ;
let timestamps = vec!;
// 仅存储差值: [1000000, 5, 5, 32]
let encoded = e_diff;
let decoded = d_diff.unwrap;
assert_eq!;
API 参考
| 函数 | 说明 |
|---|---|
e(value, buf) |
编码单个 u64,追加到缓冲区 |
d(bytes) |
解码单个 u64,返回 (值, 消耗字节数) |
e_li(iter) |
将 u64 迭代器编码为 Vec<u8> |
d_li(bytes) |
将字节解码为 Vec<u64> |
e_diff(slice) |
差分压缩递增序列 |
d_diff(bytes) |
解码差分压缩序列 |
性能
测试数据:10,000 个整数(60% 小值,30% 中值,10% 大值)
| 库 | 编码 (百万/秒) | 解码 (百万/秒) |
|---|---|---|
| vb | 430 | 415 |
| leb128 | 289 | 213 |
| integer-encoding | 176 | 349 |
运行评测:
设计
VByte 每字节用 7 位存数据,最高位 (MSB) 作为延续标志:
MSB = 0:最后一个字节MSB = 1:后续还有字节
核心优化:
- 快速路径:单字节值 (< 128) 跳过所有循环
- 循环展开:2-5 字节场景完全展开
- 边界消除:剩余 ≥10 字节时使用 unsafe 指针运算
- CLZ 指令:
leading_zeros()单周期计算所需字节数
评测
VByte 编码性能评测
对比 varint 编码库,测试数据:10,000 个整数(混合分布:60% 小值,30% 中值,10% 大值)。
结果
| 库 | 编码 (百万/秒) | 解码 (百万/秒) |
|---|---|---|
| vb | 430.5 | 414.9 |
| integer-encoding | 176.2 | 348.6 |
| leb128 | 289.2 | 212.9 |
环境
macOS 26.1 (arm64) · Apple M2 Max · 12 核 · 64.0GB · rustc 1.94.0-nightly (21ff67df1 2025-12-15)
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: