Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dbnode] ReadBits to use optimized Read path #2205

Merged
merged 19 commits into from
Mar 18, 2020
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/dbnode/encoding/istream.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,27 @@ package encoding

import (
"bufio"
"encoding/binary"
"io"
"math"
)

const (
numBytesInInt64 = 8
)

// istream encapsulates a readable stream.
type istream struct {
r *bufio.Reader // encoded stream
err error // error encountered
current byte // current byte we are working off of
buffer []byte // buffer for reading in multiple bytes
remaining uint // bits remaining in current to be read
}

// NewIStream creates a new Istream
func NewIStream(reader io.Reader, bufioSize int) IStream {
return &istream{r: bufio.NewReaderSize(reader, bufioSize)}
return &istream{r: bufio.NewReaderSize(reader, bufioSize), buffer: make([]byte, numBytesInInt64, numBytesInInt64)}
rallen090 marked this conversation as resolved.
Show resolved Hide resolved
}

// ReadBit reads the next Bit
Expand Down Expand Up @@ -98,13 +104,23 @@ func (is *istream) ReadBits(numBits uint) (uint64, error) {
return 0, is.err
}
var res uint64
for numBits >= 8 {
byteRead, err := is.ReadByte()
numBytes := numBits / 8
numBits = numBits % 8
rallen090 marked this conversation as resolved.
Show resolved Hide resolved
if numBytes > 0 {
// Use Read call rather than individual ReadByte calls since it has
// optimized path for when the iterator is aligned on a byte boundary.
bytes := is.buffer[0:numBytes]
read, err := is.Read(bytes)
if err != nil {
return 0, err
}
res = (res << 8) | uint64(byteRead)
numBits -= 8
if read == numBytesInInt64 {
res = binary.BigEndian.Uint64(bytes)
rallen090 marked this conversation as resolved.
Show resolved Hide resolved
} else {
for _, b := range bytes {
res = (res << 8) | uint64(b)
}
}
}

for numBits > 0 {
Expand Down