122 releases (15 breaking)

new 0.17.0 Jul 8, 2024
0.17.0-alpha.8 Jun 25, 2024
0.15.0-alpha.5 Mar 29, 2024
0.12.0-alpha.2 Dec 26, 2023
0.4.0 Mar 28, 2023

#648 in Rust patterns

Download history 3986/week @ 2024-03-17 3056/week @ 2024-03-24 3445/week @ 2024-03-31 5474/week @ 2024-04-07 5254/week @ 2024-04-14 5783/week @ 2024-04-21 4691/week @ 2024-04-28 4374/week @ 2024-05-05 8436/week @ 2024-05-12 7903/week @ 2024-05-19 5541/week @ 2024-05-26 6269/week @ 2024-06-02 6236/week @ 2024-06-09 7685/week @ 2024-06-16 8599/week @ 2024-06-23 3847/week @ 2024-06-30

26,494 downloads per month
Used in 52 crates (5 directly)

MIT/Apache

16KB
155 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

~285–770KB
~14K SLoC