Skip to content

Commit

Permalink
Optimize struct field order to reduce memory usage (valyala#1809)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ksw2000 authored Jul 20, 2024
1 parent 7760a5b commit 86c7e84
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 139 deletions.
159 changes: 79 additions & 80 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ type Client struct {
// Default client name is used if not set.
Name string

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// Callback for establishing new connections to hosts.
//
// Default DialTimeout is used if not set.
Expand All @@ -197,15 +193,6 @@ type Client struct {
// If not set, DialTimeout is used.
Dial DialFunc

// Attempt to connect to both ipv4 and ipv6 addresses if set to true.
//
// This option is used only if default TCP dialer is used,
// i.e. if Dial is blank.
//
// By default client connects only to ipv4 addresses,
// since unfortunately ipv6 remains broken in many networks worldwide :)
DialDualStack bool

// TLS config for https connections.
//
// Default TLS config is used if not set.
Expand Down Expand Up @@ -261,6 +248,19 @@ type Client struct {
// By default response body size is unlimited.
MaxResponseBodySize int

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// Attempt to connect to both ipv4 and ipv6 addresses if set to true.
//
// This option is used only if default TCP dialer is used,
// i.e. if Dial is blank.
//
// By default client connects only to ipv4 addresses,
// since unfortunately ipv6 remains broken in many networks worldwide :)
DialDualStack bool

// Header names are passed as-is without normalization
// if this option is set.
//
Expand Down Expand Up @@ -288,6 +288,9 @@ type Client struct {
// extra slashes are removed, special characters are encoded.
DisablePathNormalizing bool

// StreamResponseBody enables response body streaming.
StreamResponseBody bool

// Maximum duration for waiting for a free connection.
//
// By default will not waiting, return ErrNoFreeConns immediately.
Expand All @@ -301,9 +304,6 @@ type Client struct {
// Connection pool strategy. Can be either LIFO or FIFO (default).
ConnPoolStrategy ConnPoolStrategyType

// StreamResponseBody enables response body streaming.
StreamResponseBody bool

// ConfigureClient configures the fasthttp.HostClient.
ConfigureClient func(hc *HostClient) error

Expand Down Expand Up @@ -698,10 +698,6 @@ type HostClient struct {
// Client name. Used in User-Agent request header.
Name string

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// Callback for establishing new connections to hosts.
//
// Default DialTimeout is used if not set.
Expand All @@ -715,19 +711,6 @@ type HostClient struct {
// If not set, DialTimeout is used.
Dial DialFunc

// Attempt to connect to both ipv4 and ipv6 host addresses
// if set to true.
//
// This option is used only if default TCP dialer is used,
// i.e. if Dial and DialTimeout are blank.
//
// By default client connects only to ipv4 addresses,
// since unfortunately ipv6 remains broken in many networks worldwide :)
DialDualStack bool

// Whether to use TLS (aka SSL or HTTPS) for host connections.
IsTLS bool

// Optional TLS config.
TLSConfig *tls.Config

Expand Down Expand Up @@ -785,6 +768,64 @@ type HostClient struct {
// By default response body size is unlimited.
MaxResponseBodySize int

// Maximum duration for waiting for a free connection.
//
// By default will not waiting, return ErrNoFreeConns immediately
MaxConnWaitTimeout time.Duration

// RetryIf controls whether a retry should be attempted after an error.
//
// By default will use isIdempotent function
RetryIf RetryIfFunc

// Transport defines a transport-like mechanism that wraps every request/response.
Transport RoundTripper

// Connection pool strategy. Can be either LIFO or FIFO (default).
ConnPoolStrategy ConnPoolStrategyType

connsLock sync.Mutex
connsCount int
conns []*clientConn
connsWait *wantConnQueue

addrsLock sync.Mutex
addrs []string
addrIdx uint32
lastUseTime uint32

tlsConfigMap map[string]*tls.Config
tlsConfigMapLock sync.Mutex

readerPool sync.Pool
writerPool sync.Pool

clientReaderPool *sync.Pool
clientWriterPool *sync.Pool

pendingRequests int32

// pendingClientRequests counts the number of requests that a Client is currently running using this HostClient.
// It will be incremented earlier than pendingRequests and will be used by Client to see if the HostClient is still in use.
pendingClientRequests int32

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// Attempt to connect to both ipv4 and ipv6 host addresses
// if set to true.
//
// This option is used only if default TCP dialer is used,
// i.e. if Dial and DialTimeout are blank.
//
// By default client connects only to ipv4 addresses,
// since unfortunately ipv6 remains broken in many networks worldwide :)
DialDualStack bool

// Whether to use TLS (aka SSL or HTTPS) for host connections.
IsTLS bool

// Header names are passed as-is without normalization
// if this option is set.
//
Expand Down Expand Up @@ -820,51 +861,9 @@ type HostClient struct {
// Client logs full errors by default.
SecureErrorLogMessage bool

// Maximum duration for waiting for a free connection.
//
// By default will not waiting, return ErrNoFreeConns immediately
MaxConnWaitTimeout time.Duration

// RetryIf controls whether a retry should be attempted after an error.
//
// By default will use isIdempotent function
RetryIf RetryIfFunc

// Transport defines a transport-like mechanism that wraps every request/response.
Transport RoundTripper

// Connection pool strategy. Can be either LIFO or FIFO (default).
ConnPoolStrategy ConnPoolStrategyType

// StreamResponseBody enables response body streaming.
StreamResponseBody bool

lastUseTime uint32

connsLock sync.Mutex
connsCount int
conns []*clientConn
connsWait *wantConnQueue

addrsLock sync.Mutex
addrs []string
addrIdx uint32

tlsConfigMap map[string]*tls.Config
tlsConfigMapLock sync.Mutex

readerPool sync.Pool
writerPool sync.Pool

clientReaderPool *sync.Pool
clientWriterPool *sync.Pool

pendingRequests int32

// pendingClientRequests counts the number of requests that a Client is currently running using this HostClient.
// It will be incremented earlier than pendingRequests and will be used by Client to see if the HostClient is still in use.
pendingClientRequests int32

connsCleanerRun bool
}

Expand Down Expand Up @@ -2174,10 +2173,6 @@ type PipelineClient struct {
// PipelineClient name. Used in User-Agent request header.
Name string

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// The maximum number of concurrent connections to the Addr.
//
// A single connection is used by default.
Expand All @@ -2200,6 +2195,10 @@ type PipelineClient struct {
// Default Dial is used if not set.
Dial DialFunc

// NoDefaultUserAgentHeader when set to true, causes the default
// User-Agent header to be excluded from the Request.
NoDefaultUserAgentHeader bool

// Attempt to connect to both ipv4 and ipv6 host addresses
// if set to true.
//
Expand Down Expand Up @@ -2284,10 +2283,10 @@ type pipelineConnClient struct {

Addr string
Name string
NoDefaultUserAgentHeader bool
MaxPendingRequests int
MaxBatchDelay time.Duration
Dial DialFunc
NoDefaultUserAgentHeader bool
DialDualStack bool
DisableHeaderNamesNormalizing bool
DisablePathNormalizing bool
Expand Down
2 changes: 1 addition & 1 deletion cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type Cookie struct {
domain []byte
path []byte

sameSite CookieSameSite
httpOnly bool
secure bool
sameSite CookieSameSite
partitioned bool

bufKV argsKV
Expand Down
62 changes: 31 additions & 31 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ type FS struct {
// Path to the root directory to serve files from.
Root string

// AllowEmptyRoot controls what happens when Root is empty. When false (default) it will default to the
// current working directory. An empty root is mostly useful when you want to use absolute paths
// on windows that are on different filesystems. On linux setting your Root to "/" already allows you to use
// absolute paths on any filesystem.
AllowEmptyRoot bool

// List of index file names to try opening during directory access.
//
// For example:
Expand All @@ -276,6 +270,36 @@ type FS struct {
// By default the list is empty.
IndexNames []string

// Path to the compressed root directory to serve files from. If this value
// is empty, Root is used.
CompressRoot string

// Path rewriting function.
//
// By default request path is not modified.
PathRewrite PathRewriteFunc

// PathNotFound fires when file is not found in filesystem
// this functions tries to replace "Cannot open requested path"
// server response giving to the programmer the control of server flow.
//
// By default PathNotFound returns
// "Cannot open requested path"
PathNotFound RequestHandler

// AllowEmptyRoot controls what happens when Root is empty. When false (default) it will default to the
// current working directory. An empty root is mostly useful when you want to use absolute paths
// on windows that are on different filesystems. On linux setting your Root to "/" already allows you to use
// absolute paths on any filesystem.
AllowEmptyRoot bool

// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
//
// This value has sense only if Compress is set.
//
// Brotli encoding is disabled by default.
CompressBrotli bool

// Index pages for directories without files matching IndexNames
// are automatically generated if set.
//
Expand All @@ -298,35 +322,11 @@ type FS struct {
// Transparent compression is disabled by default.
Compress bool

// Uses brotli encoding and fallbacks to gzip in responses if set to true, uses gzip if set to false.
//
// This value has sense only if Compress is set.
//
// Brotli encoding is disabled by default.
CompressBrotli bool

// Path to the compressed root directory to serve files from. If this value
// is empty, Root is used.
CompressRoot string

// Enables byte range requests if set to true.
//
// Byte range requests are disabled by default.
AcceptByteRange bool

// Path rewriting function.
//
// By default request path is not modified.
PathRewrite PathRewriteFunc

// PathNotFound fires when file is not found in filesystem
// this functions tries to replace "Cannot open requested path"
// server response giving to the programmer the control of server flow.
//
// By default PathNotFound returns
// "Cannot open requested path"
PathNotFound RequestHandler

// SkipCache if true, will cache no file handler.
//
// By default is false.
Expand Down Expand Up @@ -510,8 +510,8 @@ type fsHandler struct {
generateIndexPages bool
compress bool
compressBrotli bool
compressRoot string
acceptByteRange bool
compressRoot string
compressedFileSuffixes map[string]string

cacheManager cacheManager
Expand Down
Loading

0 comments on commit 86c7e84

Please sign in to comment.