Skip to content

Commit

Permalink
refactor: avoid runtime.convTslice by the sync.Pool of command builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Dec 26, 2021
1 parent 9c426a5 commit 31b0acf
Show file tree
Hide file tree
Showing 6 changed files with 9,823 additions and 8,072 deletions.
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (c *SingleClient) Info() map[string]proto.Message {

func (c *SingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
resp = c.conn.Do(cmd)
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

func (c *SingleClient) DoCache(ctx context.Context, cmd cmds.Cacheable, ttl time.Duration) (resp proto.Result) {
resp = c.conn.DoCache(cmd, ttl)
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

Expand Down Expand Up @@ -95,7 +95,7 @@ type DedicatedSingleClient struct {

func (c *DedicatedSingleClient) Do(ctx context.Context, cmd cmds.Completed) (resp proto.Result) {
resp = c.wire.Do(cmd)
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

Expand All @@ -105,7 +105,7 @@ func (c *DedicatedSingleClient) DoMulti(ctx context.Context, multi ...cmds.Compl
}
resp = c.wire.DoMulti(multi...)
for _, cmd := range multi {
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
}
return resp
}
Expand Down
8 changes: 4 additions & 4 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ process:
}
}
ret:
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

Expand Down Expand Up @@ -299,7 +299,7 @@ process:
}
}
ret:
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

Expand Down Expand Up @@ -394,7 +394,7 @@ func (c *DedicatedClusterClient) Do(ctx context.Context, cmd cmds.SCompleted) (r
} else {
resp = c.wire.Do(cmds.Completed(cmd))
}
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
return resp
}

Expand All @@ -414,7 +414,7 @@ func (c *DedicatedClusterClient) DoMulti(ctx context.Context, multi ...cmds.SCom
}
}
for _, cmd := range multi {
c.Cmd.Put(cmd.Commands())
c.Cmd.Put(cmd.CommandSlice())
}
return resp
}
Expand Down
27 changes: 14 additions & 13 deletions hack/cmds/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,19 +517,20 @@ func toGoName(paramName string) string {
}

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

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

if tag := rootCf(root); tag != "" {
fmt.Fprintf(w, "\treturn %s%s{cs: append(b.get(), %s), ks: InitSlot, cf: %s}\n", prefix, root.FullName, strings.Join(appends, ", "), tag)
fmt.Fprintf(w, "\tc = %s%s{cs: b.get(), ks: InitSlot, cf: %s}\n", prefix, root.FullName, tag)
} else {
fmt.Fprintf(w, "\treturn %s%s{cs: append(b.get(), %s), ks: InitSlot}\n", prefix, root.FullName, strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc = %s%s{cs: b.get(), ks: InitSlot}\n", prefix, root.FullName)
}

fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\treturn c\n")
fmt.Fprintf(w, "}\n\n")
}

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

if len(appends) == 0 && next.Variadic && len(next.BuildDef.Parameters) == 1 && toGoType(next.BuildDef.Parameters[0].Type) == "string" {
appends = append(appends, toGoName(next.BuildDef.Parameters[0].Name)+"...")
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
} else if len(next.BuildDef.Parameters) != 1 && next.Variadic && parent.FullName != next.FullName {
// no parameter
if len(appends) != 0 {
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
}
} else {
allstring := true
Expand All @@ -642,21 +643,21 @@ func printBuilder(w io.Writer, parent, next GoStruct, prefix string) {
for _, p := range next.BuildDef.Parameters {
appends = append(appends, toGoName(p.Name))
}
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
} else {
if len(next.BuildDef.Parameters) == 1 && next.Variadic {
if len(appends) != 0 {
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
}
if toGoType(next.BuildDef.Parameters[0].Type) == "string" {
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s...)\n", toGoName(next.BuildDef.Parameters[0].Name))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s...)\n", toGoName(next.BuildDef.Parameters[0].Name))
} else {
fmt.Fprintf(w, "\tfor _, n := range %s {\n", toGoName(next.BuildDef.Parameters[0].Name))
switch toGoType(next.BuildDef.Parameters[0].Type) {
case "float64":
fmt.Fprintf(w, "\t\tc.cs = append(c.cs, strconv.FormatFloat(n, 'f', -1, 64))\n")
fmt.Fprintf(w, "\t\tc.cs.s = append(c.cs.s, strconv.FormatFloat(n, 'f', -1, 64))\n")
case "int64":
fmt.Fprintf(w, "\t\tc.cs = append(c.cs, strconv.FormatInt(n, 10))\n")
fmt.Fprintf(w, "\t\tc.cs.s = append(c.cs.s, strconv.FormatInt(n, 10))\n")
default:
panic("unexpected param type " + next.BuildDef.Parameters[0].Type)
}
Expand All @@ -678,9 +679,9 @@ func printBuilder(w io.Writer, parent, next GoStruct, prefix string) {
panic("unexpected param type " + next.BuildDef.Parameters[0].Type)
}
}
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", strings.Join(appends, ", "))
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", strings.Join(appends, ", "))
for _, follow := range follows {
fmt.Fprintf(w, "\tc.cs = append(c.cs, %s)\n", follow)
fmt.Fprintf(w, "\tc.cs.s = append(c.cs.s, %s)\n", follow)
}
}
}
Expand Down
26 changes: 16 additions & 10 deletions internal/cmds/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,44 @@ package cmds

