uuid6 / uuid6-ietf-draft

Next Generation UUID Formats
https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
187 stars 11 forks source link

v7: Add example layout that employs exotic precision timestamp #57

Closed LiosK closed 2 years ago

LiosK commented 2 years ago

One of the benefits of the sub-second fraction representation is that we no longer need to stick to decimal clock resolutions like msec, usec, and nsec. We can employ some exotic precision resolutions such as ~0.25 milliseconds (1 / 4096 seconds) and ~5 microseconds. Please take a look at the following quick C code that prints the leading 48 bits of a UUIDv7:

#include <stdint.h>
#include <stdio.h>
#include <time.h>

int main() {
  struct timespec tp;
  clock_gettime(CLOCK_REALTIME, &tp);
  uint64_t timestamp = (uint64_t)tp.tv_sec << 12;

  // compute 12-bit (~0.25 msec precision) fraction from nsecs
  timestamp |= ((uint64_t)tp.tv_nsec << 12) / 1000000000;

  printf("%08llx-%04llx\n", timestamp >> 16, timestamp & 0xFFFF);
  return 0;
}

The above code truncates microsecond-precision (in my environment) sub-seconds to ~0.25-millisecond precision in order to fully utilize the 12-bit subsec_a space. With the sub-second fraction representation, truncating a decimal timestamp to a convenient size is always a good option to pack the timestamp into a binary field efficiently.

I think the RFC should include this approach in one way or another because, though some implementers might invent similar techniques, many might naively implement the microsecond-precision example layout as in the draft 02 and just waste ~4 bits. Also, this approach might help resolve the endless debate over the length of sub-second fields like #24.

LiosK commented 2 years ago

For reference: simple stateless UUIDv7 implementation that utilizes 16-bit (~15 usec) precision sub-second fraction in C

kyzer-davis commented 2 years ago

@LiosK, with the current state of Draft 03 and UUIDv7 does this still have any place in the document?

LiosK commented 2 years ago

Nope. This issue should be closed together with the 03 PR.

I added a comment re extra precision, and this issue might be relevant to the extra timestamp precision component.

kyzer-davis commented 2 years ago

Thanks @LiosK,

This could be re-used as a sample use-case for UUIDv8. That way I have some code to display at least one way to utilize UUIDv8 for an exotic timestamps somebody cooks up.