Improved performance

This commit is contained in:
Eduard Urbach 2024-03-14 15:11:00 +01:00
parent fe184c0892
commit 559f8c8456
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 27 additions and 22 deletions

View File

@ -7,9 +7,6 @@ import (
"unsafe" "unsafe"
) )
// maxParams defines the maximum number of parameters per route.
const maxParams = 16
// Context represents the interface for a request & response context. // Context represents the interface for a request & response context.
type Context interface { type Context interface {
Bytes([]byte) error Bytes([]byte) error
@ -36,15 +33,13 @@ type ctx struct {
request *http.Request request *http.Request
response http.ResponseWriter response http.ResponseWriter
server *server server *server
paramNames [maxParams]string params []param
paramValues [maxParams]string handlerCount uint8
paramCount int
handlerCount int
} }
// Bytes responds with a raw byte slice. // Bytes responds with a raw byte slice.
func (ctx *ctx) Bytes(body []byte) error { func (c *ctx) Bytes(body []byte) error {
_, err := ctx.response.Write(body) _, err := c.response.Write(body)
return err return err
} }
@ -66,9 +61,11 @@ func (ctx *ctx) Error(messages ...any) error {
// Get retrieves a parameter. // Get retrieves a parameter.
func (ctx *ctx) Get(param string) string { func (ctx *ctx) Get(param string) string {
for i := range ctx.paramCount { for i := range len(ctx.params) {
if ctx.paramNames[i] == param { p := ctx.params[i]
return ctx.paramValues[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. // addParameter adds a new parameter to the context.
func (ctx *ctx) addParameter(name string, value string) { func (ctx *ctx) addParameter(name string, value string) {
ctx.paramNames[ctx.paramCount] = name ctx.params = append(ctx.params, param{name, value})
ctx.paramValues[ctx.paramCount] = value
ctx.paramCount++
} }

View File

@ -50,11 +50,11 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkStatic/#00-12 33616044 33.82 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 26220148 43.75 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 19777713 58.89 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 20842587 56.36 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 17875575 65.04 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 ## License

View File

@ -57,7 +57,10 @@ func New() Server {
} }
s.pool.New = func() any { s.pool.New = func() any {
return &ctx{server: s} return &ctx{
server: s,
params: make([]param, 0, 8),
}
} }
return s return s
@ -94,7 +97,7 @@ func (s *server) ServeHTTP(response http.ResponseWriter, request *http.Request)
s.errorHandler(ctx, err) s.errorHandler(ctx, err)
} }
ctx.paramCount = 0 ctx.params = ctx.params[:0]
ctx.handlerCount = 0 ctx.handlerCount = 0
s.pool.Put(ctx) s.pool.Put(ctx)
} }

7
param.go Normal file
View 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
}