Skip to content

Commit 31b0acf

Browse files
committed
refactor: avoid runtime.convTslice by the sync.Pool of command builder
1 parent 9c426a5 commit 31b0acf

File tree

6 files changed

+9823
-8072
lines changed

6 files changed

+9823
-8072
lines changed

client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ func (c *SingleClient) Info() map[string]proto.Message {
3737

3838
func (c *SingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
3939
resp = c.conn.Do(cmd)
40-
c.Cmd.Put(cmd.Commands())
40+
c.Cmd.Put(cmd.CommandSlice())
4141
return resp
4242
}
4343

4444
func (c *SingleClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp proto.Result) {
4545
resp = c.conn.DoCache(cmd, ttl)
46-
c.Cmd.Put(cmd.Commands())
46+
c.Cmd.Put(cmd.CommandSlice())
4747
return resp
4848
}
4949

@@ -95,7 +95,7 @@ type DedicatedSingleClient struct {
9595

9696
func (c *DedicatedSingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
9797
resp = c.wire.Do(cmd)
98-
c.Cmd.Put(cmd.Commands())
98+
c.Cmd.Put(cmd.CommandSlice())
9999
return resp
100100
}
101101

@@ -105,7 +105,7 @@ func (c *DedicatedSingleClient) DoMulti(ctx context.Context, multi ...cmds.Compl
105105
}
106106
resp = c.wire.DoMulti(multi...)
107107
for _, cmd := range multi {
108-
c.Cmd.Put(cmd.Commands())
108+
c.Cmd.Put(cmd.CommandSlice())
109109
}
110110
return resp
111111
}

cluster.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ process:
271271
}
272272
}
273273
ret:
274-
c.Cmd.Put(cmd.Commands())
274+
c.Cmd.Put(cmd.CommandSlice())
275275
return resp
276276
}
277277

@@ -299,7 +299,7 @@ process:
299299
}
300300
}
301301
ret:
302-
c.Cmd.Put(cmd.Commands())
302+
c.Cmd.Put(cmd.CommandSlice())
303303
return resp
304304
}
305305

@@ -394,7 +394,7 @@ func (c *DedicatedClusterClient) Do(ctx context.Context, cmd cmds.SCompleted) (r
394394
} else {
395395
resp = c.wire.Do(cmds.Completed(cmd))
396396
}
397-
c.Cmd.Put(cmd.Commands())
397+
c.Cmd.Put(cmd.CommandSlice())
398398
return resp
399399
}
400400

@@ -414,7 +414,7 @@ func (c *DedicatedClusterClient) DoMulti(ctx context.Context, multi ...cmds.SCom
414414
}
415415
}
416416
for _, cmd := range multi {
417-
c.Cmd.Put(cmd.Commands())
417+
c.Cmd.Put(cmd.CommandSlice())
418418
}
419419
return resp
420420
}

hack/cmds/gen.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -517,19 +517,20 @@ func toGoName(paramName string) string {
517517
}
518518

519519
func printRootBuilder(w io.Writer, root GoStruct, prefix string) {
520-
fmt.Fprintf(w, "func (b *%sBuilder) %s() %s%s {\n", prefix, root.FullName, prefix, root.FullName)
520+
fmt.Fprintf(w, "func (b *%sBuilder) %s() (c %s%s) {\n", prefix, root.FullName, prefix, root.FullName)
521521

522522
var appends []string
523523
for _, cmd := range root.BuildDef.Command {
524524
appends = append(appends, fmt.Sprintf(`"%s"`, cmd))
525525
}
526526

527527
if tag := rootCf(root); tag != "" {
528-
fmt.Fprintf(w, "\treturn %s%s{cs: append(b.get(), %s), ks: InitSlot, cf: %s}\n", prefix, root.FullName, strings.Join(appends, ", "), tag)
528+
fmt.Fprintf(w, "\tc = %s%s{cs: b.get(), ks: InitSlot, cf: %s}\n", prefix, root.FullName, tag)
529529
} else {
530-
fmt.Fprintf(w, "\treturn %s%s{cs: append(b.get(), %s), ks: InitSlot}\n", prefix, root.FullName, strings.Join(appends, ", "))
530+
fmt.Fprintf(w, "\tc = %s%s{cs: b.get(), ks: InitSlot}\n", prefix, root.FullName)
531531
}
532-
532+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
533+
fmt.Fprintf(w, "\treturn c\n")
533534
fmt.Fprintf(w, "}\n\n")
534535
}
535536

