Skip to content

Commit

Permalink
optimize nested conditional (zeromicro#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored May 22, 2021
1 parent 8998f16 commit aaa3623
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
27 changes: 15 additions & 12 deletions core/search/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (t *Tree) next(n *node, route string, result *Result) bool {
if route[i] != slash {
continue
}

token := route[:i]
return n.forEach(func(k string, v *node) bool {
r := match(k, token)
Expand Down Expand Up @@ -163,21 +164,23 @@ func add(nd *node, route string, item interface{}) error {
}

for i := range route {
if route[i] == slash {
token := route[:i]
children := nd.getChildren(token)
if child, ok := children[token]; ok {
if child != nil {
return add(child, route[i+1:], item)
}

return errInvalidState
if route[i] != slash {
continue
}

token := route[:i]
children := nd.getChildren(token)
if child, ok := children[token]; ok {
if child != nil {
return add(child, route[i+1:], item)
}

child := newNode(nil)
children[token] = child
return add(child, route[i+1:], item)
return errInvalidState
}

child := newNode(nil)
children[token] = child
return add(child, route[i+1:], item)
}

children := nd.getChildren(route)
Expand Down
4 changes: 2 additions & 2 deletions core/stores/sqlx/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func format(query string, args ...interface{}) (string, error) {
}

var b strings.Builder
argIndex := 0

var argIndex int
bytes := len(query)

for i := 0; i < bytes; i++ {
ch := query[i]
switch ch {
Expand Down

0 comments on commit aaa3623

Please sign in to comment.