Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper timestamp calculation for UUID v7 #199

Merged
merged 10 commits into from
Feb 10, 2025
Merged

Conversation

kohenkatz
Copy link
Contributor

Fixes #195.

The use of the time.Time method UnixNanos() was causing an int64 overflow (officially an "undefined behavior"), and returning incorrect timestamp values.

There wasn't any good reason for using this function, since the goal is to convert milliseconds to 100-nanosecond intervals, which can be done much more simply with a single multiplication step.


This might also provide a slight performance improvement (see #128 (comment)), since time.UnixMilli, the (unneeded) call to .UTC(), and .UnixNano() are each doing a fair bit of work internally, and they have all been replaced with a single multiplication.

Copy link

codecov bot commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (7d51018) to head (240f296).
Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #199   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines          545       548    +3     
=========================================
+ Hits           545       548    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you potentially accept a couple of benchmarks if I wrote them?

uuid_test.go Show resolved Hide resolved
@kohenkatz
Copy link
Contributor Author

Would you potentially accept a couple of benchmarks if I wrote them?

Sure!

@nathanmcgarvey-modopayments
Copy link
Contributor

nathanmcgarvey-modopayments commented Feb 9, 2025

Would you potentially accept a couple of benchmarks if I wrote them?

Sure!

kohenkatz#1

Edit: example results:

% GOMAXPROCS=2 go test -benchmem -run='^$' -bench '^.*$' github.com/gofrs/uuid/v5 -count=1

...

BenchmarkString-2              	48096272	        25.09 ns/op	      48 B/op	       1 allocs/op
BenchmarkFromBytes-2           	548770134	         2.196 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString/canonical-2         	44218914	        26.68 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString/urn-2               	44909268	        27.70 ns/op	       0 B/op	       0 allocs/op
BenchmarkFromString/braced-2            	44746203	        28.34 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalText/canonical-2      	45163292	        24.64 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalText/urn-2            	49905127	        25.04 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnmarshalText/braced-2         	49757602	        25.67 ns/op	       0 B/op	       0 allocs/op
BenchmarkMarshalBinary-2                	1000000000	         0.2793 ns/op	       0 B/op	       0 allocs/op
BenchmarkMarshalText-2                  	46007859	        25.41 ns/op	      48 B/op	       1 allocs/op
BenchmarkParseV4-2                      	46078599	        26.23 ns/op	       0 B/op	       0 allocs/op
BenchmarkGenerator/NewV1-2              	29537401	        41.99 ns/op	       0 B/op	       0 allocs/op
BenchmarkGenerator/NewV3-2              	 8588953	       143.6 ns/op	     144 B/op	       4 allocs/op
BenchmarkGenerator/NewV4-2              	 5222703	       222.4 ns/op	      16 B/op	       1 allocs/op
BenchmarkGenerator/NewV5-2              	12039100	        98.06 ns/op	     168 B/op	       4 allocs/op
BenchmarkGenerator/NewV6-2              	10953751	       112.0 ns/op	      16 B/op	       1 allocs/op
BenchmarkGenerator/NewV7-2              	10618904	       112.5 ns/op	      16 B/op	       1 allocs/op
BenchmarkNullMarshalJSON/Valid-2        	49983598	        24.79 ns/op	      48 B/op	       1 allocs/op
BenchmarkNullMarshalJSON/Invalid-2      	808741824	         1.426 ns/op	       0 B/op	       0 allocs/op
BenchmarkNullUnmarshalJSON/Valid-2      	48172698	        25.91 ns/op	       0 B/op	       0 allocs/op
BenchmarkNullUnmarshalJSON/Invalid-2    	862909197	         1.425 ns/op	       0 B/op	       0 allocs/op
BenchmarkFormat/s-2                     	18783224	        64.31 ns/op	      48 B/op	       1 allocs/op
BenchmarkFormat/S-2                     	14433289	        87.20 ns/op	      48 B/op	       1 allocs/op
BenchmarkFormat/q-2                     	17705152	        67.74 ns/op	      48 B/op	       1 allocs/op
BenchmarkFormat/x-2                     	19046082	        63.62 ns/op	      32 B/op	       1 allocs/op
BenchmarkFormat/X-2                     	14180890	        84.69 ns/op	      32 B/op	       1 allocs/op
BenchmarkFormat/v-2                     	16723038	        64.04 ns/op	      48 B/op	       1 allocs/op
BenchmarkFormat/+v-2                    	17415081	        65.94 ns/op	      48 B/op	       1 allocs/op
BenchmarkFormat/#v-2                    	 2197572	       535.6 ns/op	      16 B/op	       1 allocs/op
BenchmarkTimestampFrom/v1-2             	361338176	         2.841 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimestampFrom/v6-2             	386625184	         2.759 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimestampFrom/v7-2             	387401918	         2.801 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimestampTime/v1-2             	588414607	         1.732 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimestampTime/v6-2             	583686450	         1.731 ns/op	       0 B/op	       0 allocs/op
BenchmarkTimestampTime/v7-2             	587475630	         1.731 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/gofrs/uuid/v5	85.912s

Copy link
Member

@dylan-bourque dylan-bourque left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a few comments and a suggestion but LGTM otherwise

generator_test.go Outdated Show resolved Hide resolved
uuid.go Outdated Show resolved Hide resolved
uuid_test.go Outdated Show resolved Hide resolved
uuid_test.go Outdated Show resolved Hide resolved
uuid_test.go Outdated Show resolved Hide resolved
@kohenkatz
Copy link
Contributor Author

@dylan-bourque Updated with your suggestions.

uuid.go Outdated Show resolved Hide resolved
uuid.go Outdated Show resolved Hide resolved
@kohenkatz kohenkatz merged commit 142fcab into gofrs:master Feb 10, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TimestampFromUUIDv7 Artificially Limited
3 participants