From 446783960277251fcb837f3672f377469d204918 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 10 Aug 2023 11:56:53 -0400 Subject: [PATCH] time: make time.Since a few nanoseconds faster time.Since(base) is an idiom that can be used to read the system monotonic time as efficiently as possible, when that matters. The current code structure adds a few nanoseconds on top of the 15-20ns the time read already takes. Remove those few. After this CL, there is no reason at all for anyone to //go:linkname runtime.nanotime1 instead. Came up while investigating #61765. Change-Id: Ic9e688af039babfc2a5a8e67dcbb02847a5eb686 Reviewed-on: https://go-review.googlesource.com/c/go/+/518336 TryBot-Result: Gopher Robot Reviewed-by: Damien Neil Run-TryBot: Russ Cox Reviewed-by: Joseph Tsai --- src/time/time.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/time/time.go b/src/time/time.go index 00c6d6423f..8570635e2a 100644 --- a/src/time/time.go +++ b/src/time/time.go @@ -891,16 +891,7 @@ func (t Time) Add(d Duration) Time { // To compute t-d for a duration d, use t.Add(-d). func (t Time) Sub(u Time) Duration { if t.wall&u.wall&hasMonotonic != 0 { - te := t.ext - ue := u.ext - d := Duration(te - ue) - if d < 0 && te > ue { - return maxDuration // t - u is positive out of range - } - if d > 0 && te < ue { - return minDuration // t - u is negative out of range - } - return d + return subMono(t.ext, u.ext) } d := Duration(t.sec()-u.sec())*Second + Duration(t.nsec()-u.nsec()) // Check for overflow or underflow. @@ -914,17 +905,25 @@ func (t Time) Sub(u Time) Duration { } } +func subMono(t, u int64) Duration { + d := Duration(t - u) + if d < 0 && t > u { + return maxDuration // t - u is positive out of range + } + if d > 0 && t < u { + return minDuration // t - u is negative out of range + } + return d +} + // Since returns the time elapsed since t. // It is shorthand for time.Now().Sub(t). func Since(t Time) Duration { - var now Time if t.wall&hasMonotonic != 0 { // Common case optimization: if t has monotonic time, then Sub will use only it. - now = Time{hasMonotonic, runtimeNano() - startNano, nil} - } else { - now = Now() + return subMono(runtimeNano()-startNano, t.ext) } - return now.Sub(t) + return Now().Sub(t) } // Until returns the duration until t. -- 2.50.0