Improved performance
This commit is contained in:
parent
00c85d5e15
commit
b30dad77dc
4 changed files with 27 additions and 22 deletions
25
Context.go
25
Context.go
|
@ -7,9 +7,6 @@ import (
|
|||
"unsafe"
|
||||
)
|
||||
|
||||
// maxParams defines the maximum number of parameters per route.
|
||||
const maxParams = 16
|
||||
|
||||
// Context represents the interface for a request & response context.
|
||||
type Context interface {
|
||||
Bytes([]byte) error
|
||||
|
@ -36,15 +33,13 @@ type ctx struct {
|
|||
request *http.Request
|
||||
response http.ResponseWriter
|
||||
server *server
|
||||
paramNames [maxParams]string
|
||||
paramValues [maxParams]string
|
||||
paramCount int
|
||||
handlerCount int
|
||||
params []param
|
||||
handlerCount uint8
|
||||
}
|
||||
|
||||
// Bytes responds with a raw byte slice.
|
||||
func (ctx *ctx) Bytes(body []byte) error {
|
||||
_, err := ctx.response.Write(body)
|
||||
func (c *ctx) Bytes(body []byte) error {
|
||||
_, err := c.response.Write(body)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -66,9 +61,11 @@ func (ctx *ctx) Error(messages ...any) error {
|
|||
|
||||
// Get retrieves a parameter.
|
||||
func (ctx *ctx) Get(param string) string {
|
||||
for i := range ctx.paramCount {
|
||||
if ctx.paramNames[i] == param {
|
||||
return ctx.paramValues[i]
|
||||
for i := range len(ctx.params) {
|
||||
p := ctx.params[i]
|
||||
|
||||
if p.Name == param {
|
||||
return p.Value
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,7 +148,5 @@ func (ctx *ctx) WriteString(body string) (int, error) {
|
|||
|
||||
// addParameter adds a new parameter to the context.
|
||||
func (ctx *ctx) addParameter(name string, value string) {
|
||||
ctx.paramNames[ctx.paramCount] = name
|
||||
ctx.paramValues[ctx.paramCount] = value
|
||||
ctx.paramCount++
|
||||
ctx.params = append(ctx.params, param{name, value})
|
||||
}
|
||||
|
|
10
README.md
10
README.md
|
@ -50,11 +50,11 @@ coverage: 100.0% of statements
|
|||
## Benchmarks
|
||||
|
||||
```
|
||||
BenchmarkStatic/#00-12 33616044 33.82 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkStatic/hello-12 26220148 43.75 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkStatic/hello/world-12 19777713 58.89 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub/gists/:id-12 20842587 56.36 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub/repos/:a/:b-12 17875575 65.04 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkStatic/#00-12 34975490 33.63 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkStatic/hello-12 26550235 44.20 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkStatic/hello/world-12 20356144 59.08 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub/gists/:id-12 21693214 54.80 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkGitHub/repos/:a/:b-12 18118347 65.33 ns/op 0 B/op 0 allocs/op
|
||||
```
|
||||
|
||||
## License
|
||||
|
|
|
@ -57,7 +57,10 @@ func New() Server {
|
|||
}
|
||||
|
||||
s.pool.New = func() any {
|
||||
return &ctx{server: s}
|
||||
return &ctx{
|
||||
server: s,
|
||||
params: make([]param, 0, 8),
|
||||
}
|
||||
}
|
||||
|
||||
return s
|
||||
|
@ -94,7 +97,7 @@ func (s *server) ServeHTTP(response http.ResponseWriter, request *http.Request)
|
|||
s.errorHandler(ctx, err)
|
||||
}
|
||||
|
||||
ctx.paramCount = 0
|
||||
ctx.params = ctx.params[:0]
|
||||
ctx.handlerCount = 0
|
||||
s.pool.Put(ctx)
|
||||
}
|
||||
|
|
7
param.go
Normal file
7
param.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package server
|
||||
|
||||
// param represents a URL parameter in a route like `/user/:id`.
|
||||
type param struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue