Skip to content

Commit aea4410

Browse files
committed
strconv: replace small int string table with constant string
This reduces memory use yet still provides the significant performance gain seen when using a fast path for small integers. Improvement of this CL comparing to code without fast path: name old time/op new time/op delta FormatIntSmall-8 35.6ns ± 1% 4.5ns ± 1% -87.30% (p=0.008 n=5+5) AppendIntSmall-8 17.4ns ± 1% 9.4ns ± 3% -45.70% (p=0.008 n=5+5) For comparison, here's the improvement before this CL to code without fast path (1% better for FormatIntSmall): name old time/op new time/op delta FormatIntSmall-8 35.6ns ± 1% 4.0ns ± 3% -88.64% (p=0.008 n=5+5) AppendIntSmall-8 17.4ns ± 1% 8.2ns ± 1% -52.80% (p=0.008 n=5+5) Thus, the code in this CL performs slower for small integers using fast path then the prior version, but this is relative to an already very fast version: name old time/op new time/op delta FormatIntSmall-8 4.05ns ± 3% 4.52ns ± 1% +11.81% (p=0.008 n=5+5) AppendIntSmall-8 8.21ns ± 1% 9.45ns ± 3% +15.05% (p=0.008 n=5+5) Measured on 2.3 GHz Intel Core i7 running macOS Sierra 10.12.3. Overall, it's still ~88% faster than without fast path for small integers, so probably worth it as it removes 100 global string slices in favor of a single string. Credits: This is based on the original (but cleaned up) version of the code by Aliaksandr Valialkin (https://go-review.googlesource.com/c/37963/). Change-Id: Icda78679c8c14666d46257894e9fa3d7f35e58b8 Reviewed-on: https://go-review.googlesource.com/38319 Reviewed-by: Martin Möhrmann <[email protected]>
1 parent b744a11 commit aea4410

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/strconv/itoa.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
package strconv
66

7+
const fastSmalls = true // enable fast path for small integers
8+
79
// FormatUint returns the string representation of i in the given base,
810
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
911
// for digit values >= 10.
1012
func FormatUint(i uint64, base int) string {
11-
if i < uint64(len(smallints)) && base == 10 {
12-
return smallints[i]
13+
if fastSmalls && i < nSmalls && base == 10 {
14+
return small(int(i))
1315
}
1416
_, s := formatBits(nil, i, base, false, false)
1517
return s
@@ -19,8 +21,8 @@ func FormatUint(i uint64, base int) string {
1921
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
2022
// for digit values >= 10.
2123
func FormatInt(i int64, base int) string {
22-
if 0 <= i && i < int64(len(smallints)) && base == 10 {
23-
return smallints[i]
24+
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
25+
return small(int(i))
2426
}
2527
_, s := formatBits(nil, uint64(i), base, i < 0, false)
2628
return s
@@ -34,8 +36,8 @@ func Itoa(i int) string {
3436
// AppendInt appends the string form of the integer i,
3537
// as generated by FormatInt, to dst and returns the extended buffer.
3638
func AppendInt(dst []byte, i int64, base int) []byte {
37-
if 0 <= i && i < int64(len(smallints)) && base == 10 {
38-
return append(dst, smallints[i]...)
39+
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
40+
return append(dst, small(int(i))...)
3941
}
4042
dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
4143
return dst
@@ -44,30 +46,39 @@ func AppendInt(dst []byte, i int64, base int) []byte {
4446
// AppendUint appends the string form of the unsigned integer i,
4547
// as generated by FormatUint, to dst and returns the extended buffer.
4648
func AppendUint(dst []byte, i uint64, base int) []byte {
47-
if i < uint64(len(smallints)) && base == 10 {
48-
return append(dst, smallints[i]...)
49+
if fastSmalls && i < nSmalls && base == 10 {
50+
return append(dst, small(int(i))...)
4951
}
5052
dst, _ = formatBits(dst, i, base, false, true)
5153
return dst
5254
}
5355

56+
// small returns the string for an i with 0 <= i < nSmalls.
57+
func small(i int) string {
58+
off := 0
59+
if i < 10 {
60+
off = 1
61+
}
62+
return smallsString[i*2+off : i*2+2]
63+
}
64+
65+
const nSmalls = 100
66+
67+
const smallsString = "00010203040506070809" +
68+
"10111213141516171819" +
69+
"20212223242526272829" +
70+
"30313233343536373839" +
71+
"40414243444546474849" +
72+
"50515253545556575859" +
73+
"60616263646566676869" +
74+
"70717273747576777879" +
75+
"80818283848586878889" +
76+
"90919293949596979899"
77+
5478
const host32bit = ^uint(0)>>32 == 0
5579

5680
const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
5781

58-
var smallints = [...]string{
59-
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
60-
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
61-
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
62-
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
63-
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
64-
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
65-
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
66-
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
67-
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
68-
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99",
69-
}
70-
7182
var shifts = [len(digits) + 1]uint{
7283
1 << 1: 1,
7384
1 << 2: 2,

0 commit comments

Comments
 (0)