Skip to content

Commit ef40c04

Browse files
ksw2000gaby
andauthored
♻️ Refactor: reduce DefaultCtx from 768 bytes to 736 bytes (#3353)
* ♻️ Refactor: reduce DefaultCtx from 768 bytes to 736 bytes * ♻️ Refactor: add comments --------- Co-authored-by: Juan Calderon-Perez <[email protected]>
1 parent e947e03 commit ef40c04

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

ctx.go

+34-35
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,26 @@ const userContextKey contextKey = 0 // __local_user_context__
4949
//
5050
//go:generate ifacemaker --file ctx.go --struct DefaultCtx --iface Ctx --pkg fiber --output ctx_interface_gen.go --not-exported true --iface-comment "Ctx represents the Context which hold the HTTP request and response.\nIt has methods for the request query string, parameters, body, HTTP headers and so on."
5151
type DefaultCtx struct {
52-
app *App // Reference to *App
53-
route *Route // Reference to *Route
54-
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
55-
bind *Bind // Default bind reference
56-
redirect *Redirect // Default redirect reference
57-
req *DefaultReq // Default request api reference
58-
res *DefaultRes // Default response api reference
59-
values [maxParams]string // Route parameter values
60-
viewBindMap sync.Map // Default view map to bind template engine
61-
method string // HTTP method
62-
baseURI string // HTTP base uri
63-
path string // HTTP path with the modifications by the configuration -> string copy from pathBuffer
64-
detectionPath string // Route detection path -> string copy from detectionPathBuffer
65-
treePath string // Path for the search in the tree
66-
pathOriginal string // Original HTTP path
67-
pathBuffer []byte // HTTP path buffer
68-
detectionPathBuffer []byte // HTTP detectionPath buffer
69-
flashMessages redirectionMsgs // Flash messages
70-
indexRoute int // Index of the current route
71-
indexHandler int // Index of the current handler
72-
methodINT int // HTTP method INT equivalent
73-
matched bool // Non use route matched
52+
app *App // Reference to *App
53+
route *Route // Reference to *Route
54+
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
55+
bind *Bind // Default bind reference
56+
redirect *Redirect // Default redirect reference
57+
req *DefaultReq // Default request api reference
58+
res *DefaultRes // Default response api reference
59+
values [maxParams]string // Route parameter values
60+
viewBindMap sync.Map // Default view map to bind template engine
61+
method string // HTTP method
62+
baseURI string // HTTP base uri
63+
treePath string // Path for the search in the tree
64+
pathOriginal string // Original HTTP path
65+
flashMessages redirectionMsgs // Flash messages
66+
path []byte // HTTP path with the modifications by the configuration
67+
detectionPath []byte // Route detection path
68+
indexRoute int // Index of the current route
69+
indexHandler int // Index of the current handler
70+
methodINT int // HTTP method INT equivalent
71+
matched bool // Non use route matched
7472
}
7573

7674
// SendFile defines configuration options when to transfer file with SendFile.
@@ -1123,8 +1121,9 @@ func Params[V GenericType](c Ctx, key string, defaultValue ...V) V {
11231121

11241122
// Path returns the path part of the request URL.
11251123
// Optionally, you could override the path.
1124+
// Make copies or use the Immutable setting to use the value outside the Handler.
11261125
func (c *DefaultCtx) Path(override ...string) string {
1127-
if len(override) != 0 && c.path != override[0] {
1126+
if len(override) != 0 && string(c.path) != override[0] {
11281127
// Set new path to context
11291128
c.pathOriginal = override[0]
11301129

@@ -1133,7 +1132,7 @@ func (c *DefaultCtx) Path(override ...string) string {
11331132
// Prettify path
11341133
c.configDependentPaths()
11351134
}
1136-
return c.path
1135+
return c.app.getString(c.path)
11371136
}
11381137

11391138
// Scheme contains the request protocol string: http or https for TLS requests.
@@ -1832,32 +1831,32 @@ func (c *DefaultCtx) XHR() bool {
18321831
// configDependentPaths set paths for route recognition and prepared paths for the user,
18331832
// here the features for caseSensitive, decoded paths, strict paths are evaluated
18341833
func (c *DefaultCtx) configDependentPaths() {
1835-
c.pathBuffer = append(c.pathBuffer[0:0], c.pathOriginal...)
1834+
c.path = append(c.path[:0], c.pathOriginal...)
18361835
// If UnescapePath enabled, we decode the path and save it for the framework user
18371836
if c.app.config.UnescapePath {
1838-
c.pathBuffer = fasthttp.AppendUnquotedArg(c.pathBuffer[:0], c.pathBuffer)
1837+
c.path = fasthttp.AppendUnquotedArg(c.path[:0], c.path)
18391838
}
1840-
c.path = c.app.getString(c.pathBuffer)
18411839

18421840
// another path is specified which is for routing recognition only
18431841
// use the path that was changed by the previous configuration flags
1844-
c.detectionPathBuffer = append(c.detectionPathBuffer[0:0], c.pathBuffer...)
1842+
c.detectionPath = append(c.detectionPath[:0], c.path...)
18451843
// If CaseSensitive is disabled, we lowercase the original path
18461844
if !c.app.config.CaseSensitive {
1847-
c.detectionPathBuffer = utils.ToLowerBytes(c.detectionPathBuffer)
1845+
c.detectionPath = utils.ToLowerBytes(c.detectionPath)
18481846
}
18491847
// If StrictRouting is disabled, we strip all trailing slashes
1850-
if !c.app.config.StrictRouting && len(c.detectionPathBuffer) > 1 && c.detectionPathBuffer[len(c.detectionPathBuffer)-1] == '/' {
1851-
c.detectionPathBuffer = utils.TrimRight(c.detectionPathBuffer, '/')
1848+
if !c.app.config.StrictRouting && len(c.detectionPath) > 1 && c.detectionPath[len(c.detectionPath)-1] == '/' {
1849+
c.detectionPath = utils.TrimRight(c.detectionPath, '/')
18521850
}
1853-
c.detectionPath = c.app.getString(c.detectionPathBuffer)
18541851

18551852
// Define the path for dividing routes into areas for fast tree detection, so that fewer routes need to be traversed,
18561853
// since the first three characters area select a list of routes
1857-
c.treePath = c.treePath[0:0]
1854+
c.treePath = ""
18581855
const maxDetectionPaths = 3
18591856
if len(c.detectionPath) >= maxDetectionPaths {
1860-
c.treePath = c.detectionPath[:maxDetectionPaths]
1857+
// c.treePath is only used by Fiber and is not exposed to the user
1858+
// so we can use utils.UnsafeString instead of c.app.getString
1859+
c.treePath = utils.UnsafeString(c.detectionPath[:maxDetectionPaths])
18611860
}
18621861
}
18631862

@@ -1963,7 +1962,7 @@ func (c *DefaultCtx) getTreePath() string {
19631962
}
19641963

19651964
func (c *DefaultCtx) getDetectionPath() string {
1966-
return c.detectionPath
1965+
return c.app.getString(c.detectionPath)
19671966
}
19681967

19691968
func (c *DefaultCtx) getPathOriginal() string {

router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
180180
}
181181

182182
// Check if it matches the request path
183-
match = route.match(c.detectionPath, c.path, &c.values)
183+
match = route.match(utils.UnsafeString(c.detectionPath), utils.UnsafeString(c.path), &c.values)
184184
if !match {
185185
// No match, next route
186186
continue

0 commit comments

Comments
 (0)