refactor: use b.Loop() to simplify the code and improve performance (#4497)

Signed-off-by: dulanting <[email protected]>

These changes use b.Loop() to simplify the code and improve performance
Supported by Go Team, more info: https://go.dev/blog/testing-b-loop  and https://go.dev/issue/73137
This commit is contained in:
dulanting
2025-10-14 10:43:37 -04:00
committed by GitHub
parent f9dd660729
commit 7afd5da285
4 changed files with 23 additions and 33 deletions
+3 -3
View File
@@ -155,7 +155,7 @@ func BenchmarkFromChunkSmall(b *testing.B) {
d := Base64{} d := Base64{}
data := detectors.MustGetBenchmarkData()["small"] data := detectors.MustGetBenchmarkData()["small"]
for n := 0; n < b.N; n++ { for b.Loop() {
d.FromChunk(&sources.Chunk{Data: data}) d.FromChunk(&sources.Chunk{Data: data})
} }
} }
@@ -164,7 +164,7 @@ func BenchmarkFromChunkMedium(b *testing.B) {
d := Base64{} d := Base64{}
data := detectors.MustGetBenchmarkData()["medium"] data := detectors.MustGetBenchmarkData()["medium"]
for n := 0; n < b.N; n++ { for b.Loop() {
d.FromChunk(&sources.Chunk{Data: data}) d.FromChunk(&sources.Chunk{Data: data})
} }
} }
@@ -173,7 +173,7 @@ func BenchmarkFromChunkLarge(b *testing.B) {
d := Base64{} d := Base64{}
data := detectors.MustGetBenchmarkData()["big"] data := detectors.MustGetBenchmarkData()["big"]
for n := 0; n < b.N; n++ { for b.Loop() {
d.FromChunk(&sources.Chunk{Data: data}) d.FromChunk(&sources.Chunk{Data: data})
} }
} }
+18 -28
View File
@@ -60,49 +60,49 @@ var (
// Benchmark individual decoder functions // Benchmark individual decoder functions
func BenchmarkDecodeOriginalEscape(b *testing.B) { func BenchmarkDecodeOriginalEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeEscaped(originalUnicodeData) _ = decodeEscaped(originalUnicodeData)
} }
} }
func BenchmarkDecodeCodePoint(b *testing.B) { func BenchmarkDecodeCodePoint(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeCodePoint(codePointData) _ = decodeCodePoint(codePointData)
} }
} }
func BenchmarkDecodeBraceEscape(b *testing.B) { func BenchmarkDecodeBraceEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeBraceEscape(braceEscapeData) _ = decodeBraceEscape(braceEscapeData)
} }
} }
func BenchmarkDecodeLongEscape(b *testing.B) { func BenchmarkDecodeLongEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeLongEscape(longEscapeData) _ = decodeLongEscape(longEscapeData)
} }
} }
func BenchmarkDecodePerlEscape(b *testing.B) { func BenchmarkDecodePerlEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodePerlEscape(perlEscapeData) _ = decodePerlEscape(perlEscapeData)
} }
} }
func BenchmarkDecodeCssEscape(b *testing.B) { func BenchmarkDecodeCssEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeCssEscape(cssEscapeData) _ = decodeCssEscape(cssEscapeData)
} }
} }
func BenchmarkDecodeHtmlEscape(b *testing.B) { func BenchmarkDecodeHtmlEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodeHtmlEscape(htmlEscapeData) _ = decodeHtmlEscape(htmlEscapeData)
} }
} }
func BenchmarkDecodePercentEscape(b *testing.B) { func BenchmarkDecodePercentEscape(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
_ = decodePercentEscape(percentEscapeData) _ = decodePercentEscape(percentEscapeData)
} }
} }
@@ -118,8 +118,7 @@ func BenchmarkFromChunk_OriginalFormat(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: originalUnicodeData} chunk := &sources.Chunk{Data: originalUnicodeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -128,8 +127,7 @@ func BenchmarkFromChunk_BraceFormat(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: braceEscapeData} chunk := &sources.Chunk{Data: braceEscapeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -138,8 +136,7 @@ func BenchmarkFromChunk_LongFormat(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: longEscapeData} chunk := &sources.Chunk{Data: longEscapeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -148,8 +145,7 @@ func BenchmarkFromChunk_HtmlFormat(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: htmlEscapeData} chunk := &sources.Chunk{Data: htmlEscapeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -158,8 +154,7 @@ func BenchmarkFromChunk_MixedContent(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: mixedContentData} chunk := &sources.Chunk{Data: mixedContentData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -168,8 +163,7 @@ func BenchmarkFromChunk_NoUnicode(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: noUnicodeData} chunk := &sources.Chunk{Data: noUnicodeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -178,8 +172,7 @@ func BenchmarkFromChunk_LargeData(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: largeData} chunk := &sources.Chunk{Data: largeData}
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
_ = decoder.FromChunk(chunk) _ = decoder.FromChunk(chunk)
} }
} }
@@ -188,8 +181,7 @@ func BenchmarkFromChunk_LargeData(b *testing.B) {
func BenchmarkRegexMatching_AllPatterns(b *testing.B) { func BenchmarkRegexMatching_AllPatterns(b *testing.B) {
testData := mixedContentData testData := mixedContentData
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
// Simulate the pattern matching in FromChunk // Simulate the pattern matching in FromChunk
_ = longEscapePat.Match(testData) _ = longEscapePat.Match(testData)
_ = braceEscapePat.Match(testData) _ = braceEscapePat.Match(testData)
@@ -206,8 +198,7 @@ func BenchmarkRegexMatching_AllPatterns(b *testing.B) {
func BenchmarkRegexMatching_NoMatch(b *testing.B) { func BenchmarkRegexMatching_NoMatch(b *testing.B) {
testData := noUnicodeData testData := noUnicodeData
b.ResetTimer() for b.Loop() {
for i := 0; i < b.N; i++ {
// Simulate the pattern matching in FromChunk on data with no matches // Simulate the pattern matching in FromChunk on data with no matches
_ = longEscapePat.Match(testData) _ = longEscapePat.Match(testData)
_ = braceEscapePat.Match(testData) _ = braceEscapePat.Match(testData)
@@ -226,9 +217,8 @@ func BenchmarkFromChunk_MemoryAllocation(b *testing.B) {
decoder := &EscapedUnicode{} decoder := &EscapedUnicode{}
chunk := &sources.Chunk{Data: mixedContentData} chunk := &sources.Chunk{Data: mixedContentData}
b.ResetTimer()
b.ReportAllocs() b.ReportAllocs()
for i := 0; i < b.N; i++ { for b.Loop() {
result := decoder.FromChunk(chunk) result := decoder.FromChunk(chunk)
if result != nil { if result != nil {
// Prevent compiler optimization // Prevent compiler optimization
+1 -1
View File
@@ -100,7 +100,7 @@ func BenchmarkUtf16ToUtf8(b *testing.B) {
// Example UTF-16LE encoded data // Example UTF-16LE encoded data
data := []byte{72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0} data := []byte{72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0}
for n := 0; n < b.N; n++ { for b.Loop() {
_, _ = utf16ToUTF8(data) _, _ = utf16ToUTF8(data)
} }
} }
+1 -1
View File
@@ -375,7 +375,7 @@ go
away.`) away.`)
func Benchmark_extractSubstrings(b *testing.B) { func Benchmark_extractSubstrings(b *testing.B) {
for i := 0; i < b.N; i++ { for b.Loop() {
extractSubstrings(testBytes) extractSubstrings(testBytes)
} }
} }