refactor: use b.Loop() to simplify the code and improve performance #4497
+23
−33
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description:
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
Before:
go test -run=^$ -bench=. ./pkg/decoders goos: darwin goarch: arm64 pkg: github.com/trufflesecurity/trufflehog/v3/pkg/decoders cpu: Apple M4 BenchmarkFromChunkSmall-10 6595539 181.3 ns/op BenchmarkFromChunkMedium-10 466124 2224 ns/op BenchmarkFromChunkLarge-10 33437010 34.91 ns/op BenchmarkDecodeOriginalEscape-10 50474690 42.09 ns/op BenchmarkDecodeCodePoint-10 880795 1218 ns/op BenchmarkDecodeBraceEscape-10 48463552 49.64 ns/op BenchmarkDecodeLongEscape-10 43757558 56.57 ns/op BenchmarkDecodePerlEscape-10 46256365 68.06 ns/op BenchmarkDecodeCssEscape-10 50212356 23.12 ns/op BenchmarkDecodeHtmlEscape-10 47056438 59.62 ns/op BenchmarkDecodePercentEscape-10 50229433 40.58 ns/op BenchmarkFromChunk_OriginalFormat-10 998955 1168 ns/op BenchmarkFromChunk_BraceFormat-10 1000000 1168 ns/op BenchmarkFromChunk_LongFormat-10 603687 2029 ns/op BenchmarkFromChunk_HtmlFormat-10 999502 1237 ns/op BenchmarkFromChunk_MixedContent-10 198066 5813 ns/op BenchmarkFromChunk_NoUnicode-10 258198 4645 ns/op BenchmarkFromChunk_LargeData-10 528 2244064 ns/op BenchmarkRegexMatching_AllPatterns-10 124023 9874 ns/op BenchmarkRegexMatching_NoMatch-10 245246 4659 ns/op BenchmarkFromChunk_MemoryAllocation-10 197952 6189 ns/op 14398 B/op 86 allocs/op BenchmarkUtf16ToUtf8-10 28343331 38.98 ns/op Benchmark_extractSubstrings-10 5197101 222.4 ns/op PASS ok github.com/trufflesecurity/trufflehog/v3/pkg/decoders 41.076sAfter:
go test -run=^$ -bench=. ./pkg/decoders goos: darwin goarch: arm64 pkg: github.com/trufflesecurity/trufflehog/v3/pkg/decoders cpu: Apple M4 BenchmarkFromChunkSmall-10 6542812 172.2 ns/op BenchmarkFromChunkMedium-10 642433 1850 ns/op BenchmarkFromChunkLarge-10 39467463 30.15 ns/op BenchmarkDecodeOriginalEscape-10 51547774 23.02 ns/op BenchmarkDecodeCodePoint-10 918812 1209 ns/op BenchmarkDecodeBraceEscape-10 44588186 25.17 ns/op BenchmarkDecodeLongEscape-10 45711601 26.57 ns/op BenchmarkDecodePerlEscape-10 43454577 26.05 ns/op BenchmarkDecodeCssEscape-10 50294870 23.55 ns/op BenchmarkDecodeHtmlEscape-10 43514719 24.98 ns/op BenchmarkDecodePercentEscape-10 49723412 24.82 ns/op BenchmarkFromChunk_OriginalFormat-10 960178 1216 ns/op BenchmarkFromChunk_BraceFormat-10 1000000 1168 ns/op BenchmarkFromChunk_LongFormat-10 625647 1924 ns/op BenchmarkFromChunk_HtmlFormat-10 992942 1167 ns/op BenchmarkFromChunk_MixedContent-10 202584 5804 ns/op BenchmarkFromChunk_NoUnicode-10 253852 4512 ns/op BenchmarkFromChunk_LargeData-10 536 2221862 ns/op BenchmarkRegexMatching_AllPatterns-10 125577 9504 ns/op BenchmarkRegexMatching_NoMatch-10 261069 4548 ns/op BenchmarkFromChunk_MemoryAllocation-10 195384 5780 ns/op 14393 B/op 86 allocs/op BenchmarkUtf16ToUtf8-10 31611598 37.37 ns/op Benchmark_extractSubstrings-10 5364784 224.2 ns/op PASS ok github.com/trufflesecurity/trufflehog/v3/pkg/decoders 27.924sChecklist:
make test-community)?make lintthis requires golangci-lint)?