as-com/varint-simd

Decoding and encoding gigabytes of LEB128 variable-length integers per second in Rust with SIMD

Rust

141

109 commits

updated Sep 3, 2024

See the code

README

varint-simd

Crates.io Docs.rs Continuous integration

varint-simd is a fast SIMD-accelerated variable-length integer and LEB128 encoder and decoder written in Rust. It combines a largely branchless design with compile-time specialization to achieve gigabytes per second of throughput encoding and decoding individual integers on commodity hardware. An interface to decode multiple adjacent variable-length integers is also provided to achieve even higher throughput, reaching over a billion decoded 8-bit integers per second on a single thread.

This library currently targets a minimum of x86_64 processors with support for SSSE3 (Intel Core/AMD Bulldozer or newer), with optional optimizations for processors supporting POPCNT, LZCNT, BMI2, and/or AVX2. It is intended for use in implementations of Protocol Buffers (protobuf), Apache Avro, and similar serialization formats, but likely has many other applications.

Usage

Important: Ensure the Rust compiler has an appropriate target-cpu setting. An example is provided in .cargo/config.toml, but you may need to edit the file to specify the oldest CPUs your compiled binaries will support. Your project will not compile unless this is set correctly.

The native-optimizations feature should be enabled if and only if target-cpu is set to native, such as in the example. This enables some extra optimizations if suitable for your specific CPU. Read more below.

use varint_simd::{encode, decode, encode_zigzag, decode_zigzag};

fn main() {
  let num: u32 = 300;
  
  let encoded = encode::<u32>(num); // turbofish for demonstration purposes, usually not necessary
  // encoded now contains a tuple
  // (
  //    [0xAC, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // encoded in a 128-bit vector
  //    2 // the number of bytes encoded
  // )
  
  let decoded = decode::<u32>(&encoded.0).unwrap();
  // decoded now contains another tuple:
  // (
  //    300, // the decoded number
  //    2 // the number of bytes read from the slice
  // )
  assert_eq!(decoded.0, num);
  
  // Signed integers can be encoded/decoded with convenience functions encode_zigzag and decode_zigzag
  let num: i32 = -20;
  let encoded = encode_zigzag::<i32>(num);
  let decoded = decode_zigzag::<i32>(&encoded.0).unwrap();
  assert_eq!(decoded.0, num);
}

The type parameter passed into the encode/decode functions greatly affects performance - the code takes shorter paths for shorter integers, and may exhibit comparatively poor performance if you're decoding a lot of tiny integers into u64's.

Safety

This crate uses a lot of unsafe code. Please exercise caution, although I do not expect there to be major issues.

There is also an optional "unsafe" interface for bypassing overflow and bounds checks. This can be used when you know your input data won't cause undefined behavior and your calling code can tolerate truncated numbers.

Benchmarks

The benchmarks below reflect the performance of decoding and encoding a sequence of random integers bounded by each integer size. All benchmarks are run with native optimizations. For more details, please see the source code for these benchmarks.

