Refactor cleanPath switch cases
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.
Codecov Report
Merging #1968 into master will increase coverage by
<.01%. The diff coverage is100%.
@@ 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 dataPowered by Codecov. Last update b8b2fad...d8cd8b6. Read the comment docs.
@styd Could you provide the benchmark before and after? Thanks.
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?
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
Please fix conflicts and move to 1.11