From 594cad69db74c19bc2b41b84c61c3d5720e5fa3f Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 14 Mar 2024 00:06:20 +0100 Subject: [PATCH] Improved performance --- Context.go | 11 ----------- README.md | 4 ++-- Server.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/Context.go b/Context.go index 0f35cd3..30b9358 100644 --- a/Context.go +++ b/Context.go @@ -40,17 +40,6 @@ type ctx struct { handlerCount int } -// newContext returns a new context from the pool. -func newContext(req *http.Request, res http.ResponseWriter, server *Server) *ctx { - ctx := contextPool.Get().(*ctx) - ctx.request = req - ctx.response = res - ctx.server = server - ctx.paramCount = 0 - ctx.handlerCount = 0 - return ctx -} - // Bytes responds with a raw byte slice. func (ctx *ctx) Bytes(body []byte) error { _, err := ctx.response.Write(body) diff --git a/README.md b/README.md index 1af75f5..ea7a12e 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,8 @@ coverage: 100.0% of statements ## Benchmarks ``` -BenchmarkHello-12 31128832 38.41 ns/op 0 B/op 0 allocs/op -BenchmarkGitHub-12 17406580 68.56 ns/op 0 B/op 0 allocs/op +BenchmarkHello-12 35983602 33.28 ns/op 0 B/op 0 allocs/op +BenchmarkGitHub-12 18320769 68.66 ns/op 0 B/op 0 allocs/op ``` ## License diff --git a/Server.go b/Server.go index 1042ee4..80357cb 100644 --- a/Server.go +++ b/Server.go @@ -61,15 +61,20 @@ func (server *Server) Put(path string, handler Handler) { // ServeHTTP responds to the given request. func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) { - ctx := newContext(request, response, server) - defer contextPool.Put(ctx) - + ctx := contextPool.Get().(*ctx) + ctx.request = request + ctx.response = response + ctx.server = server err := server.handlers[0](ctx) if err != nil { response.(io.StringWriter).WriteString(err.Error()) log.Println(request.URL, err) } + + ctx.paramCount = 0 + ctx.handlerCount = 0 + contextPool.Put(ctx) } // Run starts the server on the given address.