Skip to content

Commit

Permalink
Merge pull request pingcap#808 from pingcap/ngaut/reduce-allocation
Browse files Browse the repository at this point in the history
util: Reduce allocation.
  • Loading branch information
ngaut committed Dec 29, 2015
2 parents 2cc2024 + 202f56b commit 0376f4a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions util/codec/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const (
encPad = byte(0x0)
)

var (
pads = make([]byte, encGroupSize)
encPads = []byte{encPad}
)

// EncodeBytes guarantees the encoded value is in ascending order for comparison,
// encoding with the following rule:
// [group1][marker1]...[groupN][markerN]
Expand All @@ -54,7 +59,7 @@ func EncodeBytes(b []byte, data []byte) []byte {
} else {
padCount = encGroupSize - remain
result = append(result, data[idx:]...)
result = append(result, make([]byte, padCount)...)
result = append(result, pads[:padCount]...)
}

marker := encMarker - byte(padCount)
Expand Down Expand Up @@ -91,7 +96,7 @@ func decodeBytes(b []byte, reverse bool) ([]byte, []byte, error) {

if marker != encMarker {
// Check validity of padding bytes.
if bytes.Count(group[realGroupSize:], []byte{encPad}) != int(padCount) {
if bytes.Count(group[realGroupSize:], encPads) != int(padCount) {
return nil, nil, errors.Errorf("invalid padding byte, group bytes %q", groupBytes)
}

Expand Down

0 comments on commit 0376f4a

Please sign in to comment.