@@ -624,11 +625,11 @@ func printBuilder(w io.Writer, parent, next GoStruct, prefix string) {
624625

625626
if len(appends) == 0 && next.Variadic && len(next.BuildDef.Parameters) == 1 && toGoType(next.BuildDef.Parameters[0].Type) == "string" {
626627
appends = append(appends, toGoName(next.BuildDef.Parameters[0].Name)+"...")
627-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
628+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
628629
} else if len(next.BuildDef.Parameters) != 1 && next.Variadic && parent.FullName != next.FullName {
629630
// no parameter
630631
if len(appends) != 0 {
631-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
632+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
632633
}
633634
} else {
634635
allstring := true
@@ -642,21 +643,21 @@ func printBuilder(w io.Writer, parent, next GoStruct, prefix string) {
642643
for _, p := range next.BuildDef.Parameters {
643644
appends = append(appends, toGoName(p.Name))
644645
}
645-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
646+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
646647
} else {
647648
if len(next.BuildDef.Parameters) == 1 && next.Variadic {
648649
if len(appends) != 0 {
649-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
650+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
650651
}
651652
if toGoType(next.BuildDef.Parameters[0].Type) == "string" {
652-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s...)\n", toGoName(next.BuildDef.Parameters[0].Name))
653+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s...)\n", toGoName(next.BuildDef.Parameters[0].Name))
653654
} else {
654655
fmt.Fprintf(w, "\tfor _, n := range %s {\n", toGoName(next.BuildDef.Parameters[0].Name))
655656
switch toGoType(next.BuildDef.Parameters[0].Type) {
656657
case "float64":
657-
fmt.Fprintf(w, "\t\tc.cs = append(c.cs, strconv.FormatFloat(n, 'f', -1, 64))\n")
658+
fmt.Fprintf(w, "\t\tc.cs.s = append(c.cs.s, strconv.FormatFloat(n, 'f', -1, 64))\n")
658659
case "int64":
659-
fmt.Fprintf(w, "\t\tc.cs = append(c.cs, strconv.FormatInt(n, 10))\n")
660+
fmt.Fprintf(w, "\t\tc.cs.s = append(c.cs.s, strconv.FormatInt(n, 10))\n")
660661
default:
661662
panic("unexpected param type " + next.BuildDef.Parameters[0].Type)
662663
}
@@ -678,9 +679,9 @@ func printBuilder(w io.Writer, parent, next GoStruct, prefix string) {
678679
panic("unexpected param type " + next.BuildDef.Parameters[0].Type)
679680
}
680681
}
681-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
682+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
682683
for _, follow := range follows {
683-
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", follow)
684+
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", follow)
684685
}
685686
}
686687
}

internal/cmds/builder.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,44 @@ package cmds
22

33
import "sync"
44

5+
type CommandSlice struct {
6+
s []string
7+
}
8+
59
func NewBuilder() *Builder {
610
return &Builder{sp: sync.Pool{New: func() interface{} {
7-
return make([]string, 0, 2)
11+
return &CommandSlice{s: make([]string, 0, 2)}
812
}}}
913
}
1014

1115
func NewSBuilder() *SBuilder {
1216
return &SBuilder{sp: sync.Pool{New: func() interface{} {
13-
return make([]string, 0, 2)
17+
return &CommandSlice{s: make([]string, 0, 2)}
1418
}}}
1519
}
1620

1721
type Builder struct {
1822
sp sync.Pool
1923
}
2024

