gin icon indicating copy to clipboard operation
gin copied to clipboard

Refactor cleanPath switch cases

Open styd opened this issue 6 years ago • 6 comments

Hi, I'm thinking of removing some checking repetitions. Although it's slightly less readable, it should perform faster as there are less comparisons for some cases. For example, you don't have to repeatedly check if p[r] is '.' when the next byte is not the last.

styd avatar Jul 02 '19 22:07 styd

Codecov Report

Merging #1968 into master will increase coverage by <.01%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1968      +/-   ##
==========================================
+ Coverage   98.83%   98.83%   +<.01%     
==========================================
  Files          40       40              
  Lines        2226     2234       +8     
==========================================
+ Hits         2200     2208       +8     
  Misses         14       14              
  Partials       12       12
Impacted Files Coverage Δ
path.go 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update b8b2fad...d8cd8b6. Read the comment docs.

codecov[bot] avatar Jul 03 '19 00:07 codecov[bot]

@styd Could you provide the benchmark before and after? Thanks.

appleboy avatar Sep 03 '19 14:09 appleboy

I added benchmark but the result is not as I expected it would be. The performance of my refactor was about the same or even worse once in a while.

BenchmarkCleanPath/OldCleanPath-8                1000000              2064 ns/op
BenchmarkCleanPath/NewCleanPath-8                1122319              2084 ns/op

Do you happen to know why? Did I do the benchmark wrong?

styd avatar Sep 04 '19 12:09 styd

After I added some test data (for when a two dots sequence is not removed), the refactored cleanPath always runs slightly faster in my computer:

$ go test -bench BenchmarkCleanPath -benchtime=5s
[GIN] 2019/09/06 - 22:05:53 | 200 |       3.323µs |       192.0.2.1 | GET      /name1/api
[GIN] 2019/09/06 - 22:05:53 | 200 |       1.199µs |       192.0.2.1 | GET      /name2/api
[GIN] 2019/09/06 - 22:05:53 | 200 |       4.283µs |       192.0.2.1 | GET      /test/copy/race
goos: linux
goarch: amd64
pkg: github.com/gin-gonic/gin
BenchmarkCleanPath/OldCleanPath-8                2164497              2762 ns/op
BenchmarkCleanPath/NewCleanPath-8                2218494              2746 ns/op
PASS
ok      github.com/gin-gonic/gin        17.854s

styd avatar Sep 06 '19 15:09 styd

Please fix conflicts and move to 1.11

appleboy avatar Mar 21 '24 14:03 appleboy