import "sync"

type CommandSlice struct {
s []string
}

func NewBuilder() *Builder {
return &Builder{sp: sync.Pool{New: func() interface{} {
return make([]string, 0, 2)
return &CommandSlice{s: make([]string, 0, 2)}
}}}
}

func NewSBuilder() *SBuilder {
return &SBuilder{sp: sync.Pool{New: func() interface{} {
return make([]string, 0, 2)
return &CommandSlice{s: make([]string, 0, 2)}
}}}
}

type Builder struct {
sp sync.Pool
}

func (b *Builder) get() []string {
return b.sp.Get().([]string)
func (b *Builder) get() *CommandSlice {
return b.sp.Get().(*CommandSlice)
}

func (b *Builder) Put(s []string) {
b.sp.Put(s[:0])
func (b *Builder) Put(cs *CommandSlice) {
cs.s = cs.s[:0]
b.sp.Put(cs)
}

type SBuilder struct {
sp sync.Pool
}

func (b *SBuilder) get() []string {
return b.sp.Get().([]string)
func (b *SBuilder) get() *CommandSlice {
return b.sp.Get().(*CommandSlice)
}

func (b *SBuilder) Put(s []string) {
b.sp.Put(s[:0])
func (b *SBuilder) Put(cs *CommandSlice) {
cs.s = cs.s[:0]
b.sp.Put(cs)
}
50 changes: 33 additions & 17 deletions internal/cmds/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ const (

var (
OptInCmd = Completed{
cs: []string{"CLIENT", "CACHING", "YES"},
cs: &CommandSlice{s: []string{"CLIENT", "CACHING", "YES"}},
cf: optInTag,
}
PingCmd = Completed{
cs: []string{"PING"},
cs: &CommandSlice{s: []string{"PING"}},
}
QuitCmd = Completed{
cs: []string{"QUIT"},
cs: &CommandSlice{s: []string{"QUIT"}},
}
SlotCmd = Completed{
cs: []string{"CLUSTER", "SLOTS"},
cs: &CommandSlice{s: []string{"CLUSTER", "SLOTS"}},
}
AskingCmd = Completed{
cs: []string{"ASKING"},
cs: &CommandSlice{s: []string{"ASKING"}},
}
)

type Completed struct {
cs []string
cs *CommandSlice
cf uint16
ks uint16
}

func (c *Completed) IsEmpty() bool {
return len(c.cs) == 0
return c.cs == nil || len(c.cs.s) == 0
}

func (c *Completed) IsOptIn() bool {
Expand All @@ -60,13 +60,21 @@ func (c *Completed) IsWrite() bool {
}

func (c *Completed) Commands() []string {
return c.cs.s
}

func (c *Completed) CommandSlice() *CommandSlice {
return c.cs
}

type Cacheable Completed
type SCompleted Completed

func (c *SCompleted) Commands() []string {
return c.cs.s
}

func (c *SCompleted) CommandSlice() *CommandSlice {
return c.cs
}

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

func (c *SCacheable) Commands() []string {
return c.cs.s
}

func (c *SCacheable) CommandSlice() *CommandSlice {
return c.cs
}

func (c *Cacheable) Commands() []string {
return c.cs.s
}

func (c *Cacheable) CommandSlice() *CommandSlice {
return c.cs
}

func (c *Cacheable) CacheKey() (key, command string) {
if len(c.cs) == 2 {
return c.cs[1], c.cs[0]
if len(c.cs.s) == 2 {
return c.cs.s[1], c.cs.s[0]
}

length := 0
for i, v := range c.cs {
for i, v := range c.cs.s {
if i == 1 {
continue
}
length += len(v)
}
sb := strings.Builder{}
sb.Grow(length)
for i, v := range c.cs {
for i, v := range c.cs.s {
if i == 1 {
key = v
} else {
Expand All @@ -112,16 +128,16 @@ func (c *Cacheable) CacheKey() (key, command string) {
return key, sb.String()
}

func NewCompleted(cs []string) Completed {
return Completed{cs: cs}
func NewCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}}
}

func NewBlockingCompleted(cs []string) Completed {
return Completed{cs: cs, cf: blockTag}
func NewBlockingCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}, cf: blockTag}
}

func NewReadOnlyCompleted(cs []string) Completed {
return Completed{cs: cs, cf: readonly}
func NewReadOnlyCompleted(ss []string) Completed {
return Completed{cs: &CommandSlice{s: ss}, cf: readonly}
}

func NewMultiCompleted(cs [][]string) []Completed {
Expand Down
Loading

0 comments on commit 31b0acf

Please sign in to comment.