Abstract
Variable-length integers (varints) are essential for space-efficient data storage but present significant decoding bottlenecks due to unpredictable branch patterns. We present SFVInt, a high-performance decoding library that leverages x86 Bit Manipulation Instructions (BMI2) to replace branch-heavy logic with parallel bit extraction. SFVInt achieves up to a 2x speedup over industry-standard libraries like Google Protobuf and Facebook Folly while maintaining a concise 500-line codebase.
Introduction
Variable-length integers, specifically the Little Endian Base-128 (LEB128) format, are the backbone of modern data serialization and storage systems, including Google Protobuf, Apache Lucene, and WebAssembly. By using 7 bits for data and an 8th bit as a "continuation flag," LEB128 significantly reduces the footprint of small integers.
However, the efficiency of the format comes at a high computational cost during decoding. Because the length of each integer is unpredictable, traditional decoders rely on byte-at-a-time processing and conditional branches. For modern superscalar CPUs, these unpredictable branches lead to frequent pipeline stalls and mispredictions. Furthermore, this sequential approach is inherently difficult to vectorize, leaving significant hardware performance untapped.
The SFVInt Approach
SFVInt shifts the decoding strategy from a control-flow problem (branch-and-check) to a data-flow problem (extract-and-map) by utilizing the x86 BMI2 instruction set, specifically the PEXT (Parallel Bit Extract) and PDEP (Parallel Bit Deposit) instructions.
1. Bulk Extraction and Pattern Mapping
Instead of reading a single byte, SFVInt loads a 64-bit word. It uses _pext_u64 to filter out all continuation bits from the word simultaneously. The resulting bitmask, referred to as mval, identifies exactly how many integers are contained within the 64-bit chunk and where their boundaries lie. This mask is then used in a switch-case statement to jump to a highly optimized code path tailored to that specific bit pattern.
2. The 6-Byte Optimization
While processing 8 bytes at once is theoretically possible, the authors discovered a "sweet spot" using a 6-byte mask. An 8-byte mask creates a massive number of possible configurations, which can bloat the instruction footprint and lead to L1 instruction cache misses. A 6-byte approach balances throughput with a manageable number of code paths.
3. Handling Cross-Boundary Integers
To handle integers that span across two 64-bit words, SFVInt maintains internal state variables (shift_bits and partial_value). This allows the decoder to seamlessly reconstruct split integers without falling back to slow, scalar processing.
4. Generic Implementation
Unlike previous specialized SIMD decoders that require thousands of lines of code, SFVInt is implemented in approximately 500 lines of C++. It utilizes C++ templates to provide a single, generic codebase that handles both 32-bit and 64-bit unsigned integers.
Experimental Evaluation
SFVInt was evaluated across four distinct workloads designed to simulate various data environments:
- W1 (Uniform): 32-bit integers distributed uniformly across the full range.
- W2 (WebAssembly): Real-world lengths from the WebAssembly build suite, where 90% of integers are 1-byte.
- W3 & W4 (Proprietary): Data from ByteDance internal systems, representing realistic, skewed distributions (72% to 81% 1-byte integers).
The benchmarks were performed on AWS EC2 instances covering multiple generations of Intel (Ice Lake, Skylake, Haswell) and AMD (EPYC Milan and Rome) architectures.
Performance and Key Insights
SFVInt consistently outperformed Google Protobuf and Facebook Folly by approximately 2x on modern Intel (Ice Lake) and AMD (Zen 3/Milan) hardware. Notably, the library maintained its performance lead even on datasets heavily skewed toward 1-byte integers, where traditional branch-based decoders typically perform best.
A critical finding of the research is the disparity in hardware implementation of BMI2. While Intel executes PEXT natively and extremely fast, older AMD Zen 2 processors emulate these instructions in microcode, resulting in higher latency. On such legacy hardware, SFVInt may be slower than traditional methods, suggesting that production implementations should include a dynamic selection mechanism based on the detected CPU architecture.
Conclusion
SFVInt demonstrates that by leveraging specific hardware primitives like BMI2, it is possible to achieve significant performance gains in data decoding with minimal code complexity. By transforming branch-heavy logic into parallel bit manipulation, SFVInt provides a robust, generic solution for high-throughput varint processing. Future work will focus on supporting ZigZag-encoded signed integers and integrating the library into large-scale data frameworks like Apache Parquet.
Summary updated