Conversation
🦋 Changeset detectedLatest commit: ad92465 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR 💥 An error occurred when fetching the changed packages and changesets in this PR |
| // Now creates a monotonic time without reading the system wall clock | ||
| func Now() time.Time { | ||
| return time.Now() | ||
| return time.Unix(0, epochNano+int64(time.Since(epoch))) |
There was a problem hiding this comment.
time.Since skips reading the wall clock time so it's ~30% faster than time.Now
pkg: github.com/livekit/protocol/utils/mono
cpu: Apple M1 Pro
BenchmarkTime/Now()-10 40969438 24.71 ns/op
BenchmarkTime/time.Now()-10 34145271 34.97 ns/op
There was a problem hiding this comment.
The only concern is that it requires the use of mono.Now() to properly account for time jumps, right?
Previously, any value produced by time.Now() would still work, since it had monotonic field set. Only requirement was that timestamps from API/files should be passed through FromTime to capture the monotonic offset (from epoch).
The nice thing about the previous approach was that it's idiomatic, one may still use stdlib for everything, except getting timestamps from external sources.
There was a problem hiding this comment.
mono.FromTime(time.Now()) still works but yeah, for times to be comparable they have to have been updated here. we could define a monotonic time type (int64) and reimplement the time.Time api but idk... i think anyone reaching for this can be trusted to use it correctly
| return time.Time{} | ||
| } | ||
| return epoch.Add(t.Sub(epoch)) | ||
| return time.Unix(0, epochNano+int64(t.Sub(epoch))) |
There was a problem hiding this comment.
to avoid any confusion we store the epoch relative monotonic time in the wall clock field of a time without the hasMonotonic flag so normal time mutation apis work and the time remains comparable to other mono produced times
| // Now creates a monotonic time without reading the system wall clock | ||
| func Now() time.Time { | ||
| return time.Now() | ||
| return time.Unix(0, epochNano+int64(time.Since(epoch))) |
No description provided.