-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: avoid allocations with
(*regexp.Regexp).MatchString
We should use `(*regexp.Regexp).MatchString` instead of `(*regexp.Regexp).Match([]byte(...))` when matching string to avoid unnecessary `[]byte` conversions and reduce allocations. Example benchmark: func BenchmarkMatch(b *testing.B) { for i := 0; i < b.N; i++ { if match := looksLikeUrl.Match([]byte("https://www.example.com")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { for i := 0; i < b.N; i++ { if match := looksLikeUrl.MatchString("https://www.example.com"); !match { b.Fail() } } } goos: linux goarch: amd64 pkg: github.com/bearer/bearer/internal/parser/interfaces/paths cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics BenchmarkMatch-16 1788562 812.1 ns/op 24 B/op 1 allocs/op BenchmarkMatchString-16 2106370 517.8 ns/op 0 B/op 0 allocs/op PASS ok github.com/bearer/bearer/internal/parser/interfaces/paths 3.628s Signed-off-by: Eng Zer Jun <[email protected]>
- Loading branch information
Showing
5 changed files
with
7 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters