Git Product home page Git Product logo

bitcode's Introduction

Bitcode

Documentation crates.io Build unsafe forbidden

A bitwise encoder/decoder similar to bincode, which attempts to shrink the serialized size without sacrificing speed (as would be the case with compression).

The format may change between major versions, so we are free to optimize it.

Comparison with bincode

Features

  • Bitwise serialization
  • Gamma encoded lengths and enum variant indices

Additional features with #[derive(bitcode::Encode, bitcode::Decode)]

  • Enums use the fewest possible bits, e.g. an enum with 4 variants uses 2 bits
  • Apply attributes to fields/enum variants:
Attribute Type Result
#[bitcode_hint(ascii)] String Uses 7 bits per character
#[bitcode_hint(ascii_lowercase)] String Uses 5 bits per character
#[bitcode_hint(expected_range = "50..100"] u8-u64 Uses log2(range.end - range.start) bits
#[bitcode_hint(expected_range = "0.0..1.0"] f32/f64 Uses ~25 bits for f32 and ~54 bits for f64
#[bitcode_hint(frequency = 123) enum variant Frequent variants use fewer bits (see Huffman coding)
#[bitcode_hint(gamma)] i8-i64/u8-u64 Small integers use fewer bits (see Elias gamma coding)
#[bitcode(with_serde)] T: Serialize Uses serde::Serialize instead of bitcode::Encode

Limitations

  • Doesn't support streaming APIs
  • Format may change between major versions
  • With feature = "derive", types containing themselves must use #[bitcode(recursive)] to compile

Benchmarks vs. bincode and postcard

Primitives (size in bits)

Type Bitcode (derive) Bitcode (serde) Bincode Bincode (varint) Postcard
bool 1 1 8 8 8
u8/i8 8 8 8 8 8
u16/i16 16 16 16 8-24 8-24
u32/i32 32 32 32 8-40 8-40
u64/i64 64 64 64 8-72 8-80
u128/i128 128 128 128 8-136 8-152
usize/isize 64 64 64 8-72 8-80
f32 32 32 32 32 32
f64 64 64 64 64 64
char 21 21 8-32 8-32 16-40
Option<()> 1 1 8 8 8
Result<(), ()> 1 1-3 32 8 8
enum { A, B, C, D } 2 1-5 32 8 8
Duration 94 96 96 16-112 16-120

Values (size in bits)

Value Bitcode (derive) Bitcode (serde) Bincode Bincode (varint) Postcard
[true; 4] 4 4 32 32 32
vec![(); 0] 1 1 64 8 8
vec![(); 1] 3 3 64 8 8
vec![(); 256] 17 17 64 24 16
vec![(); 65536] 33 33 64 40 24
"" 1 1 64 8 8
"abcd" 37 37 96 40 40
"abcd1234" 71 71 128 72 72

Random Structs and Enums (average size and speed)

Format Size (bytes) Serialize (ns) Deserialize (ns)
Bitcode (derive) 6.2 14 50
Bitcode (serde) 6.7 18 59
Bincode 20.3 17 61
Bincode (varint) 10.9 26 68
Bincode (LZ4) 9.9 58 73
Bincode (Deflate Fast) 8.4 336 279
Bincode (Deflate Best) 7.8 1990 275
Postcard 10.7 21 57

Acknowledgement

Some test cases were derived from bincode (see comment in tests.rs).

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.

bitcode's People

Contributors

caibear avatar finnbear avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.