]> Cypherpunks repositories - gostls13.git/commitdiff
time: make Duration.String inlineable
authorJoe Tsai <joetsai@digital-static.net>
Fri, 18 Aug 2023 01:13:01 +0000 (18:13 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 18 Aug 2023 23:45:44 +0000 (23:45 +0000)
Perform the [32]byte to string conversion in an inlinable method.
Thus, if the result does not escape in the context of the caller,
we can entirely avoid a call to runtime.slicebytetostring.

Change-Id: Iae8ec2a532776ed6cf99597f19e3f7f21c694c3a
Reviewed-on: https://go-review.googlesource.com/c/go/+/520602
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/time/time.go

index 8570635e2a3d145d8824dc1c3ec1f6c8fb1414e7..cc9dd6652af41931b879cad20c54d6b61be35ac9 100644 (file)
@@ -650,8 +650,17 @@ const (
 // second format use a smaller unit (milli-, micro-, or nanoseconds) to ensure
 // that the leading digit is non-zero. The zero duration formats as 0s.
 func (d Duration) String() string {
+       // This is inlinable to take advantage of "function outlining".
+       // Thus, the caller can decide whether a string must be heap allocated.
+       var arr [32]byte
+       n := d.format(&arr)
+       return string(arr[n:])
+}
+
+// format formats the representation of d into the end of buf and
+// returns the offset of the first character.
+func (d Duration) format(buf *[32]byte) int {
        // Largest time is 2540400h10m10.000000000s
-       var buf [32]byte
        w := len(buf)
 
        u := uint64(d)
@@ -669,7 +678,8 @@ func (d Duration) String() string {
                w--
                switch {
                case u == 0:
-                       return "0s"
+                       buf[w] = '0'
+                       return w
                case u < uint64(Microsecond):
                        // print nanoseconds
                        prec = 0
@@ -719,7 +729,7 @@ func (d Duration) String() string {
                buf[w] = '-'
        }
 
-       return string(buf[w:])
+       return w
 }
 
 // fmtFrac formats the fraction of v/10**prec (e.g., ".12345") into the