Skip to content

Commit 3a4c475

Browse files
Dean KarnDean Karn
authored andcommitted
Merge branch 'minor-perf-tests'
2 parents b643f12 + d9459b4 commit 3a4c475

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
##Pure
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/pure/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-2.5.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-2.6.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/pure/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/pure)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pure/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pure?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/pure)](https://goreportcard.com/report/github.com/go-playground/pure)
@@ -166,22 +166,22 @@ go test -bench=. -benchmem=true
166166
#Static Routes: 157
167167
Pure: 21224 Bytes
168168

169-
BenchmarkPure_Param 10000000 157 ns/op 240 B/op 1 allocs/op
170-
BenchmarkPure_Param5 10000000 208 ns/op 240 B/op 1 allocs/op
171-
BenchmarkPure_Param20 5000000 350 ns/op 240 B/op 1 allocs/op
172-
BenchmarkPure_ParamWrite 10000000 221 ns/op 240 B/op 1 allocs/op
173-
BenchmarkPure_GithubStatic 20000000 72.6 ns/op 0 B/op 0 allocs/op
174-
BenchmarkPure_GithubParam 10000000 230 ns/op 240 B/op 1 allocs/op
175-
BenchmarkPure_GithubAll 30000 43054 ns/op 40082 B/op 167 allocs/op
176-
BenchmarkPure_GPlusStatic 30000000 54.0 ns/op 0 B/op 0 allocs/op
177-
BenchmarkPure_GPlusParam 10000000 182 ns/op 240 B/op 1 allocs/op
178-
BenchmarkPure_GPlus2Params 10000000 207 ns/op 240 B/op 1 allocs/op
179-
BenchmarkPure_GPlusAll 1000000 2297 ns/op 2640 B/op 11 allocs/op
180-
BenchmarkPure_ParseStatic 30000000 56.2 ns/op 0 B/op 0 allocs/op
181-
BenchmarkPure_ParseParam 10000000 166 ns/op 240 B/op 1 allocs/op
182-
BenchmarkPure_Parse2Params 10000000 180 ns/op 240 B/op 1 allocs/op
183-
BenchmarkPure_ParseAll 500000 3671 ns/op 3840 B/op 16 allocs/op
184-
BenchmarkPure_StaticAll 100000 14646 ns/op 0 B/op 0 allocs/op
169+
BenchmarkPure_Param 10000000 161 ns/op 240 B/op 1 allocs/op
170+
BenchmarkPure_Param5 10000000 205 ns/op 240 B/op 1 allocs/op
171+
BenchmarkPure_Param20 5000000 356 ns/op 240 B/op 1 allocs/op
172+
BenchmarkPure_ParamWrite 10000000 215 ns/op 240 B/op 1 allocs/op
173+
BenchmarkPure_GithubStatic 30000000 47.1 ns/op 0 B/op 0 allocs/op
174+
BenchmarkPure_GithubParam 10000000 227 ns/op 240 B/op 1 allocs/op
175+
BenchmarkPure_GithubAll 30000 42284 ns/op 40082 B/op 167 allocs/op
176+
BenchmarkPure_GPlusStatic 50000000 31.7 ns/op 0 B/op 0 allocs/op
177+
BenchmarkPure_GPlusParam 10000000 178 ns/op 240 B/op 1 allocs/op
178+
BenchmarkPure_GPlus2Params 10000000 193 ns/op 240 B/op 1 allocs/op
179+
BenchmarkPure_GPlusAll 1000000 2159 ns/op 2640 B/op 11 allocs/op
180+
BenchmarkPure_ParseStatic 50000000 30.9 ns/op 0 B/op 0 allocs/op
181+
BenchmarkPure_ParseParam 10000000 159 ns/op 240 B/op 1 allocs/op
182+
BenchmarkPure_Parse2Params 10000000 174 ns/op 240 B/op 1 allocs/op
183+
BenchmarkPure_ParseAll 500000 3290 ns/op 3840 B/op 16 allocs/op
184+
BenchmarkPure_StaticAll 200000 9964 ns/op 0 B/op 0 allocs/op
185185
```
186186

187187
Package Versioning

node.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ func (n *node) insertChild(numParams uint8, existing existingParams, path string
314314
}
315315

316316
// Returns the handle registered with the given path (key).
317-
func (n *node) find(path string, po Params) (handler http.HandlerFunc, p Params) {
318-
319-
p = po
317+
func (n *node) find(path string, mux *Mux) (handler http.HandlerFunc, rv *requestVars) {
320318

321319
walk: // Outer loop for walking the tree
322320
for {
@@ -351,11 +349,16 @@ walk: // Outer loop for walking the tree
351349
end++
352350
}
353351

352+
if rv == nil {
353+
rv = mux.pool.Get().(*requestVars)
354+
rv.params = rv.params[0:0]
355+
}
356+
354357
// save param value
355-
i := len(p)
356-
p = p[:i+1] // expand slice within preallocated capacity
357-
p[i].Key = n.path[1:]
358-
p[i].Value = path[:end]
358+
i := len(rv.params)
359+
rv.params = rv.params[:i+1] // expand slice within preallocated capacity
360+
rv.params[i].Key = n.path[1:]
361+
rv.params[i].Value = path[:end]
359362

360363
// we need to go deeper!
361364
if end < len(path) {
@@ -380,11 +383,16 @@ walk: // Outer loop for walking the tree
380383

381384
case matchesAny:
382385

386+
if rv == nil {
387+
rv = mux.pool.Get().(*requestVars)
388+
rv.params = rv.params[0:0]
389+
}
390+
383391
// save param value
384-
i := len(p)
385-
p = p[:i+1] // expand slice within preallocated capacity
386-
p[i].Key = WildcardParam
387-
p[i].Value = path[1:]
392+
i := len(rv.params)
393+
rv.params = rv.params[:i+1] // expand slice within preallocated capacity
394+
rv.params[i].Key = WildcardParam
395+
rv.params[i].Value = path[1:]
388396

389397
handler = n.handler
390398
return

pure.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,10 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
201201
tree := p.trees[r.Method]
202202

203203
var h http.HandlerFunc
204-
rv := p.pool.Get().(*requestVars)
205-
rv.r = r
204+
var rv *requestVars
206205

207206
if tree != nil {
208-
if h, rv.params = tree.find(r.URL.Path, rv.params[0:0]); h == nil {
207+
if h, rv = tree.find(r.URL.Path, p); h == nil {
209208

210209
if p.redirectTrailingSlash && len(r.URL.Path) > 1 {
211210

@@ -215,7 +214,7 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
215214

216215
if lc != r.URL.Path {
217216

218-
if h, _ = tree.find(lc, rv.params[0:0]); h != nil {
217+
if h, _ = tree.find(lc, p); h != nil {
219218
r.URL.Path = lc
220219
h = p.redirect(r.Method, r.URL.String())
221220
r.URL.Path = orig
@@ -229,7 +228,7 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
229228
lc = lc + basePath
230229
}
231230

232-
if h, _ = tree.find(lc, rv.params[0:0]); h != nil {
231+
if h, _ = tree.find(lc, p); h != nil {
233232
r.URL.Path = lc
234233
h = p.redirect(r.Method, r.URL.String())
235234
r.URL.Path = orig
@@ -262,7 +261,7 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
262261
if m == r.Method || m == http.MethodOptions {
263262
continue
264263
}
265-
if h, _ = ctree.find(r.URL.Path, rv.params[0:0]); h != nil {
264+
if h, _ = ctree.find(r.URL.Path, p); h != nil {
266265
w.Header().Add(Allow, m)
267266
}
268267
}
@@ -284,7 +283,7 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
284283
continue
285284
}
286285

287-
if h, _ = ctree.find(r.URL.Path, rv.params[0:0]); h != nil {
286+
if h, _ = ctree.find(r.URL.Path, p); h != nil {
288287
w.Header().Add(Allow, m)
289288
found = true
290289
}
@@ -301,19 +300,22 @@ func (p *Mux) serveHTTP(w http.ResponseWriter, r *http.Request) {
301300

302301
END:
303302

304-
if len(rv.params) > 0 {
303+
if rv != nil {
305304

306305
rv.formParsed = false
306+
rv.r = r
307307

308308
// store on context
309309
r = r.WithContext(rv.ctx)
310310
}
311311

312312
h(w, r)
313313

314-
rv.queryParams = nil
315-
rv.r = nil
316-
p.pool.Put(rv)
314+
if rv != nil {
315+
rv.queryParams = nil
316+
rv.r = nil
317+
p.pool.Put(rv)
318+
}
317319
}
318320

319321
func (p *Mux) redirect(method string, to string) (h http.HandlerFunc) {

0 commit comments

Comments
 (0)