From e9d89755debc22d19f7e58b628279b4f25ab2b99 Mon Sep 17 00:00:00 2001 From: fx408 Date: Sat, 22 Nov 2025 02:32:23 +0800 Subject: [PATCH 1/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format.go | 27 +++++++++++++++++++++++++++ src/time/format_rfc3339.go | 16 ++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/time/format.go b/src/time/format.go index ad5486f4d28f89..89c413ba1fe663 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -464,6 +464,33 @@ func appendInt(b []byte, x int, width int) []byte { return b } +const unitsDigit = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +const tensDigit = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999" + +// appendIntWidth2 special scenario for appendInt, with parameter width=2 +func appendIntWidth2(b []byte, x int) []byte { + if x < 0 { + b = append(b, '-') + x = -x + } + if x >= 1e2 { + x %= 1e2 + } + return append(b, tensDigit[x], unitsDigit[x]) +} + +// appendIntWidth4 special scenario for appendInt, with parameter width=4 +func appendIntWidth4(b []byte, x int) []byte { + if x < 0 { + b = append(b, '-') + x = -x + } + if x >= 1e4 { + x %= 1e4 + } + return append(b, tensDigit[x/1e2], unitsDigit[x/1e2], tensDigit[x%1e2], unitsDigit[x%1e2]) +} + // Never printed, just needs to be non-nil for return by atoi. var errAtoi = errors.New("time: invalid number") diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index 05fddfca89f64c..deb8dfaa5ce65c 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -20,21 +20,21 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { // Format date. year, month, day := abs.days().date() - b = appendInt(b, year, 4) + b = appendIntWidth4(b, year) b = append(b, '-') - b = appendInt(b, int(month), 2) + b = appendIntWidth2(b, int(month)) b = append(b, '-') - b = appendInt(b, day, 2) + b = appendIntWidth2(b, day) b = append(b, 'T') // Format time. hour, min, sec := abs.clock() - b = appendInt(b, hour, 2) + b = appendIntWidth2(b, hour) b = append(b, ':') - b = appendInt(b, min, 2) + b = appendIntWidth2(b, min) b = append(b, ':') - b = appendInt(b, sec, 2) + b = appendIntWidth2(b, sec) if nanos { std := stdFracSecond(stdFracSecond9, 9, '.') @@ -53,9 +53,9 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { } else { b = append(b, '+') } - b = appendInt(b, zone/60, 2) + b = appendIntWidth2(b, zone/60) b = append(b, ':') - b = appendInt(b, zone%60, 2) + b = appendIntWidth2(b, zone%60) return b } From 565945ffecd26d1893228a3113fc38b63d9c3567 Mon Sep 17 00:00:00 2001 From: fx408 Date: Sat, 22 Nov 2025 04:19:13 +0800 Subject: [PATCH 2/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/time/format.go b/src/time/format.go index 89c413ba1fe663..39456d1c627a55 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -464,8 +464,10 @@ func appendInt(b []byte, x int, width int) []byte { return b } -const unitsDigit = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" -const tensDigit = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999" +const unitsDigit = "01234567890123456789012345678901234567890123456789" + + "01234567890123456789012345678901234567890123456789" +const tensDigit = "00000000001111111111222222222233333333334444444444" + + "55555555556666666666777777777788888888889999999999" // appendIntWidth2 special scenario for appendInt, with parameter width=2 func appendIntWidth2(b []byte, x int) []byte { From ba2dc466b1ac656467d97296bd9dd64501b479ca Mon Sep 17 00:00:00 2001 From: fx408 Date: Sat, 22 Nov 2025 09:49:12 +0800 Subject: [PATCH 3/8] optimize the performance of the function appendFormatRFC3339 --- src/time/export_test.go | 2 ++ src/time/format.go | 3 +- src/time/format_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/time/export_test.go b/src/time/export_test.go index a4940d12f91d80..21119561f04570 100644 --- a/src/time/export_test.go +++ b/src/time/export_test.go @@ -134,6 +134,8 @@ var StdChunkNames = map[int]string{ var Quote = quote var AppendInt = appendInt +var AppendIntWidth2 = appendIntWidth2 +var AppendIntWidth4 = appendIntWidth4 var AppendFormatAny = Time.appendFormat var AppendFormatRFC3339 = Time.appendFormatRFC3339 var ParseAny = parse diff --git a/src/time/format.go b/src/time/format.go index 39456d1c627a55..60c5a3b66e4d9b 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -470,6 +470,7 @@ const tensDigit = "00000000001111111111222222222233333333334444444444" + "55555555556666666666777777777788888888889999999999" // appendIntWidth2 special scenario for appendInt, with parameter width=2 +// Only applicable to integers with absolute value less than 100 func appendIntWidth2(b []byte, x int) []byte { if x < 0 { b = append(b, '-') @@ -488,7 +489,7 @@ func appendIntWidth4(b []byte, x int) []byte { x = -x } if x >= 1e4 { - x %= 1e4 + return appendInt(b, x, 4) } return append(b, tensDigit[x/1e2], unitsDigit[x/1e2], tensDigit[x%1e2], unitsDigit[x%1e2]) } diff --git a/src/time/format_test.go b/src/time/format_test.go index 2537c765968ee2..b2742544cbebac 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -1091,3 +1091,70 @@ func FuzzParseRFC3339(f *testing.F) { } }) } + +func TestAppendIntWidth(t *testing.T) { + values := []int{0, -1, 1, 10, -10, 99, -99} + for _, v := range values { + exp := AppendInt(nil, v, 2) + got := AppendIntWidth2(nil, v) + if !bytes.Equal(got, exp) { + t.Errorf("AppendIntWidth2(%d) = %s, want %s", v, got, exp) + } + } + + got := AppendIntWidth2(nil, 199) + if !bytes.Equal(got, []byte("99")) { + t.Errorf("AppendIntWidth2(199) = %s, want %s", got, []byte("99")) + } + + values = append(values, 9999, -9999, 10001) + for _, v := range values { + exp := AppendInt(nil, v, 4) + got := AppendIntWidth4(nil, v) + if !bytes.Equal(got, exp) { + t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, exp) + } + } +} + +func BenchmarkAppendIntWidth2(b *testing.B) { + b.Run("name=AppendInt", func(b *testing.B) { + var buf = make([]byte, 0, 8) + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf = AppendInt(buf[:0], 36, 2) + } + }) + b.Run("name=AppendIntWidth2", func(b *testing.B) { + var buf = make([]byte, 0, 8) + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf = AppendIntWidth2(buf[:0], 36) + } + }) +} + +func BenchmarkAppendIntWidth4(b *testing.B) { + b.Run("name=AppendInt", func(b *testing.B) { + var buf = make([]byte, 0, 8) + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf = AppendInt(buf[:0], 360, 4) + } + }) + b.Run("name=AppendIntWidth4", func(b *testing.B) { + var buf = make([]byte, 0, 8) + b.ResetTimer() + for i := 0; i < b.N; i++ { + buf = AppendIntWidth4(buf[:0], 360) + } + }) +} + +func BenchmarkTimeFormatRFC3339(b *testing.B) { + tm := Now() + buf := make([]byte, 0, 64) + for i := 0; i < b.N; i++ { + buf = tm.AppendFormat(buf[:0], RFC3339) + } +} From 71bf560afc58d5338b7139b985251239d5751794 Mon Sep 17 00:00:00 2001 From: fx408 Date: Sat, 29 Nov 2025 20:58:16 +0800 Subject: [PATCH 4/8] optimize the performance of the function appendFormatRFC3339 --- src/time/export_test.go | 1 - src/time/format.go | 30 ++++++------------------------ src/time/format_rfc3339.go | 24 ++++++++++++------------ src/time/format_test.go | 33 +-------------------------------- 4 files changed, 19 insertions(+), 69 deletions(-) diff --git a/src/time/export_test.go b/src/time/export_test.go index 21119561f04570..a3fb4717b12aa8 100644 --- a/src/time/export_test.go +++ b/src/time/export_test.go @@ -134,7 +134,6 @@ var StdChunkNames = map[int]string{ var Quote = quote var AppendInt = appendInt -var AppendIntWidth2 = appendIntWidth2 var AppendIntWidth4 = appendIntWidth4 var AppendFormatAny = Time.appendFormat var AppendFormatRFC3339 = Time.appendFormatRFC3339 diff --git a/src/time/format.go b/src/time/format.go index 60c5a3b66e4d9b..c40a2c6f72af04 100644 --- a/src/time/format.go +++ b/src/time/format.go @@ -464,34 +464,16 @@ func appendInt(b []byte, x int, width int) []byte { return b } -const unitsDigit = "01234567890123456789012345678901234567890123456789" + - "01234567890123456789012345678901234567890123456789" -const tensDigit = "00000000001111111111222222222233333333334444444444" + - "55555555556666666666777777777788888888889999999999" - -// appendIntWidth2 special scenario for appendInt, with parameter width=2 -// Only applicable to integers with absolute value less than 100 -func appendIntWidth2(b []byte, x int) []byte { - if x < 0 { - b = append(b, '-') - x = -x - } - if x >= 1e2 { - x %= 1e2 - } - return append(b, tensDigit[x], unitsDigit[x]) -} +const onesDigit = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" +const tensDigit = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999" -// appendIntWidth4 special scenario for appendInt, with parameter width=4 +// appendIntWidth4 is semantically identical to appendInt(b, x, 4) +// but optimized for 0 ≤ x < 10000. func appendIntWidth4(b []byte, x int) []byte { - if x < 0 { - b = append(b, '-') - x = -x - } - if x >= 1e4 { + if x < 0 || x >= 1e4 { return appendInt(b, x, 4) } - return append(b, tensDigit[x/1e2], unitsDigit[x/1e2], tensDigit[x%1e2], unitsDigit[x%1e2]) + return append(b, tensDigit[x/1e2], onesDigit[x/1e2], tensDigit[x%1e2], onesDigit[x%1e2]) } // Never printed, just needs to be non-nil for return by atoi. diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index deb8dfaa5ce65c..3e7fa5de64ec03 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -21,20 +21,18 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { // Format date. year, month, day := abs.days().date() b = appendIntWidth4(b, year) - b = append(b, '-') - b = appendIntWidth2(b, int(month)) - b = append(b, '-') - b = appendIntWidth2(b, day) + b = append(b, '-', + tensDigit[month], onesDigit[month], '-', + tensDigit[day], onesDigit[day]) b = append(b, 'T') // Format time. hour, min, sec := abs.clock() - b = appendIntWidth2(b, hour) - b = append(b, ':') - b = appendIntWidth2(b, min) - b = append(b, ':') - b = appendIntWidth2(b, sec) + b = append(b, + tensDigit[hour], onesDigit[hour], ':', + tensDigit[min], onesDigit[min], ':', + tensDigit[sec], onesDigit[sec]) if nanos { std := stdFracSecond(stdFracSecond9, 9, '.') @@ -53,9 +51,11 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { } else { b = append(b, '+') } - b = appendIntWidth2(b, zone/60) - b = append(b, ':') - b = appendIntWidth2(b, zone%60) + + zone %= 3600 + b = append(b, + tensDigit[zone/60], onesDigit[zone/60], ':', + tensDigit[zone%60], onesDigit[zone%60]) return b } diff --git a/src/time/format_test.go b/src/time/format_test.go index b2742544cbebac..e0c4449e352625 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -1093,21 +1093,7 @@ func FuzzParseRFC3339(f *testing.F) { } func TestAppendIntWidth(t *testing.T) { - values := []int{0, -1, 1, 10, -10, 99, -99} - for _, v := range values { - exp := AppendInt(nil, v, 2) - got := AppendIntWidth2(nil, v) - if !bytes.Equal(got, exp) { - t.Errorf("AppendIntWidth2(%d) = %s, want %s", v, got, exp) - } - } - - got := AppendIntWidth2(nil, 199) - if !bytes.Equal(got, []byte("99")) { - t.Errorf("AppendIntWidth2(199) = %s, want %s", got, []byte("99")) - } - - values = append(values, 9999, -9999, 10001) + values := []int{0, -1, 1, 10, -10, 99, -99, 9999, -9999, 10001} for _, v := range values { exp := AppendInt(nil, v, 4) got := AppendIntWidth4(nil, v) @@ -1117,23 +1103,6 @@ func TestAppendIntWidth(t *testing.T) { } } -func BenchmarkAppendIntWidth2(b *testing.B) { - b.Run("name=AppendInt", func(b *testing.B) { - var buf = make([]byte, 0, 8) - b.ResetTimer() - for i := 0; i < b.N; i++ { - buf = AppendInt(buf[:0], 36, 2) - } - }) - b.Run("name=AppendIntWidth2", func(b *testing.B) { - var buf = make([]byte, 0, 8) - b.ResetTimer() - for i := 0; i < b.N; i++ { - buf = AppendIntWidth2(buf[:0], 36) - } - }) -} - func BenchmarkAppendIntWidth4(b *testing.B) { b.Run("name=AppendInt", func(b *testing.B) { var buf = make([]byte, 0, 8) From 1d1bc9e811b6e42129718802bb2beab4d194c20a Mon Sep 17 00:00:00 2001 From: fx408 Date: Sun, 30 Nov 2025 09:05:21 +0800 Subject: [PATCH 5/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format_rfc3339.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index 3e7fa5de64ec03..f2a0e302489bb1 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -20,19 +20,21 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { // Format date. year, month, day := abs.days().date() - b = appendIntWidth4(b, year) - b = append(b, '-', - tensDigit[month], onesDigit[month], '-', - tensDigit[day], onesDigit[day]) - - b = append(b, 'T') - // Format time. hour, min, sec := abs.clock() - b = append(b, - tensDigit[hour], onesDigit[hour], ':', - tensDigit[min], onesDigit[min], ':', - tensDigit[sec], onesDigit[sec]) + + b = appendIntWidth4(b, year) + b = append(b, '-', + tensDigit[month], onesDigit[month], + '-', + tensDigit[day], onesDigit[day], + 'T', + tensDigit[hour], onesDigit[hour], + ':', + tensDigit[min], onesDigit[min], + ':', + tensDigit[sec], onesDigit[sec], + ) if nanos { std := stdFracSecond(stdFracSecond9, 9, '.') @@ -54,8 +56,10 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { zone %= 3600 b = append(b, - tensDigit[zone/60], onesDigit[zone/60], ':', - tensDigit[zone%60], onesDigit[zone%60]) + tensDigit[zone/60], onesDigit[zone/60], + ':', + tensDigit[zone%60], onesDigit[zone%60], + ) return b } From 536ee116b2757109aec1612be41afc7da158e851 Mon Sep 17 00:00:00 2001 From: fx408 Date: Fri, 5 Dec 2025 21:42:07 +0800 Subject: [PATCH 6/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format_rfc3339.go | 3 +-- src/time/format_test.go | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index f2a0e302489bb1..3ac8d97c8d443f 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -18,9 +18,8 @@ import "errors" func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { _, offset, abs := t.locabs() - // Format date. + // Format date and time. year, month, day := abs.days().date() - // Format time. hour, min, sec := abs.clock() b = appendIntWidth4(b, year) diff --git a/src/time/format_test.go b/src/time/format_test.go index e0c4449e352625..5b2964e609a55b 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -1095,10 +1095,10 @@ func FuzzParseRFC3339(f *testing.F) { func TestAppendIntWidth(t *testing.T) { values := []int{0, -1, 1, 10, -10, 99, -99, 9999, -9999, 10001} for _, v := range values { - exp := AppendInt(nil, v, 4) + want := AppendInt(nil, v, 4) got := AppendIntWidth4(nil, v) - if !bytes.Equal(got, exp) { - t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, exp) + if !bytes.Equal(got, want) { + t.Errorf("AppendIntWidth4(%d) = %s, want %s", v, got, want) } } } @@ -1107,23 +1107,23 @@ func BenchmarkAppendIntWidth4(b *testing.B) { b.Run("name=AppendInt", func(b *testing.B) { var buf = make([]byte, 0, 8) b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { buf = AppendInt(buf[:0], 360, 4) } }) b.Run("name=AppendIntWidth4", func(b *testing.B) { var buf = make([]byte, 0, 8) b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { buf = AppendIntWidth4(buf[:0], 360) } }) } func BenchmarkTimeFormatRFC3339(b *testing.B) { - tm := Now() + tm := Unix(1661201140, 676836973) buf := make([]byte, 0, 64) - for i := 0; i < b.N; i++ { + for b.Loop() { buf = tm.AppendFormat(buf[:0], RFC3339) } } From f008ee51ef493f97213a19025a293faf7d8c7fac Mon Sep 17 00:00:00 2001 From: fx408 Date: Sat, 6 Dec 2025 21:24:44 +0800 Subject: [PATCH 7/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format_rfc3339.go | 7 +++++++ src/time/format_test.go | 1 + 2 files changed, 8 insertions(+) diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index 3ac8d97c8d443f..db37f369fb61b8 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -53,6 +53,13 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { b = append(b, '+') } + if zone > 3600 { + b = appendInt(b, zone/60, 2) + b = append(b, ':') + b = appendInt(b, zone%60, 2) + return b + } + zone %= 3600 b = append(b, tensDigit[zone/60], onesDigit[zone/60], diff --git a/src/time/format_test.go b/src/time/format_test.go index 5b2964e609a55b..8422f0f356b795 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -1123,6 +1123,7 @@ func BenchmarkAppendIntWidth4(b *testing.B) { func BenchmarkTimeFormatRFC3339(b *testing.B) { tm := Unix(1661201140, 676836973) buf := make([]byte, 0, 64) + b.ReportAllocs() for b.Loop() { buf = tm.AppendFormat(buf[:0], RFC3339) } From 0a21cd15c02f2f6d3aae4cad12aca905ba6a553c Mon Sep 17 00:00:00 2001 From: fx408 Date: Sun, 7 Dec 2025 14:47:34 +0800 Subject: [PATCH 8/8] optimize the performance of the function appendFormatRFC3339 --- src/time/format_rfc3339.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/time/format_rfc3339.go b/src/time/format_rfc3339.go index db37f369fb61b8..ab12c877aaf1e4 100644 --- a/src/time/format_rfc3339.go +++ b/src/time/format_rfc3339.go @@ -55,12 +55,11 @@ func (t Time) appendFormatRFC3339(b []byte, nanos bool) []byte { if zone > 3600 { b = appendInt(b, zone/60, 2) - b = append(b, ':') - b = appendInt(b, zone%60, 2) + b = append(b, ':', + tensDigit[zone%60], onesDigit[zone%60]) return b } - zone %= 3600 b = append(b, tensDigit[zone/60], onesDigit[zone/60], ':',