From c6b37ce904863c771b069a97397c814c3bb95e32 Mon Sep 17 00:00:00 2001 From: Tuomas Makinen Date: Fri, 3 Feb 2017 23:47:52 +0200 Subject: [PATCH] Change unnecessary string comparison to length check in Go benchmarks Because router already guarantees that URL starts with '/', it's unnecessary to compare the path string further for 404 check. One may as well check that it's length matches expected one character. Doesn't make huge difference on Go standard library version, but gives couple thousand req/s more for fasthttp version. Also made 404 response writing a cleaner, while at there. --- benchmarks/golang-fasthttp/micro.go | 5 ++--- benchmarks/golang/micro.go | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/benchmarks/golang-fasthttp/micro.go b/benchmarks/golang-fasthttp/micro.go index 8961104..97b1c2d 100644 --- a/benchmarks/golang-fasthttp/micro.go +++ b/benchmarks/golang-fasthttp/micro.go @@ -3,9 +3,8 @@ package main import "github.com/valyala/fasthttp" func hello(ctx *fasthttp.RequestCtx) { - if string(ctx.Path()) != "/" { - ctx.SetStatusCode(404) - ctx.WriteString("Not Found") + if len(ctx.Path()) != 1 { + ctx.Error("Not Found", fasthttp.StatusNotFound) return } ctx.WriteString("Hello world!") diff --git a/benchmarks/golang/micro.go b/benchmarks/golang/micro.go index 52062c6..6d236a6 100644 --- a/benchmarks/golang/micro.go +++ b/benchmarks/golang/micro.go @@ -8,9 +8,8 @@ var ( ) func hello(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - w.WriteHeader(http.StatusNotFound) - w.Write(notFoundResp) + if len(r.URL.Path) != 1 { + http.NotFound(w, r) return } w.Write(helloResp)