Skip to content

Commit

Permalink
Optimize Rebind performance and allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
nussjustin committed Feb 17, 2017
1 parent f980a91 commit d0322a1
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,28 @@ func Rebind(bindType int, query string) string {
return query
}

qb := []byte(query)
// Add space enough for 10 params before we have to allocate
rqb := make([]byte, 0, len(qb)+10)
j := 1
for _, b := range qb {
if b == '?' {
switch bindType {
case DOLLAR:
rqb = append(rqb, '$')
case NAMED:
rqb = append(rqb, ':', 'a', 'r', 'g')
}
for _, b := range strconv.Itoa(j) {
rqb = append(rqb, byte(b))
}
j++
} else {
rqb = append(rqb, b)
rqb := make([]byte, 0, len(query)+10)

var i, j int

for i = strings.Index(query, "?"); i != -1; i = strings.Index(query, "?") {
rqb = append(rqb, query[:i]...)

switch bindType {
case DOLLAR:
rqb = append(rqb, '$')
case NAMED:
rqb = append(rqb, ':', 'a', 'r', 'g')
}

j++
rqb = strconv.AppendInt(rqb, int64(j), 10)

query = query[i+1:]
}
return string(rqb)

return string(append(rqb, query...))
}

// Experimental implementation of Rebind which uses a bytes.Buffer. The code is
Expand Down

0 comments on commit d0322a1

Please sign in to comment.