Optimize/new route regexp allocations
What type of PR is this? (check all applicable)
- [x] Refactor
- [ ] Feature
- [ ] Bug Fix
- [x] Optimization
- [ ] Documentation Update
Description
This optimizes NewRouter creation. The change introduces strings.Builder for more efficient strings concatenation, and replaces SplitN with Index, avoiding the allocations for the slice.
Related Tickets & Documents
# go test -bench=NewRouter -benchmem -memprofile memprofile.out -cpuprofile profile.out -count 10
goos: linux
goarch: amd64
pkg: github.com/gorilla/mux
cpu: 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
BenchmarkNewRouter-8 49730 32679 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 44238 32534 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 38658 29237 ns/op 25576 B/op 343 allocs/op
BenchmarkNewRouter-8 36507 34312 ns/op 25578 B/op 343 allocs/op
BenchmarkNewRouter-8 32928 37220 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 32894 42599 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 32104 33482 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 27852 37150 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouter-8 28695 40131 ns/op 25580 B/op 343 allocs/op
BenchmarkNewRouter-8 35971 36103 ns/op 25577 B/op 343 allocs/op
BenchmarkNewRouterRegexpFunc-8 461474 2702 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 397422 2639 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 488200 2496 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 594357 2891 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 532392 2602 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 487236 2535 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 512724 3118 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 397882 2704 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 364755 2881 ns/op 1424 B/op 42 allocs/op
BenchmarkNewRouterRegexpFunc-8 616176 2580 ns/op 1424 B/op 42 allocs/op
PASS
ok github.com/gorilla/mux 36.247s
Added/updated tests?
- [x] Yes
Run verifications and test
- [ ]
make verifyis passing - [x]
make testis passing
Codecov Report
Attention: Patch coverage is 90.90909% with 3 lines in your changes are missing coverage. Please review.
Project coverage is 78.18%. Comparing base (
7d9a6f7) to head (f80a252). Report is 1 commits behind head on main.
:exclamation: Current head f80a252 differs from pull request most recent head b684b38. Consider uploading reports for the commit b684b38 to get more accurate results
| Files | Patch % | Lines |
|---|---|---|
| regexp.go | 90.62% | 2 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #732 +/- ##
==========================================
- Coverage 79.58% 78.18% -1.40%
==========================================
Files 5 5
Lines 911 903 -8
==========================================
- Hits 725 706 -19
- Misses 131 141 +10
- Partials 55 56 +1
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
Rebased off #731
@AlexVulaj thanks for #731 merge; rebased this one for review. Benchmark still the same as in description.
@coreydaley is there anything i can/should do to get this reviewed?
Hi @titpetric, thanks for working on this! Also, thanks for being patient while we were adapting to the changes :). I have looked through & added some inputs, please feel free to share your thoughts.
@apoorvajagtap took your suggestion, added test to cover the expectation of the error return contents, PTAL
The lint & security scan errors are not related to the current changes, so I'll go ahead with squash & merge.
Will there be a new release soon that includes this change? My team and I came across this because we specifically need this type of optimization.