Skip to content

Commit 719bb7c

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

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/time/export_test.go

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

136136
var AppendInt = appendInt
137+
var AppendIntWidth2 = appendIntWidth2
138+
var AppendIntWidth4 = appendIntWidth4
137139
var AppendFormatAny = Time.appendFormat
138140
var AppendFormatRFC3339 = Time.appendFormatRFC3339
139141
var ParseAny = parse

src/time/format.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ const tensDigit = "00000000001111111111222222222233333333334444444444" +
470470
"55555555556666666666777777777788888888889999999999"
471471

472472
// appendIntWidth2 special scenario for appendInt, with parameter width=2
473+
// Only applicable to integers with absolute value less than 100
473474
func appendIntWidth2(b []byte, x int) []byte {
474475
if x < 0 {
475476
b = append(b, '-')
@@ -488,7 +489,7 @@ func appendIntWidth4(b []byte, x int) []byte {
488489
x = -x
489490
}
490491
if x >= 1e4 {
491-
x %= 1e4
492+
return appendInt(b, x, 4)
492493
}
493494
return append(b, tensDigit[x/1e2], unitsDigit[x/1e2], tensDigit[x%1e2], unitsDigit[x%1e2])
494495
}

src/time/format_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,3 +1091,70 @@ func FuzzParseRFC3339(f *testing.F) {
10911091
}
10921092
})
10931093
}
1094+
1095+
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)
1111+
for _, v := range values {
1112+
exp := AppendInt(nil, v, 4)
1113+
got := AppendIntWidth4(nil, v)
1114+
if !bytes.Equal(got, exp) {
1115+
t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, exp)
1116+
}
1117+
}
1118+
}
1119+
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+
1137+
func BenchmarkAppendIntWidth4(b *testing.B) {
1138+
b.Run("name=AppendInt", func(b *testing.B) {
1139+
var buf = make([]byte, 0, 8)
1140+
b.ResetTimer()
1141+
for i := 0; i < b.N; i++ {
1142+
buf = AppendInt(buf[:0], 360, 4)
1143+
}
1144+
})
1145+
b.Run("name=AppendIntWidth4", func(b *testing.B) {
1146+
var buf = make([]byte, 0, 8)
1147+
b.ResetTimer()
1148+
for i := 0; i < b.N; i++ {
1149+
buf = AppendIntWidth4(buf[:0], 360)
1150+
}
1151+
})
1152+
}
1153+
1154+
func BenchmarkTimeFormatRFC3339(b *testing.B) {
1155+
tm := Now()
1156+
buf := make([]byte, 0, 64)
1157+
for i := 0; i < b.N; i++ {
1158+
buf = tm.AppendFormat(buf[:0], RFC3339)
1159+
}
1160+
}

0 commit comments

Comments
 (0)