Intel Core i7-8850H "Coffee Lake" (2018 15" MacBook Pro)

benchmark graph

Decode

All numbers are in millions of integers per second.

varint-simd unsafevarint-simd saferustcinteger-encoding-rsprost
u8554.81283.26131.71116.59131.42
u16493.96349.74168.09121.35157.68
u32482.95332.11191.37120.16196.05
u64330.86277.6582.31580.32897.585
varint-simd 2xvarint-simd 4xvarint-simd 8x
u8658.52644.36896.32
u16547.39540.93
u32688.11

Encode

varint-simdrustcinteger-encoding-rsprost
u8383.01214.05126.6693.617
u16341.25181.18126.7985.014
u32360.87157.95125.0077.402
u64303.7272.66078.15346.456

AMD Ryzen 5 2600X @ 4.125 GHz "Zen+"

Decode

varint-simd unsafevarint-simd saferustcinteger-encoding-rsprost
u8537.51304.85152.35138.54124.44
u16403.39300.68170.31156.06147.83
u32293.88235.92160.48159.13150.05
u64229.28193.2875.82285.01083.407
varint-simd 2xvarint-simd 4xvarint-simd 8x
u8943.75808.451,106.50
u16721.01632.03
u32459.77

Encode

varint-simdrustcinteger-encoding-rsprost
u8362.97211.07142.1698.237
u16334.10172.09140.7896.480
u32288.19101.56126.2782.210
u64207.8952.51579.37548.088

TODO

  • Encoding multiple values at once
  • Faster decode for two u64 values with AVX2 (currently fairly slow)
  • Improve performance of "safe" interface
  • Parallel ZigZag decode/encode
  • Support for ARM NEON
  • Fallback scalar implementation
  • Further optimization (I'm pretty sure I left some performance on the table)

Contributions are welcome. 🙂

About the native-optimizations feature

This feature flag enables a build script that detects the current CPU and enables PDEP/PEXT optimizations if the CPU supports running these instructions efficiently. It should be enabled if and only if the target-cpu option is set to native.

This is necessary because AMD Zen, Zen+, and Zen 2 processors implement these instructions in microcode, which means they run much, much slower than if they were implemented in hardware. Additionally, Rust does not allow conditional compilation based on the target-cpu option, so it is necessary to specify this feature manually.

Library crates should not enable this feature by default. A separate feature flag should be provided to enable this feature in this crate.

Previous Work

  • Daniel Lemire, Nathan Kurz, Christoph Rupp - Stream VByte: Faster Byte-Oriented Integer Compression, Information Processing Letters 130, 2018: https://arxiv.org/abs/1709.08990
  • Jeff Plaisance, Nathan Kurz, Daniel Lemire - Vectorized VByte Decoding, International Symposium on Web Algorithms, 2015: https://arxiv.org/abs/1503.07387

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

leb128
rust
simd
varint

as-com/varint-simd

Decoding and encoding gigabytes of LEB128 variable-length integers per second in Rust with SIMD

Rust

141

109 commits

updated Sep 3, 2024

See the code

README

varint-simd

Crates.io Docs.rs Continuous integration

varint-simd is a fast SIMD-accelerated variable-length integer and LEB128 encoder and decoder written in Rust. It combines a largely branchless design with compile-time specialization to achieve gigabytes per second of throughput encoding and decoding individual integers on commodity hardware. An interface to decode multiple adjacent variable-length integers is also provided to achieve even higher throughput, reaching over a billion decoded 8-bit integers per second on a single thread.

This library currently targets a minimum of x86_64 processors with support for SSSE3 (Intel Core/AMD Bulldozer or newer), with optional optimizations for processors supporting POPCNT, LZCNT, BMI2, and/or AVX2. It is intended for use in implementations of Protocol Buffers (protobuf), Apache Avro, and similar serialization formats, but likely has many other applications.

Usage

Important: Ensure the Rust compiler has an appropriate target-cpu setting. An example is provided in .cargo/config.toml, but you may need to edit the file to specify the oldest CPUs your compiled binaries will support. Your project will not compile unless this is set correctly.

The native-optimizations feature should be enabled if and only if target-cpu is set to native, such as in the example. This enables some extra optimizations if suitable for your specific CPU. Read more below.

use varint_simd::{encode, decode, encode_zigzag, decode_zigzag};

fn main() {
  let num: u32 = 300;
  
  let encoded = encode::<u32>(num); // turbofish for demonstration purposes, usually not necessary
  // encoded now contains a tuple
  // (
  //    [0xAC, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], // encoded in a 128-bit vector
  //    2 // the number of bytes encoded
  // )
  
  let decoded = decode::<u32>(&encoded.0).unwrap();
  // decoded now contains another tuple:
  // (
  //    300, // the decoded number
  //    2 // the number of bytes read from the slice
  // )
  assert_eq!(decoded.0, num);
  
  // Signed integers can be encoded/decoded with convenience functions encode_zigzag and decode_zigzag
  let num: i32 = -20;
  let encoded = encode_zigzag::<i32>(num);
  let decoded = decode_zigzag::<i32>(&encoded.0).unwrap();
  assert_eq!(decoded.0, num);
}

The type parameter passed into the encode/decode functions greatly affects performance - the code takes shorter paths for shorter integers, and may exhibit comparatively poor performance if you're decoding a lot of tiny integers into u64's.

Safety

This crate uses a lot of unsafe code. Please exercise caution, although I do not expect there to be major issues.

There is also an optional "unsafe" interface for bypassing overflow and bounds checks. This can be used when you know your input data won't cause undefined behavior and your calling code can tolerate truncated numbers.

Benchmarks

The benchmarks below reflect the performance of decoding and encoding a sequence of random integers bounded by each integer size. All benchmarks are run with native optimizations. For more details, please see the source code for these benchmarks.

Intel Core i7-8850H "Coffee Lake" (2018 15" MacBook Pro)

benchmark graph

Decode

All numbers are in millions of integers per second.

varint-simd unsafevarint-simd saferustcinteger-encoding-rsprost
u8554.81283.26131.71116.59131.42
u16493.96349.74168.09121.35157.68
u32482.95332.11191.37120.16196.05
u64330.86277.6582.31580.32897.585
varint-simd 2xvarint-simd 4xvarint-simd 8x
u8658.52644.36896.32
u16547.39540.93
u32688.11

Encode

varint-simdrustcinteger-encoding-rsprost
u8383.01214.05126.6693.617
u16341.25181.18126.7985.014
u32360.87157.95125.0077.402
u64303.7272.66078.15346.456

AMD Ryzen 5 2600X @ 4.125 GHz "Zen+"

Decode

varint-simd unsafevarint-simd saferustcinteger-encoding-rsprost
u8537.51304.85152.35138.54124.44
u16403.39300.68170.31156.06147.83
u32293.88235.92160.48159.13150.05
u64229.28193.2875.82285.01083.407
varint-simd 2xvarint-simd 4xvarint-simd 8x
u8943.75808.451,106.50
u16721.01632.03
u32459.77

Encode

varint-simdrustcinteger-encoding-rsprost
u8362.97211.07142.1698.237
u16334.10172.09140.7896.480
u32288.19101.56126.2782.210
u64207.8952.51579.37548.088

TODO

  • Encoding multiple values at once
  • Faster decode for two u64 values with AVX2 (currently fairly slow)
  • Improve performance of "safe" interface
  • Parallel ZigZag decode/encode
  • Support for ARM NEON
  • Fallback scalar implementation
  • Further optimization (I'm pretty sure I left some performance on the table)

Contributions are welcome. 🙂

About the native-optimizations feature

This feature flag enables a build script that detects the current CPU and enables PDEP/PEXT optimizations if the CPU supports running these instructions efficiently. It should be enabled if and only if the target-cpu option is set to native.

This is necessary because AMD Zen, Zen+, and Zen 2 processors implement these instructions in microcode, which means they run much, much slower than if they were implemented in hardware. Additionally, Rust does not allow conditional compilation based on the target-cpu option, so it is necessary to specify this feature manually.

Library crates should not enable this feature by default. A separate feature flag should be provided to enable this feature in this crate.

Previous Work

  • Daniel Lemire, Nathan Kurz, Christoph Rupp - Stream VByte: Faster Byte-Oriented Integer Compression, Information Processing Letters 130, 2018: https://arxiv.org/abs/1709.08990
  • Jeff Plaisance, Nathan Kurz, Daniel Lemire - Vectorized VByte Decoding, International Symposium on Web Algorithms, 2015: https://arxiv.org/abs/1503.07387

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

leb128
rust
simd
varint

Project health

Overall

3.6/10

Maintained

0/10

Code review

1/10
OpenSSF Scorecard

crates.io

Latest release

0.4.1 · Sep 3, 2024

Releases this year

0

Significant stargazers

David Buchanan

1,703 followers · starred Feb 2024

Jay Feldblum

118 followers · starred Jan 2023

Raz Luvaton

158 followers · starred May 2025

Connor Skees

88 followers · starred Mar 2024