fiber/ctx_interface.go
RW ad64f2cd8f
Add NewWithCustomCtx initialization helper (#3476)
* Make custom context setup via NewWithCustomCtx

* Update docs/api/app.md

* fix lint issues

* small optimizations

* Fix perf 3476 (#3499)

* perf: modify app.pool

goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   old.txt   │              new.txt               │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   36.26n ± 0%   34.51n ± 0%  -4.83% (p=0.000 n=30)

                       │  old.txt   │            new.txt             │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  old.txt   │            new.txt             │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

* perf: optimize defaultCtx in app.next

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   new.txt   │              new4.txt               │
                       │   sec/op    │   sec/op     vs base                │
_Communication_Flow-10   34.51n ± 0%   30.02n ± 0%  -13.01% (p=0.000 n=30)

                       │  new.txt   │            new4.txt            │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  new.txt   │            new4.txt            │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

---------

Co-authored-by: Kashiwa <13825170+ksw2000@users.noreply.github.com>

* Improve performance #3476 (#3532)

* Make custom context setup via NewWithCustomCtx

* Update docs/api/app.md

* fix lint issues

* small optimizations

* perf: modify app.pool

goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   old.txt   │              new.txt               │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   36.26n ± 0%   34.51n ± 0%  -4.83% (p=0.000 n=30)

                       │  old.txt   │            new.txt             │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  old.txt   │            new.txt             │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

* perf: optimize defaultCtx in app.next

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │   new.txt   │              new4.txt               │
                       │   sec/op    │   sec/op     vs base                │
_Communication_Flow-10   34.51n ± 0%   30.02n ± 0%  -13.01% (p=0.000 n=30)

                       │  new.txt   │            new4.txt            │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  new.txt   │            new4.txt            │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

* perf: revert nextCustom

```
goos: darwin
goarch: arm64
pkg: github.com/gofiber/fiber/v3
cpu: Apple M4
                       │  head.txt   │            revert-1.txt            │
                       │   sec/op    │   sec/op     vs base               │
_Communication_Flow-10   30.04n ± 0%   28.74n ± 0%  -4.33% (p=0.000 n=30)

                       │  head.txt  │          revert-1.txt          │
                       │    B/op    │    B/op     vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal

                       │  head.txt  │          revert-1.txt          │
                       │ allocs/op  │ allocs/op   vs base            │
_Communication_Flow-10   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=30) ¹
¹ all samples are equal
```

---------

Co-authored-by: RW <rene@gofiber.io>

---------

Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
Co-authored-by: Kashiwa <13825170+ksw2000@users.noreply.github.com>
2025-06-23 12:47:52 +02:00

62 lines
1.3 KiB
Go

// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package fiber
import (
"errors"
"github.com/valyala/fasthttp"
)
type CustomCtx interface {
Ctx
// Reset is a method to reset context fields by given request when to use server handlers.
Reset(fctx *fasthttp.RequestCtx)
// Methods to use with next stack.
getMethodInt() int
getIndexRoute() int
getTreePathHash() int
getDetectionPath() string
getPathOriginal() string
getValues() *[maxParams]string
getMatched() bool
setIndexHandler(handler int)
setIndexRoute(route int)
setMatched(matched bool)
setRoute(route *Route)
}
func NewDefaultCtx(app *App) *DefaultCtx {
// return ctx
ctx := &DefaultCtx{
// Set app reference
app: app,
}
ctx.req = &DefaultReq{ctx: ctx}
ctx.res = &DefaultRes{ctx: ctx}
return ctx
}
// AcquireCtx retrieves a new Ctx from the pool.
func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) CustomCtx {
ctx, ok := app.pool.Get().(CustomCtx)
if !ok {
panic(errors.New("failed to type-assert to CustomCtx"))
}
ctx.Reset(fctx)
return ctx
}
// ReleaseCtx releases the ctx back into the pool.
func (app *App) ReleaseCtx(c CustomCtx) {
c.release()
app.pool.Put(c)
}