Skip to content

Commit bed8648

Browse files
authored
optimize sructs with memory alignment / padding (go-chi#596)
1 parent fb48a47 commit bed8648

File tree

7 files changed

+37
-35
lines changed

7 files changed

+37
-35
lines changed

Diff for: middleware/compress.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func Compress(level int, types ...string) func(next http.Handler) http.Handler {
4545

4646
// Compressor represents a set of encoding configurations.
4747
type Compressor struct {
48-
level int // The compression level.
4948
// The mapping of encoder names to encoder functions.
5049
encoders map[string]EncoderFunc
5150
// The mapping of pooled encoders to pools.
@@ -55,6 +54,7 @@ type Compressor struct {
5554
allowedWildcards map[string]struct{}
5655
// The list of encoders in order of decreasing precedence.
5756
encodingPrecedence []string
57+
level int // The compression level.
5858
}
5959

6060
// NewCompressor creates a new Compressor that will handle encoding responses.
@@ -271,9 +271,9 @@ type compressResponseWriter struct {
271271
// The streaming encoder writer to be used if there is one. Otherwise,
272272
// this is just the normal writer.
273273
w io.Writer
274-
encoding string
275274
contentTypes map[string]struct{}
276275
contentWildcards map[string]struct{}
276+
encoding string
277277
wroteHeader bool
278278
compressable bool
279279
}

Diff for: middleware/compress_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func TestCompressor(t *testing.T) {
5353
tests := []struct {
5454
name string
5555
path string
56-
acceptedEncodings []string
5756
expectedEncoding string
57+
acceptedEncodings []string
5858
}{
5959
{
6060
name: "no expected encodings due to no accepted encodings",
@@ -114,10 +114,10 @@ func TestCompressor(t *testing.T) {
114114
func TestCompressorWildcards(t *testing.T) {
115115
tests := []struct {
116116
name string
117+
recover string
117118
types []string
118119
typesCount int
119120
wcCount int
120-
recover string
121121
}{
122122
{
123123
name: "defaults",

Diff for: middleware/route_headers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ func (hr HeaderRouter) Handler(next http.Handler) http.Handler {
112112
}
113113

114114
type HeaderRoute struct {
115-
MatchAny []Pattern
116-
MatchOne Pattern
117115
Middleware func(next http.Handler) http.Handler
116+
MatchOne Pattern
117+
MatchAny []Pattern
118118
}
119119

120120
func (r HeaderRoute) IsMatch(value string) bool {

Diff for: mux.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,30 @@ var _ Router = &Mux{}
1919
// particularly useful for writing large REST API services that break a handler
2020
// into many smaller parts composed of middlewares and end handlers.
2121
type Mux struct {
22+
// The computed mux handler made of the chained middleware stack and
23+
// the tree router
24+
handler http.Handler
25+
2226
// The radix trie router
2327
tree *node
2428

25-
// The middleware stack
26-
middlewares []func(http.Handler) http.Handler
29+
// Custom method not allowed handler
30+
methodNotAllowedHandler http.HandlerFunc
2731

2832
// Controls the behaviour of middleware chain generation when a mux
2933
// is registered as an inline group inside another mux.
30-
inline bool
3134
parent *Mux
3235

33-
// The computed mux handler made of the chained middleware stack and
34-
// the tree router
35-
handler http.Handler
36-
3736
// Routing context pool
3837
pool *sync.Pool
3938

4039
// Custom route not found handler
4140
notFoundHandler http.HandlerFunc
4241

43-
// Custom method not allowed handler
44-
methodNotAllowedHandler http.HandlerFunc
42+
// The middleware stack
43+
middlewares []func(http.Handler) http.Handler
44+
45+
inline bool
4546
}
4647

4748
// NewMux returns a newly initialized Mux object that implements the Router

Diff for: mux_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,11 @@ func TestServeHTTPExistingContext(t *testing.T) {
12191219
})
12201220

12211221
testcases := []struct {
1222+
Ctx context.Context
12221223
Method string
12231224
Path string
1224-
Ctx context.Context
1225-
ExpectedStatus int
12261225
ExpectedBody string
1226+
ExpectedStatus int
12271227
}{
12281228
{
12291229
Method: "GET",

Diff for: tree.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,31 @@ const (
7272
)
7373

7474
type node struct {
75-
// node type: static, regexp, param, catchAll
76-
typ nodeTyp
7775

78-
// first byte of the prefix
79-
label byte
80-
81-
// first byte of the child prefix
82-
tail byte
83-
84-
// prefix is the common prefix we ignore
85-
prefix string
76+
// subroutes on the leaf node
77+
subroutes Routes
8678

8779
// regexp matcher for regexp nodes
8880
rex *regexp.Regexp
8981

9082
// HTTP handler endpoints on the leaf node
9183
endpoints endpoints
9284

93-
// subroutes on the leaf node
94-
subroutes Routes
85+
// prefix is the common prefix we ignore
86+
prefix string
9587

9688
// child nodes should be stored in-order for iteration,
9789
// in groups of the node type.
9890
children [ntCatchAll + 1]nodes
91+
92+
// first byte of the child prefix
93+
tail byte
94+
95+
// node type: static, regexp, param, catchAll
96+
typ nodeTyp
97+
98+
// first byte of the prefix
99+
label byte
99100
}
100101

101102
// endpoints is a mapping of http method constants to handlers

Diff for: tree_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ func TestTreeMoar(t *testing.T) {
202202
tr.InsertRoute(mGET, "/users/{id}/settings/*", hStub16)
203203

204204
tests := []struct {
205-
m methodTyp // input request http method
206-
r string // input request path
207-
h http.Handler // output matched handler
208-
k []string // output param keys
209-
v []string // output param values
205+
h http.Handler
206+
r string
207+
k []string
208+
v []string
209+
m methodTyp
210210
}{
211211
{m: mGET, r: "/articles/search", h: hStub1, k: []string{}, v: []string{}},
212212
{m: mGET, r: "/articlefun", h: hStub5, k: []string{}, v: []string{}},
@@ -394,8 +394,8 @@ func TestTreeRegexMatchWholeParam(t *testing.T) {
394394
tr.InsertRoute(mGET, "/{param:[0-9]*}/test", hStub1)
395395

396396
tests := []struct {
397-
url string
398397
expectedHandler http.Handler
398+
url string
399399
}{
400400
{url: "/13", expectedHandler: hStub1},
401401
{url: "/a13", expectedHandler: nil},

0 commit comments

Comments
 (0)