6 releases

0.1.10 Oct 17, 2023
0.1.9 Oct 17, 2023
0.1.8 Oct 6, 2022

#545 in Development tools

Download history 1/week @ 2024-02-21 14/week @ 2024-02-28 2/week @ 2024-03-06 164/week @ 2024-03-13

180 downloads per month

MIT/Apache

21KB
402 lines

snowflake-rs

A simple to use rust package to generate or parse Twitter snowflake IDs,generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)

Build crates.io docs.rs License

A distributed unique ID generator inspired by Twitter's Snowflake.

This is a Rust implementation of the original houseme/snowflake, which is written in Go.

A Snowflake ID is composed of

39 bits for time in units of 10 msec 8 bits for a sequence number 16 bits for a machine id

Install

Add the following to your Cargo.toml:

[dependencies]
snowflake_me = "0.1"

Quickstart

use snowflake_me::Snowflake;

let sf = Snowflake::new().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);

Benchmarks

Run them yourself with cargo bench.

1, Benchmarks were run on a MacBook Pro (16-inch, 2019) with a 2,4GHz i9 and 64 GB memory.

test bench_decompose ... bench:         651 ns/iter (+/- 251)
test bench_new       ... bench:     795,722 ns/iter (+/- 371,556)
test bench_next_id   ... bench:      36,652 ns/iter (+/- 1,105)

2, Benchmarks were run on a MacBook Pro (15-inch, 2017) with a 2,8GHz i7 and 16 GB memory.

test bench_decompose ... bench:       1,066 ns/iter (+/- 132)
test bench_new       ... bench:     738,129 ns/iter (+/- 318,192)
test bench_next_id   ... bench:      37,390 ns/iter (+/- 499)

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.

Dependencies

~4–5.5MB
~98K SLoC