170 releases (19 breaking)

0.21.0 Dec 18, 2024
0.20.3 Dec 3, 2024
0.20.2 Nov 28, 2024
0.17.0 Jul 8, 2024
0.4.0 Mar 28, 2023

#480 in Rust patterns

Download history 7053/week @ 2024-09-23 3659/week @ 2024-09-30 5124/week @ 2024-10-07 7063/week @ 2024-10-14 4752/week @ 2024-10-21 5046/week @ 2024-10-28 6287/week @ 2024-11-04 6526/week @ 2024-11-11 6486/week @ 2024-11-18 5920/week @ 2024-11-25 7166/week @ 2024-12-02 6201/week @ 2024-12-09 6459/week @ 2024-12-16 3290/week @ 2024-12-23 3882/week @ 2024-12-30 5273/week @ 2025-01-06

19,320 downloads per month
Used in 81 crates (4 directly)

MIT/Apache

105KB
2K SLoC

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

~0.8–9MB
~79K SLoC