Skip to content

Commit 8509192

Browse files
committed
optimize the performance of the function appendFormatRFC3339
1 parent 719bb7c commit 8509192

File tree

4 files changed

+19
-69
lines changed

4 files changed

+19
-69
lines changed

src/time/export_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ var StdChunkNames = map[int]string{
134134
var Quote = quote
135135

136136
var AppendInt = appendInt
137-
var AppendIntWidth2 = appendIntWidth2
138137
var AppendIntWidth4 = appendIntWidth4
139138
var AppendFormatAny = Time.appendFormat
140139
var AppendFormatRFC3339 = Time.appendFormatRFC3339

src/time/format.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -464,34 +464,16 @@ func appendInt(b []byte, x int, width int) []byte {
464464
return b
465465
}
466466

467-
const unitsDigit = "01234567890123456789012345678901234567890123456789" +
468-
"01234567890123456789012345678901234567890123456789"
469-
const tensDigit = "00000000001111111111222222222233333333334444444444" +
470-
"55555555556666666666777777777788888888889999999999"
471-
472-
// appendIntWidth2 special scenario for appendInt, with parameter width=2
473-
// Only applicable to integers with absolute value less than 100
474-
func appendIntWidth2(b []byte, x int) []byte {
475-
if x < 0 {
476-
b = append(b, '-')
477-
x = -x
478-
}
479-
if x >= 1e2 {
480-
x %= 1e2
481-
}
482-
return append(b, tensDigit[x], unitsDigit[x])
483-
}
467+
const onesDigit = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
468+
const tensDigit = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
484469

485-
// appendIntWidth4 special scenario for appendInt, with parameter width=4
470+
// appendIntWidth4 is semantically identical to appendInt(b, x, 4)
471+
// but optimized for 0 ≤ x < 10000.
486472
func appendIntWidth4(b []byte, x int) []byte {
487-
if x < 0 {
488-
b = append(b, '-')
489-
x = -x
490-
}
491-
if x >= 1e4 {
473+
if x < 0 || x >= 1e4 {
492474
return appendInt(b, x, 4)
493475
}
494-
return append(b, tensDigit[x/1e2], unitsDigit[x/1e2], tensDigit[x%1e2], unitsDigit[x%1e2])
476+
return append(b, tensDigit[x/1e2], onesDigit[x/1e2], tensDigit[x%1e2], onesDigit[x%1e2])
495477
}
496478

497479
// Never printed, just needs to be non-nil for return by atoi.

src/time/format_rfc3339.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte {
2121
// Format date.
2222
year, month, day := abs.days().date()
2323
b = appendIntWidth4(b, year)
24-
b = append(b, '-')
25-
b = appendIntWidth2(b, int(month))
26-
b = append(b, '-')
27-
b = appendIntWidth2(b, day)
24+
b = append(b, '-',
25+
tensDigit[month], onesDigit[month], '-',
26+
tensDigit[day], onesDigit[day])
2827

2928
b = append(b, 'T')
3029

3130
// Format time.
3231
hour, min, sec := abs.clock()
33-
b = appendIntWidth2(b, hour)
34-
b = append(b, ':')
35-
b = appendIntWidth2(b, min)
36-
b = append(b, ':')
37-
b = appendIntWidth2(b, sec)
32+
b = append(b,
33+
tensDigit[hour], onesDigit[hour], ':',
34+
tensDigit[min], onesDigit[min], ':',
35+
tensDigit[sec], onesDigit[sec])
3836

3937
if nanos {
4038
std := stdFracSecond(stdFracSecond9, 9, '.')
@@ -53,9 +51,11 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte {
5351
} else {
5452
b = append(b, '+')
5553
}
56-
b = appendIntWidth2(b, zone/60)
57-
b = append(b, ':')
58-
b = appendIntWidth2(b, zone%60)
54+
55+
zone %= 3600
56+
b = append(b,
57+
tensDigit[zone/60], onesDigit[zone/60], ':',
58+
tensDigit[zone%60], onesDigit[zone%60])
5959
return b
6060
}
6161

src/time/format_test.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,21 +1093,7 @@ func FuzzParseRFC3339(f *testing.F) {
10931093
}
10941094

10951095
func TestAppendIntWidth(t *testing.T) {
1096-
values := []int{0, -1, 1, 10, -10, 99, -99}
1097-
for _, v := range values {
1098-
exp := AppendInt(nil, v, 2)
1099-
got := AppendIntWidth2(nil, v)
1100-
if !bytes.Equal(got, exp) {
1101-
t.Errorf("AppendIntWidth2(%d) = %s, want %s", v, got, exp)
1102-
}
1103-
}
1104-
1105-
got := AppendIntWidth2(nil, 199)
1106-
if !bytes.Equal(got, []byte("99")) {
1107-
t.Errorf("AppendIntWidth2(199) = %s, want %s", got, []byte("99"))
1108-
}
1109-
1110-
values = append(values, 9999, -9999, 10001)
1096+
values := []int{0, -1, 1, 10, -10, 99, -99, 9999, -9999, 10001}
11111097
for _, v := range values {
11121098
exp := AppendInt(nil, v, 4)
11131099
got := AppendIntWidth4(nil, v)
@@ -1117,23 +1103,6 @@ func TestAppendIntWidth(t *testing.T) {
11171103
}
11181104
}
11191105

1120-
func BenchmarkAppendIntWidth2(b *testing.B) {
1121-
b.Run("name=AppendInt", func(b *testing.B) {
1122-
var buf = make([]byte, 0, 8)
1123-
b.ResetTimer()
1124-
for i := 0; i < b.N; i++ {
1125-
buf = AppendInt(buf[:0], 36, 2)
1126-
}
1127-
})
1128-
b.Run("name=AppendIntWidth2", func(b *testing.B) {
1129-
var buf = make([]byte, 0, 8)
1130-
b.ResetTimer()
1131-
for i := 0; i < b.N; i++ {
1132-
buf = AppendIntWidth2(buf[:0], 36)
1133-
}
1134-
})
1135-
}
1136-
11371106
func BenchmarkAppendIntWidth4(b *testing.B) {
11381107
b.Run("name=AppendInt", func(b *testing.B) {
11391108
var buf = make([]byte, 0, 8)

0 commit comments

Comments
 (0)