100 releases (13 breaking)

0.15.1 Apr 11, 2024
0.15.0-alpha.5 Mar 29, 2024
0.12.0-alpha.2 Dec 26, 2023
0.11.0 Nov 28, 2023
0.4.0 Mar 28, 2023

#614 in Rust patterns

Download history 1216/week @ 2024-01-05 802/week @ 2024-01-12 629/week @ 2024-01-19 939/week @ 2024-01-26 1244/week @ 2024-02-02 1582/week @ 2024-02-09 3651/week @ 2024-02-16 4661/week @ 2024-02-23 5611/week @ 2024-03-01 4836/week @ 2024-03-08 4512/week @ 2024-03-15 3093/week @ 2024-03-22 3088/week @ 2024-03-29 4849/week @ 2024-04-05 5803/week @ 2024-04-12 4426/week @ 2024-04-19

18,886 downloads per month
Used in 42 crates (3 directly)

MIT/Apache

11KB
145 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

~290–790KB
~14K SLoC