Support counters in v7 UUIDs #755

Merged
KodrAus merged 11 commits from feat/v7-counter into main 2024-06-23 18:31:14 -05:00
KodrAus commented 2024-05-12 22:25:51 -05:00 (Migrated from github.com)

For #717

This PR sketches out a solution to make the v7 support respect the input counter, while retaining existing semantics around filling with random data. It requires a bit of fudgery to keep compatibility with existing APIs, but I think the overall solution works out ok. I need to actually fill it all in fully and then explore ways to better integrate the existing Timestamp API, which may involve deprecating bits.

The gist of it is to add a usable_bits method to ClockSequence, so it can tell how many bits a given counter occupies. The UUID constructor can then do appropriate shifts and fill the rest with random bytes. That way a caller creating a v7 UUID can decide how many bits to dedicate to sequencing and how many to randomness.

As part of this PR I'm expecting to find some solution to re-seeding sequences, which will probably remain an implementation detail of the given ClockSequence.

For #717 This PR sketches out a solution to make the v7 support respect the input counter, while retaining existing semantics around filling with random data. It requires a bit of fudgery to keep compatibility with existing APIs, but I think the overall solution works out ok. I need to actually fill it all in fully and then explore ways to better integrate the existing `Timestamp` API, which may involve deprecating bits. The gist of it is to add a `usable_bits` method to `ClockSequence`, so it can tell how many bits a given counter occupies. The UUID constructor can then do appropriate shifts and fill the rest with random bytes. That way a caller creating a v7 UUID can decide how many bits to dedicate to sequencing and how many to randomness. As part of this PR I'm expecting to find some solution to re-seeding sequences, which will probably remain an implementation detail of the given `ClockSequence`.
KodrAus (Migrated from github.com) reviewed 2024-05-13 17:34:53 -05:00
@ -358,0 +475,4 @@
/// to adjust the timestamp on overflow.
#[derive(Debug)]
pub struct Context {
count: Atomic<u16>,
KodrAus (Migrated from github.com) commented 2024-05-13 17:34:52 -05:00

We need to add another implementation here with a 42bit counter that's intended to be used by v7 UUIDs.

We need to add another implementation here with a 42bit counter that's intended to be used by v7 UUIDs.
KodrAus (Migrated from github.com) reviewed 2024-05-13 17:35:49 -05:00
KodrAus (Migrated from github.com) commented 2024-05-13 17:35:49 -05:00

These examples should use Timestamp::from_unix_wide with a 42bit counter to steer users wanting counters in the right direction.

These examples should use `Timestamp::from_unix_wide` with a 42bit counter to steer users wanting counters in the right direction.
KodrAus commented 2024-06-19 18:19:43 -05:00 (Migrated from github.com)

I've got a counter implemented, but looks like performance here could use some improvement compared to uuid7:

running 2 tests
test new_v4   ... bench:          24.69 ns/iter (+/- 0.43)
test uuid7_v4 ... bench:          14.31 ns/iter (+/- 0.06)

test now_v7   ... bench:          56.86 ns/iter (+/- 1.04)
test uuid7_v7 ... bench:          38.03 ns/iter (+/- 0.59)
I've got a counter implemented, but looks like performance here could use some improvement compared to [`uuid7`](https://github.com/LiosK/uuid7-rs): ``` running 2 tests test new_v4 ... bench: 24.69 ns/iter (+/- 0.43) test uuid7_v4 ... bench: 14.31 ns/iter (+/- 0.06) test now_v7 ... bench: 56.86 ns/iter (+/- 1.04) test uuid7_v7 ... bench: 38.03 ns/iter (+/- 0.59) ```
KodrAus commented 2024-06-19 19:32:43 -05:00 (Migrated from github.com)

Ok, I think that's better now:

test new_v4   ... bench:           8.35 ns/iter (+/- 0.34)
test uuid7_v4 ... bench:          14.38 ns/iter (+/- 0.78)

test now_v7   ... bench:          36.51 ns/iter (+/- 2.60)
test uuid7_v7 ... bench:          38.31 ns/iter (+/- 2.35)
Ok, I think that's better now: ``` test new_v4 ... bench: 8.35 ns/iter (+/- 0.34) test uuid7_v4 ... bench: 14.38 ns/iter (+/- 0.78) test now_v7 ... bench: 36.51 ns/iter (+/- 2.60) test uuid7_v7 ... bench: 38.31 ns/iter (+/- 2.35) ```
KodrAus commented 2024-06-19 20:05:14 -05:00 (Migrated from github.com)

I need to add some tests for the new v7 context, and make sure we're shifting it around the variant field correctly.

I need to add some tests for the new v7 context, and make sure we're shifting it around the variant field correctly.
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-uuid-rs/uuid!755
No description provided.