21-
func (b *Builder) get() []string {
22-
return b.sp.Get().([]string)
25+
func (b *Builder) get() *CommandSlice {
26+
return b.sp.Get().(*CommandSlice)
2327
}
2428

25-
func (b *Builder) Put(s []string) {
26-
b.sp.Put(s[:0])
29+
func (b *Builder) Put(cs *CommandSlice) {
30+
cs.s = cs.s[:0]
31+
b.sp.Put(cs)
2732
}
2833

2934
type SBuilder struct {
3035
sp sync.Pool
3136
}
3237

33-
func (b *SBuilder) get() []string {
34-
return b.sp.Get().([]string)
38+
func (b *SBuilder) get() *CommandSlice {
39+
return b.sp.Get().(*CommandSlice)
3540
}
3641

37-
func (b *SBuilder) Put(s []string) {
38-
b.sp.Put(s[:0])
42+
func (b *SBuilder) Put(cs *CommandSlice) {
43+
cs.s = cs.s[:0]
44+
b.sp.Put(cs)
3945
}

internal/cmds/cmds.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ const (
1212

1313
var (
1414
OptInCmd = Completed{
15-
cs: []string{"CLIENT", "CACHING", "YES"},
15+
cs: &CommandSlice{s: []string{"CLIENT", "CACHING", "YES"}},
1616
cf: optInTag,
1717
}
1818
PingCmd = Completed{
19-
cs: []string{"PING"},
19+
cs: &CommandSlice{s: []string{"PING"}},
2020
}
2121
QuitCmd = Completed{
22-
cs: []string{"QUIT"},
22+
cs: &CommandSlice{s: []string{"QUIT"}},
2323
}
2424
SlotCmd = Completed{
25-
cs: []string{"CLUSTER", "SLOTS"},
25+
cs: &CommandSlice{s: []string{"CLUSTER", "SLOTS"}},
2626
}
2727
AskingCmd = Completed{
28-
cs: []string{"ASKING"},
28+
cs: &CommandSlice{s: []string{"ASKING"}},
2929
}
3030
)
3131

3232
type Completed struct {
33-
cs []string
33+
cs *CommandSlice
3434
cf uint16
3535
ks uint16
3636
}
3737

3838
func (c *Completed) IsEmpty() bool {
39-
return len(c.cs) == 0
39+
return c.cs == nil || len(c.cs.s) == 0
4040
}
4141

4242
func (c *Completed) IsOptIn() bool {
@@ -60,13 +60,21 @@ func (c *Completed) IsWrite() bool {
6060
}
6161

6262
func (c *Completed) Commands() []string {
63+
return c.cs.s
64+
}
65+
66+
func (c *Completed) CommandSlice() *CommandSlice {
6367
return c.cs
6468
}
6569

6670
type Cacheable Completed
6771
type SCompleted Completed
6872

6973
func (c *SCompleted) Commands() []string {
74+
return c.cs.s
75+
}
76+
77+
func (c *SCompleted) CommandSlice() *CommandSlice {
7078
return c.cs
7179
}
7280

@@ -81,28 +89,36 @@ func (c *SCacheable) Slot() uint16 {
8189
}
8290

8391
func (c *SCacheable) Commands() []string {
92+
return c.cs.s
93+
}
94+
95+
func (c *SCacheable) CommandSlice() *CommandSlice {
8496
return c.cs
8597
}
8698

8799
func (c *Cacheable) Commands() []string {
100+
return c.cs.s
101+
}
102+
103+
func (c *Cacheable) CommandSlice() *CommandSlice {
88104
return c.cs
89105
}
90106

91107
func (c *Cacheable) CacheKey() (key, command string) {
92-
if len(c.cs) == 2 {
93-
return c.cs[1], c.cs[0]
108+
if len(c.cs.s) == 2 {
109+
return c.cs.s[1], c.cs.s[0]
94110
}
95111

96112
length := 0
97-
for i, v := range c.cs {
113+
for i, v := range c.cs.s {
98114
if i == 1 {
99115
continue
100116
}
101117
length += len(v)
102118
}
103119
sb := strings.Builder{}
104120
sb.Grow(length)
105-
for i, v := range c.cs {
121+
for i, v := range c.cs.s {
106122
if i == 1 {
107123
key = v
108124
} else {
@@ -112,16 +128,16 @@ func (c *Cacheable) CacheKey() (key, command string) {
112128
return key, sb.String()
113129
}
114130

115-
func NewCompleted(cs []string) Completed {
116-
return Completed{cs: cs}
131+
func NewCompleted(ss []string) Completed {
132+
return Completed{cs: &CommandSlice{s: ss}}
117133
}
118134

119-
func NewBlockingCompleted(cs []string) Completed {
120-
return Completed{cs: cs, cf: blockTag}
135+
func NewBlockingCompleted(ss []string) Completed {
136+
return Completed{cs: &CommandSlice{s: ss}, cf: blockTag}
121137
}
122138

123-
func NewReadOnlyCompleted(cs []string) Completed {
124-
return Completed{cs: cs, cf: readonly}
139+
func NewReadOnlyCompleted(ss []string) Completed {
140+
return Completed{cs: &CommandSlice{s: ss}, cf: readonly}
125141
}
126142

127143
func NewMultiCompleted(cs [][]string) []Completed {

0 commit comments

Comments
 (0)