Skip to content

Commit

Permalink
Replace Sprintf with byebufferpool in ctx.String()
Browse files Browse the repository at this point in the history
# Original fn using Sprintf
Benchmark_Ctx_String-24          3846717               318.0 ns/op           152 B/op          8 allocs/op
Benchmark_Ctx_String-24          3780208               315.9 ns/op           152 B/op          8 allocs/op
Benchmark_Ctx_String-24          3627513               315.1 ns/op           152 B/op          8 allocs/op
Benchmark_Ctx_String-24          3712863               317.4 ns/op           152 B/op          8 allocs/op

// Modified using bytebufferpool
Benchmark_Ctx_String-24          8131666               149.3 ns/op            96 B/op          5 allocs/op
Benchmark_Ctx_String-24          7626406               148.3 ns/op            96 B/op          5 allocs/op
Benchmark_Ctx_String-24          8194621               149.2 ns/op            96 B/op          5 allocs/op
Benchmark_Ctx_String-24          8297750               156.6 ns/op            96 B/op          5 allocs/op
  • Loading branch information
Fenny committed Feb 9, 2024
1 parent fafd1ec commit 3e90ca8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
41 changes: 33 additions & 8 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,14 +1605,39 @@ func (c *DefaultCtx) Status(status int) Ctx {
//
// The returned value may be useful for logging.
func (c *DefaultCtx) String() string {
return fmt.Sprintf(
"#%016X - %s <-> %s - %s %s",
c.fasthttp.ID(),
c.fasthttp.LocalAddr(),
c.fasthttp.RemoteAddr(),
c.fasthttp.Request.Header.Method(),
c.fasthttp.URI().FullURI(),
)
// Get buffer from pool
buf := bytebufferpool.Get()

// Start with the ID, converting it to a hex string without fmt.Sprintf
buf.WriteByte('#')
// Convert ID to hexadecimal
id := strconv.FormatUint(uint64(c.fasthttp.ID()), 16)
// Pad with leading zeros to ensure 16 characters
for i := 0; i < (16 - len(id)); i++ {
buf.WriteByte('0')
}
buf.WriteString(id)
buf.WriteString(" - ")

// Add local and remote addresses directly
buf.WriteString(c.fasthttp.LocalAddr().String())
buf.WriteString(" <-> ")
buf.WriteString(c.fasthttp.RemoteAddr().String())
buf.WriteString(" - ")

// Add method and URI
buf.Write(c.fasthttp.Request.Header.Method())
buf.WriteByte(' ')
buf.Write(c.fasthttp.URI().FullURI())

// Allocate string
str := buf.String()

// Reset buffer
buf.Reset()
bytebufferpool.Put(buf)

return str
}

// Type sets the Content-Type HTTP header to the MIME type specified by the file extension.
Expand Down
14 changes: 14 additions & 0 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4631,6 +4631,20 @@ func Test_Ctx_String(t *testing.T) {
require.Equal(t, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", c.String())
}

// go test -v -run=^$ -bench=Benchmark_Ctx_String -benchmem -count=4
func Benchmark_Ctx_String(b *testing.B) {
var str string
app := New()
ctx := app.NewCtx(&fasthttp.RequestCtx{})
b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
str = ctx.String()
}
require.Equal(b, "#0000000000000000 - 0.0.0.0:0 <-> 0.0.0.0:0 - GET http:///", str)
}

func TestCtx_ParamsInt(t *testing.T) {
// Create a test context and set some strings (or params)
// create a fake app to be used within this test
Expand Down

0 comments on commit 3e90ca8

Please sign in to comment.