Skip to content

Commit 49d4a78

Browse files
authored
Merge pull request #4231 from gofiber/optimize-performance
2 parents 771b689 + 993175d commit 49d4a78

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

ctx.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"maps"
1313
"mime/multipart"
14-
"strconv"
1514
"strings"
1615
"sync/atomic"
1716
"time"
@@ -109,7 +108,13 @@ func (c *DefaultCtx) BaseURL() string {
109108
if c.baseURI != "" {
110109
return c.baseURI
111110
}
112-
c.baseURI = c.Scheme() + "://" + c.Host()
111+
scheme := c.Scheme()
112+
host := c.Host()
113+
buf := make([]byte, 0, len(scheme)+len("://")+len(host))
114+
buf = append(buf, scheme...)
115+
buf = append(buf, "://"...)
116+
buf = append(buf, host...)
117+
c.baseURI = c.app.toString(buf)
113118
return c.baseURI
114119
}
115120

@@ -574,13 +579,14 @@ func (c *DefaultCtx) String() string {
574579

575580
// Start with the ID, converting it to a hex string without fmt.Sprintf
576581
buf.WriteByte('#')
577-
// Convert ID to hexadecimal
578-
id := strconv.FormatUint(c.fasthttp.ID(), 16)
579-
// Pad with leading zeros to ensure 16 characters
580-
for i := 0; i < (16 - len(id)); i++ {
581-
buf.WriteByte('0')
582-
}
583-
buf.WriteString(id)
582+
const hex = "0123456789abcdef"
583+
var id [16]byte
584+
ctxID := c.fasthttp.ID()
585+
for i := len(id) - 1; i >= 0; i-- {
586+
id[i] = hex[ctxID&0xf]
587+
ctxID >>= 4
588+
}
589+
buf.Write(id[:])
584590
buf.WriteString(" - ")
585591

586592
// Add local and remote addresses directly

ctx_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,35 @@ func Benchmark_Ctx_BaseURL(b *testing.B) {
806806
require.Equal(b, "http://google.com:1337", res)
807807
}
808808

809+
func Benchmark_Ctx_BaseURL_Uncached(b *testing.B) {
810+
app := New()
811+
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck,forcetypeassert // not needed
812+
813+
c.Request().SetHost("google.com:1337")
814+
c.Request().URI().SetPath("/haha/oke/lol")
815+
var res string
816+
b.ReportAllocs()
817+
for b.Loop() {
818+
c.baseURI = ""
819+
res = c.BaseURL()
820+
}
821+
require.Equal(b, "http://google.com:1337", res)
822+
}
823+
824+
func Benchmark_Ctx_FullURL(b *testing.B) {
825+
app := New()
826+
c := app.AcquireCtx(&fasthttp.RequestCtx{}).(*DefaultCtx) //nolint:errcheck,forcetypeassert // not needed
827+
828+
c.Request().SetRequestURI("/haha/oke/lol?name=fiber")
829+
c.Request().URI().SetHost("google.com:1337")
830+
var res string
831+
b.ReportAllocs()
832+
for b.Loop() {
833+
res = c.FullURL()
834+
}
835+
require.Equal(b, "http://google.com:1337/haha/oke/lol?name=fiber", res)
836+
}
837+
809838
// go test -run Test_Ctx_Body
810839
func Test_Ctx_Body(t *testing.T) {
811840
t.Parallel()

router.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ func (r *Route) match(detectionPath, path string, params *[maxParams]string) boo
196196
// Does this route have parameters?
197197
if len(r.Params) > 0 {
198198
// Match params using precomputed routeParser
199-
if r.routeParser.getMatch(detectionPath, path, params, r.use) {
200-
return true
201-
}
199+
return r.routeParser.getMatch(detectionPath, path, params, r.use)
202200
}
203201

204202
// Middleware route?

0 commit comments

Comments
 (0)