Skip to content

Commit

Permalink
chore: improve ScanStruct performance (#523)
Browse files Browse the repository at this point in the history
Improve the performance of ScanStruct by using strings.IndexByte instead
of strings.Split.

Co-authored-by: yonbiaoxiao <yonbiaoxiao@tencent.com>
  • Loading branch information
imxyb and yonbiaoxiao authored Oct 15, 2020
1 parent e340398 commit 51dc771
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions redis/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ func (ss *structSpec) fieldSpec(name []byte) *fieldSpec {
}

func compileStructSpec(t reflect.Type, depth map[string]int, index []int, ss *structSpec) {
LOOP:
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
switch {
Expand All @@ -374,20 +375,30 @@ func compileStructSpec(t reflect.Type, depth map[string]int, index []int, ss *st
default:
fs := &fieldSpec{name: f.Name}
tag := f.Tag.Get("redis")
p := strings.Split(tag, ",")
if len(p) > 0 {
if p[0] == "-" {
continue

var (
p string
)
first := true
for len(tag) > 0 {
i := strings.IndexByte(tag, ',')
if i < 0 {
p, tag = tag, ""
} else {
p, tag = tag[:i], tag[i+1:]
}
if len(p[0]) > 0 {
fs.name = p[0]
if p == "-" {
continue LOOP
}
for _, s := range p[1:] {
switch s {
if first && len(p) > 0 {
fs.name = p
first = false
} else {
switch p {
case "omitempty":
fs.omitEmpty = true
default:
panic(fmt.Errorf("redigo: unknown field tag %s for type %s", s, t.Name()))
panic(fmt.Errorf("redigo: unknown field tag %s for type %s", p, t.Name()))
}
}
}
Expand Down

0 comments on commit 51dc771

Please sign in to comment.