]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: avoid append in printint, printuint
authorRuss Cox <rsc@golang.org>
Mon, 3 Nov 2025 20:35:26 +0000 (15:35 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 4 Nov 2025 00:37:04 +0000 (16:37 -0800)
Should make cmd/link/internal/ld.TestAbstractOriginSanity happier.

Change-Id: I121927d42e527ff23d996e7387066f149b11cc59
Reviewed-on: https://go-review.googlesource.com/c/go/+/717480
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/internal/strconv/itoa.go
src/runtime/print.go

index d06de4770f10526fd0ec340952c6795efa7c9e28..2375e034f5978691d5f48974dbeb69880a71d32c 100644 (file)
@@ -174,6 +174,14 @@ func small(i int) string {
        return smalls[i*2 : i*2+2]
 }
 
+// RuntimeFormatBase10 formats u into the tail of a
+// and returns the offset to the first byte written to a.
+// It is only for use by package runtime.
+// Other packages should use AppendUint.
+func RuntimeFormatBase10(a []byte, u uint64) int {
+       return formatBase10(a, u)
+}
+
 // formatBase10 formats the decimal representation of u into the tail of a
 // and returns the offset of the first byte written to a. That is, after
 //
index e32ecb94503e63696bb35a726ea9b2374d94200b..c01db9d7f9868983a8ed09776dec530cf98e6f59 100644 (file)
@@ -140,13 +140,32 @@ func printcomplex64(c complex64) {
 }
 
 func printuint(v uint64) {
+       // Note: Avoiding strconv.AppendUint so that it's clearer
+       // that there are no allocations in this routine.
+       // cmd/link/internal/ld.TestAbstractOriginSanity
+       // sees the append and doesn't realize it doesn't allocate.
        var buf [20]byte
-       gwrite(strconv.AppendUint(buf[:0], v, 10))
+       i := strconv.RuntimeFormatBase10(buf[:], v)
+       gwrite(buf[i:])
 }
 
 func printint(v int64) {
+       // Note: Avoiding strconv.AppendUint so that it's clearer
+       // that there are no allocations in this routine.
+       // cmd/link/internal/ld.TestAbstractOriginSanity
+       // sees the append and doesn't realize it doesn't allocate.
+       neg := v < 0
+       u := uint64(v)
+       if neg {
+               u = -u
+       }
        var buf [20]byte
-       gwrite(strconv.AppendInt(buf[:0], v, 10))
+       i := strconv.RuntimeFormatBase10(buf[:], u)
+       if neg {
+               i--
+               buf[i] = '-'
+       }
+       gwrite(buf[i:])
 }
 
 var minhexdigits = 0 // protected by printlock