Skip to content

Commit edcbec2

Browse files
Dean KarnDean Karn
authored andcommitted
remove QueryParams(...) function from RequestVars
- storing was no faster than reprising every time and now don't have to incur the memory to store; made the implementation simpler and a little faster.
1 parent 8a1bc7d commit edcbec2

File tree

5 files changed

+22
-69
lines changed

5 files changed

+22
-69
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.7.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.0.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 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
169+
BenchmarkPure_Param 10000000 156 ns/op 240 B/op 1 allocs/op
170+
BenchmarkPure_Param5 10000000 199 ns/op 240 B/op 1 allocs/op
171+
BenchmarkPure_Param20 5000000 349 ns/op 240 B/op 1 allocs/op
172+
BenchmarkPure_ParamWrite 10000000 209 ns/op 240 B/op 1 allocs/op
173+
BenchmarkPure_GithubStatic 30000000 45.4 ns/op 0 B/op 0 allocs/op
174+
BenchmarkPure_GithubParam 10000000 219 ns/op 240 B/op 1 allocs/op
175+
BenchmarkPure_GithubAll 30000 40244 ns/op 40082 B/op 167 allocs/op
176+
BenchmarkPure_GPlusStatic 50000000 30.4 ns/op 0 B/op 0 allocs/op
177+
BenchmarkPure_GPlusParam 10000000 173 ns/op 240 B/op 1 allocs/op
178+
BenchmarkPure_GPlus2Params 10000000 190 ns/op 240 B/op 1 allocs/op
179+
BenchmarkPure_GPlusAll 1000000 2108 ns/op 2640 B/op 11 allocs/op
180+
BenchmarkPure_ParseStatic 50000000 29.9 ns/op 0 B/op 0 allocs/op
181+
BenchmarkPure_ParseParam 10000000 154 ns/op 240 B/op 1 allocs/op
182+
BenchmarkPure_Parse2Params 10000000 167 ns/op 240 B/op 1 allocs/op
183+
BenchmarkPure_ParseAll 500000 3209 ns/op 3840 B/op 16 allocs/op
184+
BenchmarkPure_StaticAll 200000 9881 ns/op 0 B/op 0 allocs/op
185185
```
186186

187187
Package Versioning

helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func RequestVars(r *http.Request) ReqVars {
1515

1616
rv := r.Context().Value(defaultContextIdentifier)
1717
if rv == nil {
18-
return &requestVars{r: r}
18+
return new(requestVars)
1919
}
2020

2121
return rv.(*requestVars)

helpers_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ func TestNoRequestVars(t *testing.T) {
4545
Equal(t, code, http.StatusOK)
4646
}
4747

48-
func TestQueryParams(t *testing.T) {
49-
50-
qp1 := func(next http.HandlerFunc) http.HandlerFunc {
51-
return func(w http.ResponseWriter, r *http.Request) {
52-
rv := RequestVars(r)
53-
w.Write([]byte(rv.QueryParams().Get("test")))
54-
next(w, r)
55-
}
56-
}
57-
58-
qp2 := func(w http.ResponseWriter, r *http.Request) {
59-
rv := RequestVars(r)
60-
w.Write([]byte(rv.QueryParams().Get("test")))
61-
}
62-
63-
p := New()
64-
p.Use(qp1)
65-
p.Get("/home/:test", qp2)
66-
67-
code, body := request(http.MethodGet, "/home/testval?test=val", p)
68-
Equal(t, code, http.StatusOK)
69-
Equal(t, body, "valval")
70-
}
71-
7248
func TestDecode(t *testing.T) {
7349

7450
type TestStruct struct {

pure.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ END:
303303
if rv != nil {
304304

305305
rv.formParsed = false
306-
rv.r = r
307306

308307
// store on context
309308
r = r.WithContext(rv.ctx)
@@ -312,8 +311,6 @@ END:
312311
h(w, r)
313312

314313
if rv != nil {
315-
rv.queryParams = nil
316-
rv.r = nil
317314
p.pool.Put(rv)
318315
}
319316
}

request_vars.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
package pure
22

3-
import (
4-
"context"
5-
"net/http"
6-
"net/url"
7-
)
3+
import "context"
84

95
// ReqVars is the interface of request scoped variables
106
// tracked by pure
117
type ReqVars interface {
128
URLParam(pname string) string
13-
QueryParams() url.Values
149
}
1510

1611
type requestVars struct {
17-
r *http.Request
18-
ctx context.Context // holds a copy of it's parent requestVars
19-
params Params
20-
queryParams url.Values
21-
formParsed bool
12+
ctx context.Context // holds a copy of it's parent requestVars
13+
params Params
14+
formParsed bool
2215
}
2316

2417
// Params returns the current routes Params
2518
func (r *requestVars) URLParam(pname string) string {
2619
return r.params.Get(pname)
2720
}
28-
29-
// QueryParams returns the http.Request.URL Query Params
30-
// reason for storing is if http.Request.URL.Query() is called
31-
// more than once, it reparses all over again creating a new map and all.
32-
// callin this will cache and on subsequent call will not reparse
33-
func (r *requestVars) QueryParams() url.Values {
34-
35-
if r.queryParams == nil {
36-
r.queryParams = r.r.URL.Query()
37-
}
38-
39-
return r.queryParams
40-
}

0 commit comments

Comments
 (0)