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 root context in a sync pool to reduce GC: zero-alloc routing #33

Merged
merged 4 commits into from
Mar 31, 2016

Conversation

pkieltyka
Copy link
Member

This PR removes allocations during routing and speeds up overall performance.

Note: there is one breaking change, which is how URL params are referenced. Previously a handler would reference chi.URLParams(ctx)["id"] and now, its: chi.URLParam(ctx, "id")

New Benchmarks:

BenchmarkChi_Param          10000000           181 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Param5          3000000           570 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Param20         1000000          2057 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParamWrite      5000000           245 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubStatic    5000000           250 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubParam     2000000           589 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubAll         10000        102664 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusStatic    10000000           161 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusParam      5000000           291 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlus2Params    5000000           393 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusAll         300000          4335 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseStatic    10000000           162 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseParam     10000000           227 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Parse2Params    5000000           327 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseAll         200000          7368 ns/op           0 B/op          0 allocs/op
BenchmarkChi_StaticAll         30000         57990 ns/op           0 B/op          0 allocs/op

Old Benchmarks:

BenchmarkChi_Param           2000000           821 ns/op         408 B/op          6 allocs/op
BenchmarkChi_Param5          1000000          1478 ns/op         408 B/op          6 allocs/op
BenchmarkChi_Param20          200000          6595 ns/op        2259 B/op          8 allocs/op
BenchmarkChi_ParamWrite      2000000           954 ns/op         416 B/op          7 allocs/op
BenchmarkChi_GithubStatic    3000000           585 ns/op         120 B/op          5 allocs/op
BenchmarkChi_GithubParam     1000000          1286 ns/op         408 B/op          6 allocs/op
BenchmarkChi_GithubAll         10000        237874 ns/op       72456 B/op       1182 allocs/op
BenchmarkChi_GPlusStatic     3000000           495 ns/op         120 B/op          5 allocs/op
BenchmarkChi_GPlusParam      2000000           945 ns/op         408 B/op          6 allocs/op
BenchmarkChi_GPlus2Params    1000000          1100 ns/op         408 B/op          6 allocs/op
BenchmarkChi_GPlusAll         100000         12993 ns/op        4728 B/op         76 allocs/op
BenchmarkChi_ParseStatic     3000000           503 ns/op         120 B/op          5 allocs/op
BenchmarkChi_ParseParam      2000000           902 ns/op         408 B/op          6 allocs/op
BenchmarkChi_Parse2Params    1000000          1029 ns/op         408 B/op          6 allocs/op
BenchmarkChi_ParseAll         100000         21745 ns/op        7728 B/op        146 allocs/op
BenchmarkChi_StaticAll         10000        110299 ns/op       18840 B/op        785 allocs/op

@pkieltyka
Copy link
Member Author

btw.. for fun I ran the benchmarks on Go-tip (1.7)

BenchmarkChi_Param                  10000000           143 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Param5                  5000000           368 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Param20                 1000000          1336 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParamWrite             10000000           196 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubStatic           10000000           220 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubParam             3000000           445 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GithubAll                 20000         75896 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusStatic            10000000           140 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusParam             10000000           200 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlus2Params            5000000           287 ns/op           0 B/op          0 allocs/op
BenchmarkChi_GPlusAll                 500000          3036 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseStatic            10000000           145 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseParam             10000000           180 ns/op           0 B/op          0 allocs/op
BenchmarkChi_Parse2Params           10000000           234 ns/op           0 B/op          0 allocs/op
BenchmarkChi_ParseAll                 300000          5351 ns/op           0 B/op          0 allocs/op
BenchmarkChi_StaticAll                 30000         47330 ns/op           0 B/op          0 allocs/op

@pkieltyka pkieltyka merged commit c420d7f into master Mar 31, 2016
@pkieltyka pkieltyka deleted the zeroalloc branch March 31, 2016 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant