20 releases (4 breaking)

new 0.6.0 May 25, 2023
0.5.1 May 3, 2023
0.5.0 Apr 20, 2023
0.4.0 Mar 28, 2023
0.2.0 Feb 14, 2023

#197 in Images

Download history 361/week @ 2023-02-10 166/week @ 2023-02-17 95/week @ 2023-02-24 162/week @ 2023-03-03 75/week @ 2023-03-10 65/week @ 2023-03-17 88/week @ 2023-03-24 56/week @ 2023-03-31 55/week @ 2023-04-07 127/week @ 2023-04-14 55/week @ 2023-04-21 207/week @ 2023-04-28 58/week @ 2023-05-05 30/week @ 2023-05-12 21/week @ 2023-05-19

322 downloads per month
Used in 16 crates (2 directly)

MIT/Apache

9KB
116 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

~0.3–1.5MB
~31K SLoC