Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize/new route regexp allocations #732

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

titpetric
Copy link

@titpetric titpetric commented Aug 24, 2023

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • 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?

  • Yes

Run verifications and test

  • make verify is passing
  • make test is passing

@codecov
Copy link

codecov bot commented Aug 24, 2023

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).

❗ Current head f80a252 differs from pull request most recent head ed62916. Consider uploading reports for the commit ed62916 to get more accurate results

Files Patch % Lines
regexp.go 90.62% 2 Missing and 1 partial ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@titpetric
Copy link
Author

Rebased off #731

@titpetric titpetric force-pushed the optimize/newRouteRegexp-allocations branch from bd66659 to ed62916 Compare April 2, 2024 07:14
@pull-request-size pull-request-size bot added size/M and removed size/L labels Apr 2, 2024
@titpetric
Copy link
Author

@AlexVulaj thanks for #731 merge; rebased this one for review. Benchmark still the same as in description.

@titpetric
Copy link
Author

@coreydaley is there anything i can/should do to get this reviewed?

@apoorvajagtap apoorvajagtap self-assigned this May 6, 2024
// Name or pattern can't be empty.
if name == "" || patt == "" {
return nil, fmt.Errorf("mux: missing name or pattern in %q",
tpl[idxs[i]:end])
return nil, fmt.Errorf("mux: missing name or pattern in %q", param)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return nil, fmt.Errorf("mux: missing name or pattern in %q", param)
return nil, fmt.Errorf("mux: missing name or pattern in %q", tpl[idxs[i]:end])

Having param as the variable might not help much, as when there's no pattern or name inside braces {}, param will just return an empty string (""), whereas. Above will at least help with the braces for reference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, so {} would be printed as well. Sounds legit, I'll give it a once over and add a test, some inline comments, it's a bit hard to read after some time since writing it

@apoorvajagtap
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

2 participants