Skip to content

Commit 86c7e84

Browse files
authored
Optimize struct field order to reduce memory usage (#1809)
1. Reduce RequestHeader from 368 bytes to 360 bytes 2. Reduce Request from 816 bytes to 800 bytes 3. Reduce Response from 432 bytes to 416 bytes 4. Reduce Client from 312 bytes to 288 bytes 5. Reduce HostClient from 416 bytes to 392 bytes 6. Reduce PipelineClient from 176 bytes to 168 bytes 7. Reduce pipelineConnClient from 216 bytes to 208 bytes 8. Reduce Cookie from 232 bytes to 224 bytes 9. Reduce FS from 184 bytes to 160 bytes 10. Reduce fsHandler from 168 bytes to 160 bytes 11. Reduce ResponseHeader from 328 bytes to 320 bytes 12. Reduce headerScanner from 128 bytes to 120 bytes 13. Reduce TCPDialer from 104 bytes to 96 bytes 14. Reduce workerPool from 152 btyes to 144 btyes
1 parent 7760a5b commit 86c7e84

File tree

7 files changed

+139
-139
lines changed

7 files changed

+139
-139
lines changed

client.go

Lines changed: 79 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@ type Client struct {
180180
// Default client name is used if not set.
181181
Name string
182182

183-
// NoDefaultUserAgentHeader when set to true, causes the default
184-
// User-Agent header to be excluded from the Request.
185-
NoDefaultUserAgentHeader bool
186-
187183
// Callback for establishing new connections to hosts.
188184
//
189185
// Default DialTimeout is used if not set.
@@ -197,15 +193,6 @@ type Client struct {
197193
// If not set, DialTimeout is used.
198194
Dial DialFunc
199195

200-
// Attempt to connect to both ipv4 and ipv6 addresses if set to true.
201-
//
202-
// This option is used only if default TCP dialer is used,
203-
// i.e. if Dial is blank.
204-
//
205-
// By default client connects only to ipv4 addresses,
206-
// since unfortunately ipv6 remains broken in many networks worldwide :)
207-
DialDualStack bool
208-
209196
// TLS config for https connections.
210197
//
211198
// Default TLS config is used if not set.
@@ -261,6 +248,19 @@ type Client struct {
261248
// By default response body size is unlimited.
262249
MaxResponseBodySize int
263250

251+
// NoDefaultUserAgentHeader when set to true, causes the default
252+
// User-Agent header to be excluded from the Request.
253+
NoDefaultUserAgentHeader bool
254+
255+
// Attempt to connect to both ipv4 and ipv6 addresses if set to true.
256+
//
257+
// This option is used only if default TCP dialer is used,
258+
// i.e. if Dial is blank.
259+
//
260+
// By default client connects only to ipv4 addresses,
261+
// since unfortunately ipv6 remains broken in many networks worldwide :)
262+
DialDualStack bool
263+
264264
// Header names are passed as-is without normalization
265265
// if this option is set.
266266
//
@@ -288,6 +288,9 @@ type Client struct {
288288
// extra slashes are removed, special characters are encoded.
289289
DisablePathNormalizing bool
290290

291+
// StreamResponseBody enables response body streaming.
292+
StreamResponseBody bool
293+
291294
// Maximum duration for waiting for a free connection.
292295
//
293296
// By default will not waiting, return ErrNoFreeConns immediately.
@@ -301,9 +304,6 @@ type Client struct {
301304
// Connection pool strategy. Can be either LIFO or FIFO (default).
302305
ConnPoolStrategy ConnPoolStrategyType
303306

304-
// StreamResponseBody enables response body streaming.
305-
StreamResponseBody bool
306-
307307
// ConfigureClient configures the fasthttp.HostClient.
308308
ConfigureClient func(hc *HostClient) error
309309

@@ -698,10 +698,6 @@ type HostClient struct {
698698
// Client name. Used in User-Agent request header.
699699
Name string
700700

701-
// NoDefaultUserAgentHeader when set to true, causes the default
702-
// User-Agent header to be excluded from the Request.
703-
NoDefaultUserAgentHeader bool
704-
705701
// Callback for establishing new connections to hosts.
706702
//
707703
// Default DialTimeout is used if not set.
@@ -715,19 +711,6 @@ type HostClient struct {
715711
// If not set, DialTimeout is used.
716712
Dial DialFunc
717713

718-
// Attempt to connect to both ipv4 and ipv6 host addresses
719-
// if set to true.
720-
//
721-
// This option is used only if default TCP dialer is used,
722-
// i.e. if Dial and DialTimeout are blank.
723-
//
724-
// By default client connects only to ipv4 addresses,
725-
// since unfortunately ipv6 remains broken in many networks worldwide :)
726-
DialDualStack bool
727-
728-
// Whether to use TLS (aka SSL or HTTPS) for host connections.
729-
IsTLS bool
730-
731714
// Optional TLS config.
732715
TLSConfig *tls.Config
733716

@@ -785,6 +768,64 @@ type HostClient struct {
785768
// By default response body size is unlimited.
786769
MaxResponseBodySize int
787770

771+
// Maximum duration for waiting for a free connection.
772+
//
773+
// By default will not waiting, return ErrNoFreeConns immediately
774+
MaxConnWaitTimeout time.Duration
775+
776+
// RetryIf controls whether a retry should be attempted after an error.
777+
//
778+
// By default will use isIdempotent function
779+
RetryIf RetryIfFunc
780+
781+
// Transport defines a transport-like mechanism that wraps every request/response.
782+
Transport RoundTripper
783+
784+
// Connection pool strategy. Can be either LIFO or FIFO (default).
785+
ConnPoolStrategy ConnPoolStrategyType
786+
787+
connsLock sync.Mutex
788+
connsCount int
789+
conns []*clientConn
790+
connsWait *wantConnQueue
791+
792+
addrsLock sync.Mutex
793+
addrs []string
794+
addrIdx uint32
795+
lastUseTime uint32
796+
797+
tlsConfigMap map[string]*tls.Config
798+
tlsConfigMapLock sync.Mutex
799+
800+
readerPool sync.Pool
801+
writerPool sync.Pool
802+
803+
clientReaderPool *sync.Pool
804+
clientWriterPool *sync.Pool
805+
806+
pendingRequests int32
807+
808+
// pendingClientRequests counts the number of requests that a Client is currently running using this HostClient.
809+
// It will be incremented earlier than pendingRequests and will be used by Client to see if the HostClient is still in use.
810+
pendingClientRequests int32
811+
812+
// NoDefaultUserAgentHeader when set to true, causes the default
813+
// User-Agent header to be excluded from the Request.
814+
NoDefaultUserAgentHeader bool
815+
816+
// Attempt to connect to both ipv4 and ipv6 host addresses
817+
// if set to true.
818+
//
819+
// This option is used only if default TCP dialer is used,
820+
// i.e. if Dial and DialTimeout are blank.
821+
//
822+
// By default client connects only to ipv4 addresses,
823+
// since unfortunately ipv6 remains broken in many networks worldwide :)
824+
DialDualStack bool
825+
826+
// Whether to use TLS (aka SSL or HTTPS) for host connections.
827+
IsTLS bool
828+
788829
// Header names are passed as-is without normalization
789830
// if this option is set.
790831
//
@@ -820,51 +861,9 @@ type HostClient struct {
820861
// Client logs full errors by default.
821862
SecureErrorLogMessage bool
822863

823-
// Maximum duration for waiting for a free connection.
824-
//
825-
// By default will not waiting, return ErrNoFreeConns immediately
826-
MaxConnWaitTimeout time.Duration
827-
828-
// RetryIf controls whether a retry should be attempted after an error.
829-
//
830-
// By default will use isIdempotent function
831-
RetryIf RetryIfFunc
832-
833-
// Transport defines a transport-like mechanism that wraps every request/response.
834-
Transport RoundTripper
835-
836-
// Connection pool strategy. Can be either LIFO or FIFO (default).
837-
ConnPoolStrategy ConnPoolStrategyType
838-
839864
// StreamResponseBody enables response body streaming.
840865
StreamResponseBody bool
841866

842-
lastUseTime uint32
843-
844-
connsLock sync.Mutex
845-
connsCount int
846-
conns []*clientConn
847-
connsWait *wantConnQueue
848-
849-
addrsLock sync.Mutex
850-
addrs []string
851-
addrIdx uint32
852-
853-
tlsConfigMap map[string]*tls.Config
854-
tlsConfigMapLock sync.Mutex
855-
856-
readerPool sync.Pool
857-
writerPool sync.Pool
858-
859-
clientReaderPool *sync.Pool
860-
clientWriterPool *sync.Pool
861-
862-
pendingRequests int32
863-
864-
// pendingClientRequests counts the number of requests that a Client is currently running using this HostClient.
865-
// It will be incremented earlier than pendingRequests and will be used by Client to see if the HostClient is still in use.
866-
pendingClientRequests int32
867-
868867
connsCleanerRun bool
869868
}
870869

@@ -2174,10 +2173,6 @@ type PipelineClient struct {
21742173
// PipelineClient name. Used in User-Agent request header.
21752174
Name string
21762175

2177-
// NoDefaultUserAgentHeader when set to true, causes the default
2178-
// User-Agent header to be excluded from the Request.
2179-
NoDefaultUserAgentHeader bool
2180-
21812176
// The maximum number of concurrent connections to the Addr.
21822177
//
21832178
// A single connection is used by default.
@@ -2200,6 +2195,10 @@ type PipelineClient struct {
22002195
// Default Dial is used if not set.
22012196
Dial DialFunc
22022197

2198+
// NoDefaultUserAgentHeader when set to true, causes the default
2199+
// User-Agent header to be excluded from the Request.
2200+
NoDefaultUserAgentHeader bool
2201+
22032202
// Attempt to connect to both ipv4 and ipv6 host addresses
22042203
// if set to true.
22052204
//
@@ -2284,10 +2283,10 @@ type pipelineConnClient struct {
22842283

22852284
Addr string
22862285
Name string
2287-
NoDefaultUserAgentHeader bool
22882286
MaxPendingRequests int
22892287
MaxBatchDelay time.Duration
22902288
Dial DialFunc
2289+
NoDefaultUserAgentHeader bool
22912290
DialDualStack bool
22922291
DisableHeaderNamesNormalizing bool
22932292
DisablePathNormalizing bool

cookie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ type Cookie struct {
7474
domain []byte
7575
path []byte
7676

77+
sameSite CookieSameSite
7778
httpOnly bool
7879
secure bool
79-
sameSite CookieSameSite
8080
partitioned bool
8181

8282
bufKV argsKV

fs.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,6 @@ type FS struct {
259259
// Path to the root directory to serve files from.
260260
Root string
261261

262-
// AllowEmptyRoot controls what happens when Root is empty. When false (default) it will default to the
263-
// current working directory. An empty root is mostly useful when you want to use absolute paths
264-
// on windows that are on different filesystems. On linux setting your Root to "/" already allows you to use
265-
// absolute paths on any filesystem.
266-
AllowEmptyRoot bool
267-
268262
// List of index file names to try opening during directory access.
269263
//
270264
// For example:
@@ -276,6 +270,36 @@ type FS struct {
276270
// By default the list is empty.
277271
IndexNames []string
278272

273+
// Path to the compressed root directory to serve files from. If this value
274+
// is empty, Root is used.
275+
CompressRoot string
276+
277+
// Path rewriting function.
278+
//
279+
// By default request path is not modified.
280+
PathRewrite PathRewriteFunc
281+
282+
// PathNotFound fires when file is not found in filesystem
283+
// this functions tries to replace "Cannot open requested path"
284+
// server response giving to the programmer the control of server flow.
285+
//
286+
// By default PathNotFound returns
287+
// "Cannot open requested path"
288+
PathNotFound RequestHandler
289+
290+
// AllowEmptyRoot controls what happens when Root is empty. When false (default) it will default to the
291+
// current working directory. An empty root is mostly useful when you want to use absolute paths
292+
// on windows that are on different filesystems. On linux setting your Root to "/" already allows you to use
293+
// absolute paths on any filesystem.
294+
AllowEmptyRoot bool
295+
296+
// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
297+
//
298+
// This value has sense only if Compress is set.
299+
//
300+
// Brotli encoding is disabled by default.
301+
CompressBrotli bool
302+
279303
// Index pages for directories without files matching IndexNames
280304
// are automatically generated if set.
281305
//
@@ -298,35 +322,11 @@ type FS struct {
298322
// Transparent compression is disabled by default.
299323
Compress bool
300324

301-
// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
302-
//
303-
// This value has sense only if Compress is set.
304-
//
305-
// Brotli encoding is disabled by default.
306-
CompressBrotli bool
307-
308-
// Path to the compressed root directory to serve files from. If this value
309-
// is empty, Root is used.
310-
CompressRoot string
311-
312325
// Enables byte range requests if set to true.
313326
//
314327
// Byte range requests are disabled by default.
315328
AcceptByteRange bool
316329

317-
// Path rewriting function.
318-
//
319-
// By default request path is not modified.
320-
PathRewrite PathRewriteFunc
321-
322-
// PathNotFound fires when file is not found in filesystem
323-
// this functions tries to replace "Cannot open requested path"
324-
// server response giving to the programmer the control of server flow.
325-
//
326-
// By default PathNotFound returns
327-
// "Cannot open requested path"
328-
PathNotFound RequestHandler
329-
330330
// SkipCache if true, will cache no file handler.
331331
//
332332
// By default is false.
@@ -510,8 +510,8 @@ type fsHandler struct {
510510
generateIndexPages bool
511511
compress bool
512512
compressBrotli bool
513-
compressRoot string
514513
acceptByteRange bool
514+
compressRoot string
515515
compressedFileSuffixes map[string]string
516516

517517
cacheManager cacheManager

0 commit comments

Comments
 (0)