Skip to content

Commit e2c6a20

Browse files
committed
runtime: avoid append in printint, printuint
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>
1 parent c93cc60 commit e2c6a20

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/internal/strconv/itoa.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ func small(i int) string {
174174
return smalls[i*2 : i*2+2]
175175
}
176176

177+
// RuntimeFormatBase10 formats u into the tail of a
178+
// and returns the offset to the first byte written to a.
179+
// It is only for use by package runtime.
180+
// Other packages should use AppendUint.
181+
func RuntimeFormatBase10(a []byte, u uint64) int {
182+
return formatBase10(a, u)
183+
}
184+
177185
// formatBase10 formats the decimal representation of u into the tail of a
178186
// and returns the offset of the first byte written to a. That is, after
179187
//

src/runtime/print.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,32 @@ func printcomplex64(c complex64) {
140140
}
141141

142142
func printuint(v uint64) {
143+
// Note: Avoiding strconv.AppendUint so that it's clearer
144+
// that there are no allocations in this routine.
145+
// cmd/link/internal/ld.TestAbstractOriginSanity
146+
// sees the append and doesn't realize it doesn't allocate.
143147
var buf [20]byte
144-
gwrite(strconv.AppendUint(buf[:0], v, 10))
148+
i := strconv.RuntimeFormatBase10(buf[:], v)
149+
gwrite(buf[i:])
145150
}
146151

147152
func printint(v int64) {
153+
// Note: Avoiding strconv.AppendUint so that it's clearer
154+
// that there are no allocations in this routine.
155+
// cmd/link/internal/ld.TestAbstractOriginSanity
156+
// sees the append and doesn't realize it doesn't allocate.
157+
neg := v < 0
158+
u := uint64(v)
159+
if neg {
160+
u = -u
161+
}
148162
var buf [20]byte
149-
gwrite(strconv.AppendInt(buf[:0], v, 10))
163+
i := strconv.RuntimeFormatBase10(buf[:], u)
164+
if neg {
165+
i--
166+
buf[i] = '-'
167+
}
168+
gwrite(buf[i:])
150169
}
151170

152171
var minhexdigits = 0 // protected by printlock

0 commit comments

Comments
 (0)