Skip to content

Commit 3db5c51

Browse files
committed
perf: better field alignment (#733)
Signed-off-by: Rueian <[email protected]>
1 parent 5c086ea commit 3db5c51

17 files changed

+202
-192
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ client.DoMultiCache(ctx,
133133
valkey.CT(client.B().Get().Key("k2").Cache(), 2*time.Minute))
134134
```
135135

136-
Cached responses, including Redis Nils, will be invalidated either when being notified by valkey servers or when their client-side TTLs are reached. See https://github.com/redis/rueidis/issues/534 for more details.
136+
Cached responses, including Valkey Nils, will be invalidated either when being notified by valkey servers or when their client-side TTLs are reached. See https://github.com/redis/rueidis/issues/534 for more details.
137137

138138
### Benchmark
139139

client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111

1212
type singleClient struct {
1313
conn conn
14-
cmd Builder
1514
retryHandler retryHandler
1615
stop uint32
16+
cmd Builder
1717
retry bool
1818
DisableCache bool
1919
}
@@ -197,9 +197,9 @@ func (c *singleClient) Close() {
197197
type dedicatedSingleClient struct {
198198
conn conn
199199
wire wire
200-
cmd Builder
201200
retryHandler retryHandler
202201
mark uint32
202+
cmd Builder
203203
retry bool
204204
}
205205

cluster.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ var ErrSendToReplicasNotSet = errors.New("SendToReplicas must be set when Replic
2323

2424
type clusterClient struct {
2525
pslots [16384]conn
26-
rslots []conn
27-
sc call
26+
retryHandler retryHandler
27+
opt *ClientOption
2828
rOpt *ClientOption
2929
conns map[string]connrole
3030
connFn connFn
31-
opt *ClientOption
32-
retryHandler retryHandler
3331
stopCh chan struct{}
34-
cmd Builder
32+
sc call
33+
rslots []conn
3534
mu sync.RWMutex
3635
stop uint32
36+
cmd Builder
3737
retry bool
3838
}
3939

@@ -1236,15 +1236,15 @@ func (c *clusterClient) shouldRefreshRetry(err error, ctx context.Context) (addr
12361236
}
12371237

12381238
type dedicatedClusterClient struct {
1239-
client *clusterClient
12401239
conn conn
12411240
wire wire
1241+
retryHandler retryHandler
1242+
client *clusterClient
12421243
pshks *pshks
12431244
mu sync.Mutex
12441245
cmd Builder
1245-
retryHandler retryHandler
1246-
retry bool
12471246
slot uint16
1247+
retry bool
12481248
mark bool
12491249
}
12501250

message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func (m *ValkeyMessage) CacheUnmarshalView(buf []byte) error {
629629
return nil
630630
}
631631

632-
// IsNil check if message is a redis nil response
632+
// IsNil check if message is a valkey nil response
633633
func (m *ValkeyMessage) IsNil() bool {
634634
return m.typ == typeNull
635635
}

pubsub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ func newSubs() *subs {
4242
}
4343

4444
type subs struct {
45-
cnt uint64
4645
chs map[string]chs
4746
sub map[uint64]*sub
47+
cnt uint64
4848
mu sync.RWMutex
4949
}
5050

sentinel.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ func newSentinelClient(opt *ClientOption, connFn connFn, retryer retryHandler) (
4242

4343
type sentinelClient struct {
4444
mConn atomic.Value
45-
mAddr string
46-
sAddr string
45+
sConn conn
46+
retryHandler retryHandler
4747
connFn connFn
4848
mOpt *ClientOption
4949
sOpt *ClientOption
50-
sConn conn
5150
sentinels *list.List
51+
mAddr string
52+
sAddr string
5253
sc call
53-
retryHandler retryHandler
54-
cmd Builder
5554
mu sync.Mutex
5655
stop uint32
56+
cmd Builder
5757
retry bool
5858
replica bool
5959
}

valkey.go

+34-32
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ var (
6363

6464
// ClientOption should be passed to NewClient to construct a Client
6565
type ClientOption struct {
66-
// TCP & TLS
67-
// Dialer can be used to customized how valkey connect to a valkey instance via TCP, including:
68-
// - Timeout, the default is DefaultDialTimeout
69-
// - KeepAlive, the default is DefaultTCPKeepAlive
70-
// The Dialer.KeepAlive interval is used to detect an unresponsive idle tcp connection.
71-
// OS takes at least (tcp_keepalive_probes+1)*Dialer.KeepAlive time to conclude an idle connection to be unresponsive.
72-
// For example: DefaultTCPKeepAlive = 1s and the default of tcp_keepalive_probes on Linux is 9.
73-
// Therefore, it takes at least 10s to kill an idle and unresponsive tcp connection on Linux by default.
74-
Dialer net.Dialer
7566
TLSConfig *tls.Config
7667

7768
// DialFn allows for a custom function to be used to create net.Conn connections
@@ -89,18 +80,45 @@ type ClientOption struct {
8980
// NOTE: This function can't be used with ReplicaOnly option.
9081
SendToReplicas func(cmd Completed) bool
9182

83+
// AuthCredentialsFn allows for setting the AUTH username and password dynamically on each connection attempt to
84+
// support rotating credentials
85+
AuthCredentialsFn func(AuthCredentialsContext) (AuthCredentials, error)
86+
87+
// RetryDelay is the function that returns the delay that should be used before retrying the attempt.
88+
// The default is an exponential backoff with a maximum delay of 1 second.
89+
// Only used when DisableRetry is false.
90+
RetryDelay RetryDelayFn
91+
92+
// ReplicaSelector selects a replica node when `SendToReplicas` returns true.
93+
// If the function is set, the client will send selected command to the replica node.
94+
// Returned value is the index of the replica node in the replicas slice.
95+
// If the returned value is out of range, the primary node will be selected.
96+
// If primary node does not have any replica, the primary node will be selected
97+
// and function will not be called.
98+
// Currently only used for cluster client.
99+
// Each ReplicaInfo must not be modified.
100+
// NOTE: This function can't be used with ReplicaOnly option.
101+
// NOTE: This function must be used with SendToReplicas function.
102+
ReplicaSelector func(slot uint16, replicas []ReplicaInfo) int
103+
92104
// Sentinel options, including MasterSet and Auth options
93105
Sentinel SentinelOption
94106

107+
// TCP & TLS
108+
// Dialer can be used to customized how valkey-go connect to a valkey instance via TCP, including:
109+
// - Timeout, the default is DefaultDialTimeout
110+
// - KeepAlive, the default is DefaultTCPKeepAlive
111+
// The Dialer.KeepAlive interval is used to detect an unresponsive idle tcp connection.
112+
// OS takes at least (tcp_keepalive_probes+1)*Dialer.KeepAlive time to conclude an idle connection to be unresponsive.
113+
// For example: DefaultTCPKeepAlive = 1s and the default of tcp_keepalive_probes on Linux is 9.
114+
// Therefore, it takes at least 10s to kill an idle and unresponsive tcp connection on Linux by default.
115+
Dialer net.Dialer
116+
95117
// Valkey AUTH parameters
96118
Username string
97119
Password string
98120
ClientName string
99121

100-
// AuthCredentialsFn allows for setting the AUTH username and password dynamically on each connection attempt to
101-
// support rotating credentials
102-
AuthCredentialsFn func(AuthCredentialsContext) (AuthCredentials, error)
103-
104122
// ClientSetInfo will assign various info attributes to the current connection.
105123
// Note that ClientSetInfo should have exactly 2 values, the lib name and the lib version respectively.
106124
ClientSetInfo []string
@@ -170,6 +188,9 @@ type ClientOption struct {
170188
// produce notable CPU usage reduction under load. Ref: https://github.com/redis/rueidis/issues/156
171189
MaxFlushDelay time.Duration
172190

191+
// ClusterOption is the options for the valkey cluster client.
192+
ClusterOption ClusterOption
193+
173194
// DisableTCPNoDelay turns on Nagle's algorithm in pipelining mode by using conn.SetNoDelay(false).
174195
// Turning this on can result in lower p99 latencies and lower CPU usages if all your requests are small.
175196
// But if you have large requests or fast network, this might degrade the performance. Ref: https://github.com/redis/rueidis/pull/650
@@ -181,10 +202,6 @@ type ClientOption struct {
181202
ClientNoTouch bool
182203
// DisableRetry disables retrying read-only commands under network errors
183204
DisableRetry bool
184-
// RetryDelay is the function that returns the delay that should be used before retrying the attempt.
185-
// The default is an exponential backoff with a maximum delay of 1 second.
186-
// Only used when DisableRetry is false.
187-
RetryDelay RetryDelayFn
188205
// DisableCache falls back Client.DoCache/Client.DoMultiCache to Client.Do/Client.DoMulti
189206
DisableCache bool
190207
// DisableAutoPipelining makes valkey.Client always pick a connection from the BlockingPool to serve each request.
@@ -206,21 +223,6 @@ type ClientOption struct {
206223
// even if we're above the configured client eviction threshold.
207224
ClientNoEvict bool
208225

209-
// ClusterOption is the options for the valkey cluster client.
210-
ClusterOption ClusterOption
211-
212-
// ReplicaSelector selects a replica node when `SendToReplicas` returns true.
213-
// If the function is set, the client will send selected command to the replica node.
214-
// Returned value is the index of the replica node in the replicas slice.
215-
// If the returned value is out of range, the primary node will be selected.
216-
// If primary node does not have any replica, the primary node will be selected
217-
// and function will not be called.
218-
// Currently only used for cluster client.
219-
// Each ReplicaInfo must not be modified.
220-
// NOTE: This function can't be used with ReplicaOnly option.
221-
// NOTE: This function must be used with SendToReplicas function.
222-
ReplicaSelector func(slot uint16, replicas []ReplicaInfo) int
223-
224226
// EnableReplicaAZInfo enables the client to load the replica node's availability zone.
225227
// If true, the client will set the `AZ` field in `ReplicaInfo`.
226228
EnableReplicaAZInfo bool

0 commit comments

Comments
 (0)