Skip to content

Commit 0bcf73f

Browse files
committed
Update benchmark
Also initialize the response headers with the default header in benchmarks as all reponse at least have one header. This removes the single allocation that should not be attributed to this library. Also add a micro-optimization on the critical path.
1 parent eacc8e8 commit 0bcf73f

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ See [API documentation](http://godoc.org/github.com/rs/cors) for more info.
106106
goos: darwin
107107
goarch: arm64
108108
pkg: github.com/rs/cors
109-
BenchmarkWithout-10 352671961 3.317 ns/op 0 B/op 0 allocs/op
110-
BenchmarkDefault-10 18261723 65.63 ns/op 0 B/op 0 allocs/op
111-
BenchmarkAllowedOrigin-10 13309591 90.21 ns/op 0 B/op 0 allocs/op
112-
BenchmarkPreflight-10 7247728 166.9 ns/op 0 B/op 0 allocs/op
113-
BenchmarkPreflightHeader-10 5915437 202.9 ns/op 0 B/op 0 allocs/op
114-
BenchmarkWildcard/match-10 250336476 4.784 ns/op 0 B/op 0 allocs/op
115-
BenchmarkWildcard/too_short-10 1000000000 0.6354 ns/op 0 B/op 0 allocs/op
109+
BenchmarkWithout-10 135325480 8.124 ns/op 0 B/op 0 allocs/op
110+
BenchmarkDefault-10 24082140 51.40 ns/op 0 B/op 0 allocs/op
111+
BenchmarkAllowedOrigin-10 16424518 88.25 ns/op 0 B/op 0 allocs/op
112+
BenchmarkPreflight-10 8010259 147.3 ns/op 0 B/op 0 allocs/op
113+
BenchmarkPreflightHeader-10 6850962 175.0 ns/op 0 B/op 0 allocs/op
114+
BenchmarkWildcard/match-10 253275342 4.714 ns/op 0 B/op 0 allocs/op
115+
BenchmarkWildcard/too_short-10 1000000000 0.6235 ns/op 0 B/op 0 allocs/op
116116
PASS
117-
ok github.com/rs/cors 9.613s
117+
ok github.com/rs/cors 99.131s
118118
```
119119

120120
## Licenses

bench_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ func BenchmarkPreflightHeader(b *testing.B) {
100100
func makeFakeResponses(n int) []*FakeResponse {
101101
resps := make([]*FakeResponse, n)
102102
for i := 0; i < n; i++ {
103-
resps[i] = &FakeResponse{http.Header{}}
103+
resps[i] = &FakeResponse{http.Header{
104+
"Content-Type": []string{"text/plain"},
105+
}}
104106
}
105107
return resps
106108
}

cors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ func (c *Cors) handleActualRequest(w http.ResponseWriter, r *http.Request) {
394394
allowed, additionalVaryHeaders := c.isOriginAllowed(r, origin)
395395

396396
// Always set Vary, see https://github.com/rs/cors/issues/10
397-
if vary, found := headers["Vary"]; found {
398-
headers["Vary"] = append(vary, headerVaryOrigin[0])
399-
} else {
397+
if vary := headers["Vary"]; vary == nil {
400398
headers["Vary"] = headerVaryOrigin
399+
} else {
400+
headers["Vary"] = append(vary, headerVaryOrigin[0])
401401
}
402402
if len(additionalVaryHeaders) > 0 {
403403
headers.Add("Vary", strings.Join(convert(additionalVaryHeaders, http.CanonicalHeaderKey), ", "))

0 commit comments

Comments
 (0)