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

Reuse bytes when cleaning the URL paths #2179

Merged
merged 4 commits into from
Jan 7, 2020
Merged

Reuse bytes when cleaning the URL paths #2179

merged 4 commits into from
Jan 7, 2020

Conversation

panjf2000
Copy link
Contributor

@panjf2000 panjf2000 commented Dec 11, 2019

Signed-off-by: Andy Pan panjf2000@gmail.com

@codecov
Copy link

codecov bot commented Dec 11, 2019

Codecov Report

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

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2179      +/-   ##
==========================================
+ Coverage    98.5%   98.51%   +<.01%     
==========================================
  Files          41       41              
  Lines        2276     2284       +8     
==========================================
+ Hits         2242     2250       +8     
  Misses         18       18              
  Partials       16       16
Impacted Files Coverage Δ
path.go 100% <100%> (ø) ⬆️

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 fd8a65b...bf153c3. Read the comment docs.

@panjf2000
Copy link
Contributor Author

/cc @thinkerou

@appleboy
Copy link
Member

Any benchmark report?

@panjf2000
Copy link
Contributor Author

panjf2000 commented Dec 12, 2019

Not yet, but I can add some tests and get benchmark-results later.

@panjf2000
Copy link
Contributor Author

panjf2000 commented Dec 13, 2019

@appleboy

BenchmarkCleanPath-8                     1000000              1321 ns/op             416 B/op         40 allocs/op
BenchmarkCleanPathSyncPool-8             1000000              2482 ns/op             848 B/op         40 allocs/op
BenchmarkCleanPathBytesPool-8            1000000              1818 ns/op             112 B/op         17 allocs/op

Time cost increases a little bit(maybe those test cases of paths are not general cuz those are mocks) and the memory allocs decreases to 25% when using bytes pool.

@panjf2000
Copy link
Contributor Author

@thinkerou @appleboy
PTAL

@appleboy appleboy added this to the 1.6 milestone Dec 18, 2019
@appleboy
Copy link
Member

@panjf2000 maybe you should create PR in https://github.com/julienschmidt/httprouter first.

@panjf2000
Copy link
Contributor Author

Good point, I will open a PR to httprouter later.

@panjf2000
Copy link
Contributor Author

panjf2000 commented Dec 20, 2019

Profiling
CPU:

      flat  flat%   sum%        cum   cum%
    1140ms 23.22% 23.22%     2500ms 50.92%  github.com/gin-gonic/gin.cleanPathRaw
    1010ms 20.57% 43.79%     1010ms 20.57%  runtime.pthread_cond_signal
     560ms 11.41% 55.19%     1010ms 20.57%  runtime.mallocgc
     340ms  6.92% 62.12%     1060ms 21.59%  github.com/gin-gonic/gin.cleanPath
     150ms  3.05% 65.17%      250ms  5.09%  github.com/gin-gonic/gin.bufApp
     150ms  3.05% 68.23%      530ms 10.79%  github.com/gin-gonic/gin.bufAppRaw
     150ms  3.05% 71.28%      150ms  3.05%  runtime.memmove
     130ms  2.65% 73.93%      620ms 12.63%  runtime.makeslice
     120ms  2.44% 76.37%      660ms 13.44%  runtime.slicebytetostring
     110ms  2.24% 78.62%      110ms  2.24%  runtime.acquirem

Mem:

      flat  flat%   sum%        cum   cum%
  343.01MB 38.99% 38.99%   578.01MB 65.70%  github.com/gin-gonic/gin.cleanPathRaw
  235.01MB 26.71% 65.70%   235.01MB 26.71%  github.com/gin-gonic/gin.bufAppRaw
  217.01MB 24.67% 90.37%   217.01MB 24.67%  github.com/gin-gonic/gin.glob..func4
   52.50MB  5.97% 96.34%    52.50MB  5.97%  github.com/gin-gonic/gin.cleanPath
      28MB  3.18% 99.52%   245.01MB 27.85%  github.com/gin-gonic/gin.cleanPathSyncPool
         0     0% 99.52%   578.01MB 65.70%  github.com/gin-gonic/gin.BenchmarkCleanPath
         0     0% 99.52%    52.50MB  5.97%  github.com/gin-gonic/gin.BenchmarkCleanPathBytesPool
         0     0% 99.52%   245.01MB 27.85%  github.com/gin-gonic/gin.BenchmarkCleanPathSyncPool
         0     0% 99.52%   875.52MB 99.52%  testing.(*B).launch
         0     0% 99.52%   875.52MB 99.52%  testing.(*B).runN

cleanPathRaw is the current function and cleanPath is the one that uses byte pool.
The memory usage has been reduced to 15% after using a byte pool.

@appleboy
Copy link
Member

appleboy commented Jan 7, 2020

Add ref: julienschmidt/httprouter#292

@appleboy
Copy link
Member

appleboy commented Jan 7, 2020

@panjf2000
Copy link
Contributor Author

Actually julienschmidt/httprouter@7b49e86 is already added in this PR, only the julienschmidt/httprouter@8e4b52b is missed, I will add it now.

@appleboy
Copy link
Member

appleboy commented Jan 7, 2020

@panjf2000 thanks.

@panjf2000
Copy link
Contributor Author

@appleboy
Done.

Copy link
Member

@thinkerou thinkerou left a comment

Choose a reason for hiding this comment

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

LGTM

@thinkerou thinkerou merged commit 025950a into gin-gonic:master Jan 7, 2020
@panjf2000 panjf2000 deleted the path-bytes-pool branch January 7, 2020 09:40
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this pull request Feb 19, 2020
* path: use stack buffer in CleanPath to avoid allocs in common case

Sync from julienschmidt/httprouter@8222db1

* path: sync test code from httprouter

* path: update path_test.go to the latest code

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
byebyebruce pushed a commit to byebyebruce/gin that referenced this pull request Mar 25, 2020
* path: use stack buffer in CleanPath to avoid allocs in common case

Sync from julienschmidt/httprouter@8222db1

* path: sync test code from httprouter

* path: update path_test.go to the latest code

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants