Skip to content

Commit

Permalink
optimization: struct alignment (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
behnambm authored May 30, 2024
1 parent 88c379f commit f7d9f51
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 37 deletions.
9 changes: 4 additions & 5 deletions binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ import (
type BindingError struct {
// Field is the field name where value binding failed
Field string `json:"field"`
*HTTPError
// Values of parameter that failed to bind.
Values []string `json:"-"`
*HTTPError
}

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

// ValueBinder provides utility methods for binding query or path parameter to various Go built-in types
type ValueBinder struct {
// failFast is flag for binding methods to return without attempting to bind when previous binding already failed
failFast bool
errors []error

// ValueFunc is used to get single parameter (first) value from request
ValueFunc func(sourceParam string) string
// ValuesFunc is used to get all values for parameter from request. i.e. `/api/search?ids=1&ids=2`
ValuesFunc func(sourceParam string) []string
// ErrorFunc is used to create errors. Allows you to use your own error type, that for example marshals to your specific json response
ErrorFunc func(sourceParam string, values []string, message interface{}, internalError error) error
errors []error
// failFast is flag for binding methods to return without attempting to bind when previous binding already failed
failFast bool
}

// QueryParamsBinder creates query parameter value binder
Expand Down
10 changes: 5 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,31 @@ type Context interface {
}

type context struct {
logger Logger
request *http.Request
response *Response
query url.Values
echo *Echo
logger Logger

store Map
lock sync.RWMutex

// following fields are set by Router
handler HandlerFunc

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

// pnames length is tied to param count for the matched route
pnames []string

// Usually echo.Echo is sizing pvalues but there could be user created middlewares that decide to
// overwrite parameter by calling SetParamNames + SetParamValues.
// When echo.Echo allocated that slice it length/capacity is tied to echo.Echo.maxParam value.
//
// It is important that pvalues size is always equal or bigger to pnames length.
pvalues []string
handler HandlerFunc

// pnames length is tied to param count for the matched route
pnames []string
}

const (
Expand Down
12 changes: 6 additions & 6 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ type Echo struct {
Listener net.Listener
TLSListener net.Listener
AutoTLSManager autocert.Manager
DisableHTTP2 bool
Debug bool
HideBanner bool
HidePort bool
HTTPErrorHandler HTTPErrorHandler
Binder Binder
JSONSerializer JSONSerializer
Expand All @@ -106,6 +102,10 @@ type Echo struct {

// OnAddRouteHandler is called when Echo adds new route to specific host router.
OnAddRouteHandler func(host string, route Route, handler HandlerFunc, middleware []MiddlewareFunc)
DisableHTTP2 bool
Debug bool
HideBanner bool
HidePort bool
}

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

// HTTPError represents an error that occurred while handling a request.
type HTTPError struct {
Code int `json:"-"`
Message interface{} `json:"message"`
Internal error `json:"-"` // Stores the error returned by an external dependency
Message interface{} `json:"message"`
Code int `json:"-"`
}

// MiddlewareFunc defines a function to process middleware.
Expand Down
2 changes: 1 addition & 1 deletion echo_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func StaticFileHandler(file string, filesystem fs.FS) HandlerFunc {
// traverse up from current executable run path.
// NB: private because you really should use fs.FS implementation instances
type defaultFS struct {
prefix string
fs fs.FS
prefix string
}

func newDefaultFS() *defaultFS {
Expand Down
2 changes: 1 addition & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Group struct {
common
host string
prefix string
middleware []MiddlewareFunc
echo *Echo
middleware []MiddlewareFunc
}

// Use implements `Echo#Use()` for sub-routes within the Group.
Expand Down
2 changes: 1 addition & 1 deletion ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ Private IPv6 address ranges:
*/

type ipChecker struct {
trustExtraRanges []*net.IPNet
trustLoopback bool
trustLinkLocal bool
trustPrivateNet bool
trustExtraRanges []*net.IPNet
}

// TrustOption is config for which IP address to trust
Expand Down
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
// by an HTTP handler to construct an HTTP response.
// See: https://golang.org/pkg/net/http/#ResponseWriter
type Response struct {
Writer http.ResponseWriter
echo *Echo
beforeFuncs []func()
afterFuncs []func()
Writer http.ResponseWriter
Status int
Size int64
Committed bool
Expand Down
33 changes: 16 additions & 17 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,31 @@ type Router struct {
}

type node struct {
kind kind
label byte
prefix string
parent *node
staticChildren children
originalPath string
methods *routeMethods
paramChild *node
anyChild *node
paramsCount int
methods *routeMethods
parent *node
paramChild *node
anyChild *node
// notFoundHandler is handler registered with RouteNotFound method and is executed for 404 cases
notFoundHandler *routeMethod
prefix string
originalPath string
staticChildren children
paramsCount int
label byte
kind kind
// isLeaf indicates that node does not have child routes
isLeaf bool
// isHandler indicates that node has at least one handler registered to it
isHandler bool

// notFoundHandler is handler registered with RouteNotFound method and is executed for 404 cases
notFoundHandler *routeMethod
}

type kind uint8
type children []*node

type routeMethod struct {
handler HandlerFunc
ppath string
pnames []string
handler HandlerFunc
}

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

if i == lcpIndex {
// path node is last fragment of route path. ie. `/users/:id`
r.insertNode(method, path[:i], paramKind, routeMethod{ppath, pnames, h})
r.insertNode(method, path[:i], paramKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
} else {
r.insertNode(method, path[:i], paramKind, routeMethod{})
}
} else if path[i] == '*' {
r.insertNode(method, path[:i], staticKind, routeMethod{})
pnames = append(pnames, "*")
r.insertNode(method, path[:i+1], anyKind, routeMethod{ppath, pnames, h})
r.insertNode(method, path[:i+1], anyKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
}
}

r.insertNode(method, path, staticKind, routeMethod{ppath, pnames, h})
r.insertNode(method, path, staticKind, routeMethod{ppath: ppath, pnames: pnames, handler: h})
}

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

0 comments on commit f7d9f51

Please sign in to comment.