Skip to content

Commit f7d9f51

Browse files
authored
optimization: struct alignment (#2636)
(#2632)
1 parent 88c379f commit f7d9f51

8 files changed

+35
-37
lines changed

binder.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ import (
6969
type BindingError struct {
7070
// Field is the field name where value binding failed
7171
Field string `json:"field"`
72+
*HTTPError
7273
// Values of parameter that failed to bind.
7374
Values []string `json:"-"`
74-
*HTTPError
7575
}
7676

7777
// NewBindingError creates new instance of binding error
@@ -94,16 +94,15 @@ func (be *BindingError) Error() string {
9494

9595
// ValueBinder provides utility methods for binding query or path parameter to various Go built-in types
9696
type ValueBinder struct {
97-
// failFast is flag for binding methods to return without attempting to bind when previous binding already failed
98-
failFast bool
99-
errors []error
100-
10197
// ValueFunc is used to get single parameter (first) value from request
10298
ValueFunc func(sourceParam string) string
10399
// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
104100
ValuesFunc func(sourceParam string) []string
105101
// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
106102
ErrorFunc func(sourceParam string, values []string, message interface{}, internalError error) error
103+
errors []error
104+
// failFast is flag for binding methods to return without attempting to bind when previous binding already failed
105+
failFast bool
107106
}
108107

109108
// QueryParamsBinder creates query parameter value binder

context.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,31 +200,31 @@ type Context interface {
200200
}
201201

202202
type context struct {
203+
logger Logger
203204
request *http.Request
204205
response *Response
205206
query url.Values
206207
echo *Echo
207-
logger Logger
208208

209209
store Map
210210
lock sync.RWMutex
211211

212212
// following fields are set by Router
213+
handler HandlerFunc
213214

214215
// path is route path that Router matched. It is empty string where there is no route match.
215216
// Route registered with RouteNotFound is considered as a match and path therefore is not empty.
216217
path string
217218

218-
// pnames length is tied to param count for the matched route
219-
pnames []string
220-
221219
// Usually echo.Echo is sizing pvalues but there could be user created middlewares that decide to
222220
// overwrite parameter by calling SetParamNames + SetParamValues.
223221
// When echo.Echo allocated that slice it length/capacity is tied to echo.Echo.maxParam value.
224222
//
225223
// It is important that pvalues size is always equal or bigger to pnames length.
226224
pvalues []string
227-
handler HandlerFunc
225+
226+
// pnames length is tied to param count for the matched route
227+
pnames []string
228228
}
229229

230230
const (

echo.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ type Echo struct {
9191
Listener net.Listener
9292
TLSListener net.Listener
9393
AutoTLSManager autocert.Manager
94-
DisableHTTP2 bool
95-
Debug bool
96-
HideBanner bool
97-
HidePort bool
9894
HTTPErrorHandler HTTPErrorHandler
9995
Binder Binder
10096
JSONSerializer JSONSerializer
@@ -106,6 +102,10 @@ type Echo struct {
106102

107103
// OnAddRouteHandler is called when Echo adds new route to specific host router.
108104
OnAddRouteHandler func(host string, route Route, handler HandlerFunc, middleware []MiddlewareFunc)
105+
DisableHTTP2 bool
106+
Debug bool
107+
HideBanner bool
108+
HidePort bool
109109
}
110110

111111
// Route contains a handler and information for matching against requests.
@@ -117,9 +117,9 @@ type Route struct {
117117

118118
// HTTPError represents an error that occurred while handling a request.
119119
type HTTPError struct {
120-
Code int `json:"-"`
121-
Message interface{} `json:"message"`
122120
Internal error `json:"-"` // Stores the error returned by an external dependency
121+
Message interface{} `json:"message"`
122+
Code int `json:"-"`
123123
}
124124

125125
// MiddlewareFunc defines a function to process middleware.

echo_fs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func StaticFileHandler(file string, filesystem fs.FS) HandlerFunc {
102102
// traverse up from current executable run path.
103103
// NB: private because you really should use fs.FS implementation instances
104104
type defaultFS struct {
105-
prefix string
106105
fs fs.FS
106+
prefix string
107107
}
108108

109109
func newDefaultFS() *defaultFS {

group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type Group struct {
1414
common
1515
host string
1616
prefix string
17-
middleware []MiddlewareFunc
1817
echo *Echo
18+
middleware []MiddlewareFunc
1919
}
2020

2121
// Use implements `Echo#Use()` for sub-routes within the Group.

ip.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ Private IPv6 address ranges:
134134
*/
135135

136136
type ipChecker struct {
137+
trustExtraRanges []*net.IPNet
137138
trustLoopback bool
138139
trustLinkLocal bool
139140
trustPrivateNet bool
140-
trustExtraRanges []*net.IPNet
141141
}
142142

143143
// TrustOption is config for which IP address to trust

response.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
// by an HTTP handler to construct an HTTP response.
1515
// See: https://golang.org/pkg/net/http/#ResponseWriter
1616
type Response struct {
17+
Writer http.ResponseWriter
1718
echo *Echo
1819
beforeFuncs []func()
1920
afterFuncs []func()
20-
Writer http.ResponseWriter
2121
Status int
2222
Size int64
2323
Committed bool

router.go

+16-17
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,31 @@ type Router struct {
1818
}
1919

2020
type node struct {
21-
kind kind
22-
label byte
23-
prefix string
24-
parent *node
25-
staticChildren children
26-
originalPath string
27-
methods *routeMethods
28-
paramChild *node
29-
anyChild *node
30-
paramsCount int
21+
methods *routeMethods
22+
parent *node
23+
paramChild *node
24+
anyChild *node
25+
// notFoundHandler is handler registered with RouteNotFound method and is executed for 404 cases
26+
notFoundHandler *routeMethod
27+
prefix string
28+
originalPath string
29+
staticChildren children
30+
paramsCount int
31+
label byte
32+
kind kind
3133
// isLeaf indicates that node does not have child routes
3234
isLeaf bool
3335
// isHandler indicates that node has at least one handler registered to it
3436
isHandler bool
35-
36-
// notFoundHandler is handler registered with RouteNotFound method and is executed for 404 cases
37-
notFoundHandler *routeMethod
3837
}
3938

4039
type kind uint8
4140
type children []*node
4241

4342
type routeMethod struct {
43+
handler HandlerFunc
4444
ppath string
4545
pnames []string
46-
handler HandlerFunc
4746
}
4847

4948
type routeMethods struct {
@@ -242,18 +241,18 @@ func (r *Router) insert(method, path string, h HandlerFunc) {
242241

243242
if i == lcpIndex {
244243
// path node is last fragment of route path. ie. `/users/:id`
245-
r.insertNode(method, path[:i], paramKind, routeMethod{ppath, pnames, h})
244+
r.insertNode(method, path[:i], paramKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
246245
} else {
247246
r.insertNode(method, path[:i], paramKind, routeMethod{})
248247
}
249248
} else if path[i] == '*' {
250249
r.insertNode(method, path[:i], staticKind, routeMethod{})
251250
pnames = append(pnames, "*")
252-
r.insertNode(method, path[:i+1], anyKind, routeMethod{ppath, pnames, h})
251+
r.insertNode(method, path[:i+1], anyKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
253252
}
254253
}
255254

256-
r.insertNode(method, path, staticKind, routeMethod{ppath, pnames, h})
255+
r.insertNode(method, path, staticKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
257256
}
258257

259258
func (r *Router) insertNode(method, path string, t kind, rm routeMethod) {

0 commit comments

Comments
 (0)