Improved performance
This commit is contained in:
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})
|
||||
}
|
||||
|
Reference in New Issue
Block a user