184 releases (21 breaking)

new 0.23.1 Apr 25, 2025
0.22.1 Feb 20, 2025
0.21.0 Dec 18, 2024
0.20.2 Nov 28, 2024
0.4.0 Mar 28, 2023

#651 in Rust patterns

Download history 5866/week @ 2025-01-06 7084/week @ 2025-01-13 6671/week @ 2025-01-20 5933/week @ 2025-01-27 7485/week @ 2025-02-03 5492/week @ 2025-02-10 7083/week @ 2025-02-17 7324/week @ 2025-02-24 10772/week @ 2025-03-03 15296/week @ 2025-03-10 11686/week @ 2025-03-17 10991/week @ 2025-03-24 10608/week @ 2025-03-31 12833/week @ 2025-04-07 13062/week @ 2025-04-14 11502/week @ 2025-04-21

48,431 downloads per month
Used in 90 crates (8 directly)

MIT/Apache

41KB
557 lines

TUID: time-based unique identifier

Part of the rerun family of crates.

Latest version Documentation MIT Apache

TUID:s are 128-bit identifiers, that have a global time-based order, with tie-breaking between threads. This means you can use a TUID as a tie-breaker in time series databases.

Implementation

TUID is based on two fields, both of which are monotonically increasing:

  • time_ns: u64
  • inc: u64

time_ns is an approximate nanoseconds since unix epoch. It is monotonically increasing, though two TUID:s generated closely together may get the same time_ns.

inc is a monotonically increasing integer, initialized to some random number on each thread.

So the algorithm is this:

  • For each thread, generate a 64-bit random number as inc
  • When generating a new TUID:
    • increment the thread-local inc
    • get current time as time_ns
    • return TUID { time_ns, inc }

Performance

On a single core of a 2022 M1 MacBook we can generate 40 million TUID/s, which is 25 ns per TUID.

Future work

For time-based exploits (like Meltdown/Spectre) time_ns should probably be rounded to nearest millisecond for sensitive systems. The last ~20 bits of time_ns can be filled with more randomness to lessen the chance of collisions.

Dependencies

~17MB
~296K SLoC