From 7ec6cf86a63c4e0d647602de0d81b5c25268fad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Sat, 6 Dec 2025 09:09:30 +0100 Subject: [PATCH 1/2] feat: enhance AppendInt to utilize cache for small negative integers and improve benchmark tests --- format.go | 5 ++++- format_test.go | 50 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/format.go b/format.go index 04d7aaa..1d427f2 100644 --- a/format.go +++ b/format.go @@ -196,9 +196,12 @@ func AppendInt(dst []byte, n int64) []byte { if n >= 0 { return AppendUint(dst, uint64(n)) } + if n > -100 { + return append(dst, smallNegInts[-n]...) + } var buf [20]byte i := formatUintBuf(&buf, uint64(-n)) i-- - buf[i] = '-' + buf[i] = '-' //nolint:gosec // i is always >= 0: formatUintBuf returns at least 1 for any input, so i >= 0 after decrement return append(dst, buf[i:]...) } diff --git a/format_test.go b/format_test.go index 13b51d2..b9b19a7 100644 --- a/format_test.go +++ b/format_test.go @@ -109,7 +109,7 @@ func Test_AppendUint(t *testing.T) { func Test_AppendInt(t *testing.T) { t.Parallel() - tests := []int64{0, 1, -1, 99, -99, 12345, -12345, math.MaxInt64, math.MinInt64} + tests := []int64{0, 1, -1, 99, -99, 100, -100, 12345, -12345, math.MaxInt64, math.MinInt64} for _, tt := range tests { expected := strconv.AppendInt([]byte("prefix"), tt, 10) result := AppendInt([]byte("prefix"), tt) @@ -117,6 +117,20 @@ func Test_AppendInt(t *testing.T) { } } +func Test_AppendInt_SmallNegativeCache(t *testing.T) { + t.Parallel() + // Test all small negative integers that should use the cache (-1 to -99) + for i := int64(-1); i >= -99; i-- { + expected := strconv.AppendInt([]byte("prefix"), i, 10) + result := AppendInt([]byte("prefix"), i) + require.Equal(t, expected, result, "AppendInt(%d)", i) + } + // Verify boundary: -100 should NOT use the cache + expected := strconv.AppendInt([]byte("prefix"), -100, 10) + result := AppendInt([]byte("prefix"), -100) + require.Equal(t, expected, result, "AppendInt(-100)") +} + // Benchmarks func Benchmark_FormatUint(b *testing.B) { @@ -295,19 +309,27 @@ func Benchmark_AppendUint(b *testing.B) { } func Benchmark_AppendInt(b *testing.B) { - input := int64(-123456789) + inputs := []struct { + name string + value int64 + }{ + {"small_neg", -42}, + {"medium_neg", -123456789}, + } dst := make([]byte, 0, 32) - b.Run("fiber", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = AppendInt(dst, input) - } - }) - b.Run("strconv", func(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = strconv.AppendInt(dst, input, 10) - } - }) + for _, input := range inputs { + b.Run(input.name+"/fiber", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _ = AppendInt(dst, input.value) + } + }) + b.Run(input.name+"/strconv", func(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _ = strconv.AppendInt(dst, input.value, 10) + } + }) + } } From 924d669e304450a11dac8d5cfb7b9f696dae0ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9?= Date: Sat, 6 Dec 2025 09:20:59 +0100 Subject: [PATCH 2/2] test: refactor tests for AppendInt to use subtests for better organization and parallel execution --- format_test.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/format_test.go b/format_test.go index b9b19a7..b878637 100644 --- a/format_test.go +++ b/format_test.go @@ -121,14 +121,20 @@ func Test_AppendInt_SmallNegativeCache(t *testing.T) { t.Parallel() // Test all small negative integers that should use the cache (-1 to -99) for i := int64(-1); i >= -99; i-- { - expected := strconv.AppendInt([]byte("prefix"), i, 10) - result := AppendInt([]byte("prefix"), i) - require.Equal(t, expected, result, "AppendInt(%d)", i) + t.Run(strconv.FormatInt(i, 10), func(t *testing.T) { + t.Parallel() + expected := strconv.AppendInt([]byte("prefix"), i, 10) + result := AppendInt([]byte("prefix"), i) + require.Equal(t, expected, result) + }) } // Verify boundary: -100 should NOT use the cache - expected := strconv.AppendInt([]byte("prefix"), -100, 10) - result := AppendInt([]byte("prefix"), -100) - require.Equal(t, expected, result, "AppendInt(-100)") + t.Run("boundary/-100", func(t *testing.T) { + t.Parallel() + expected := strconv.AppendInt([]byte("prefix"), -100, 10) + result := AppendInt([]byte("prefix"), -100) + require.Equal(t, expected, result) + }) } // Benchmarks