Skip to content

Commit fc4857e

Browse files
committed
runtime: avoid recursion in printuint64 function
This function is called from runtime.printitf, which is called from runtime._panic, and is therefore the leaf function of many call paths. This makes analyzing stack usage very difficult. Also forwarding printuint32 to printuint64 as it reduces code size in the few examples I've tested. Printing numbers is not often done so it doesn't matter if it's a bit slow (the serial connection is probably slower anyway).
1 parent f66492a commit fc4857e

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/runtime/print.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,8 @@ func printint16(n int16) {
4747
printint32(int32(n))
4848
}
4949

50-
//go:nobounds
5150
func printuint32(n uint32) {
52-
digits := [10]byte{} // enough to hold (2^32)-1
53-
// Fill in all 10 digits.
54-
firstdigit := 9 // digit index that isn't zero (by default, the last to handle '0' correctly)
55-
for i := 9; i >= 0; i-- {
56-
digit := byte(n%10 + '0')
57-
digits[i] = digit
58-
if digit != '0' {
59-
firstdigit = i
60-
}
61-
n /= 10
62-
}
63-
// Print digits without the leading zeroes.
64-
for i := firstdigit; i < 10; i++ {
65-
putchar(digits[i])
66-
}
51+
printuint64(uint64(n))
6752
}
6853

6954
func printint32(n int32) {
@@ -76,12 +61,23 @@ func printint32(n int32) {
7661
printuint32(uint32(n))
7762
}
7863

64+
//go:nobounds
7965
func printuint64(n uint64) {
80-
prevdigits := n / 10
81-
if prevdigits != 0 {
82-
printuint64(prevdigits)
66+
digits := [20]byte{} // enough to hold (2^64)-1
67+
// Fill in all 10 digits.
68+
firstdigit := 19 // digit index that isn't zero (by default, the last to handle '0' correctly)
69+
for i := 19; i >= 0; i-- {
70+
digit := byte(n%10 + '0')
71+
digits[i] = digit
72+
if digit != '0' {
73+
firstdigit = i
74+
}
75+
n /= 10
76+
}
77+
// Print digits without the leading zeroes.
78+
for i := firstdigit; i < 20; i++ {
79+
putchar(digits[i])
8380
}
84-
putchar(byte((n % 10) + '0'))
8581
}
8682

8783
func printint64(n int64) {

0 commit comments

Comments
 (0)