-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
masto-id-convert
crate to monorepo
- Loading branch information
Showing
28 changed files
with
353 additions
and
133 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "masto-id-convert" | ||
version.workspace = true | ||
edition.workspace = true | ||
|
||
[[bench]] | ||
name = "process" | ||
harness = false | ||
|
||
[dependencies] | ||
lexical = { version = "6.1.1", default-features = false, features = [ | ||
"parse-integers", | ||
] } | ||
nanorand = { version = "0.7.0", default-features = false, features = [ | ||
"wyrand", | ||
] } | ||
uuid = { version = "1.5.0", default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [] | ||
|
||
[dev-dependencies] | ||
criterion = { version = "0.5.1", default-features = false } | ||
time = "0.3.30" | ||
uuid = { version = "1.5.0", features = ["v7"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# masto-id-convert | ||
|
||
Convert a Mastodon snowflake ID into a UUID v7 while preserving the timestamp component. Fast. | ||
|
||
## How? | ||
|
||
The unix timestamp is preserved as-is, the 2-bytes sequence is stretched out via the WyRand PRNG algorithm. | ||
|
||
## Performance | ||
|
||
Tested inside a WSL2 installation on a Ryzen 5 3600X: | ||
|
||
```text | ||
process 110368129515784116 | ||
time: [16.675 ns 16.822 ns 17.037 ns] | ||
change: [-1.2226% -0.3915% +0.4911%] (p = 0.37 > 0.05) | ||
``` | ||
|
||
Processing a single Mastodon snowflake takes ~17ns | ||
|
||
## License | ||
|
||
`masto-id-convert` is licensed under the [MIT license](http://opensource.org/licenses/MIT). | ||
|
||
### Contribution | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, | ||
shall be licensed as above, without any additional terms or conditions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
|
||
fn process_bench(c: &mut Criterion) { | ||
c.bench_function("process integer 110368129515784116", |b| { | ||
b.iter(|| masto_id_convert::process_u64(black_box(110368129515784116))) | ||
}); | ||
|
||
c.bench_function("process ASCII 110368129515784116", |b| { | ||
b.iter(|| masto_id_convert::process(black_box("110368129515784116"))) | ||
}); | ||
} | ||
|
||
criterion_group!(benches, process_bench); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use time::OffsetDateTime; | ||
|
||
fn main() { | ||
let uuid = masto_id_convert::process("110368129515784116").unwrap(); | ||
println!("Converted UUID: {uuid}"); | ||
println!("UUID version: {}", uuid.get_version_num()); | ||
println!("UUID variant: {}", uuid.get_variant()); | ||
|
||
let (timestamp_seconds, timestamp_nanos) = uuid.get_timestamp().unwrap().to_unix(); | ||
let timestamp_nanos = ((timestamp_seconds as i128) * 1_000_000_000) + (timestamp_nanos as i128); | ||
let uuid_timestamp = OffsetDateTime::from_unix_timestamp_nanos(timestamp_nanos).unwrap(); | ||
|
||
println!("Embedded timestamp: {uuid_timestamp}"); | ||
} |
Oops, something went wrong.