Skip to content

Commit

Permalink
Small performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
flanglet committed Nov 9, 2024
1 parent b80c65e commit 8cb467e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
21 changes: 10 additions & 11 deletions v2/bitstream/DefaultInputBitStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func NewDefaultInputBitStream(stream io.ReadCloser, bufferSize uint) (*DefaultIn
// ReadBit returns the next bit
func (this *DefaultInputBitStream) ReadBit() int {
if this.availBits == 0 {
this.pullCurrent() // Panic if stream is closed
this.current, this.availBits = this.pull() // Panic if stream is closed
}

this.availBits--
Expand All @@ -88,7 +88,7 @@ func (this *DefaultInputBitStream) ReadBits(count uint) uint64 {
// Not enough spots available in 'current'
count -= this.availBits
res := this.current & (0xFFFFFFFFFFFFFFFF >> (64 - this.availBits))
this.pullCurrent()
this.current, this.availBits = this.pull()
return (res << count) | this.ReadBits(count) // handle this.availBits < count (at EOS)
}

Expand All @@ -110,7 +110,7 @@ func (this *DefaultInputBitStream) ReadArray(bits []byte, count uint) uint {
// Byte aligned cursor ?
if this.availBits&7 == 0 {
if this.availBits == 0 {
this.pullCurrent()
this.current, this.availBits = this.pull()
}

// Empty this.current
Expand Down Expand Up @@ -153,7 +153,7 @@ func (this *DefaultInputBitStream) ReadArray(bits []byte, count uint) uint {
v0 := this.current

if this.position+32 > this.maxPosition {
this.pullCurrent()
this.current, this.availBits = this.pull()

if this.availBits < r {
panic("No more data to read in the bitstream")
Expand Down Expand Up @@ -182,7 +182,7 @@ func (this *DefaultInputBitStream) ReadArray(bits []byte, count uint) uint {

for remaining >= 64 {
v := this.current
this.pullCurrent()
this.current, this.availBits = this.pull()

if this.availBits < r {
panic("No more data to read in the bitstream")
Expand Down Expand Up @@ -246,8 +246,8 @@ func (this *DefaultInputBitStream) HasMoreToRead() (bool, error) {
return err == nil, err
}

// Pull 64 bits of current value from buffer.
func (this *DefaultInputBitStream) pullCurrent() {
// Pull 64 bits from buffer.
func (this *DefaultInputBitStream) pull() (uint64, uint) {
if this.position > this.maxPosition {
if _, err := this.readFromInputStream(len(this.buffer)); err != nil {
panic(err)
Expand All @@ -257,7 +257,7 @@ func (this *DefaultInputBitStream) pullCurrent() {
if this.position+7 > this.maxPosition {
// End of stream: overshoot max position => adjust bit index
shift := uint(this.maxPosition-this.position) << 3
this.availBits = shift + 8
avail := shift + 8
val := uint64(0)

for this.position <= this.maxPosition {
Expand All @@ -266,12 +266,11 @@ func (this *DefaultInputBitStream) pullCurrent() {
shift -= 8
}

this.current = val
return val, avail
} else {
// Regular processing, buffer length is multiple of 8
this.current = binary.BigEndian.Uint64(this.buffer[this.position : this.position+8])
this.availBits = 64
this.position += 8
return binary.BigEndian.Uint64(this.buffer[this.position-8 : this.position]), 64
}

}
Expand Down
23 changes: 11 additions & 12 deletions v2/bitstream/DefaultOutputBitStream.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func NewDefaultOutputBitStream(stream io.WriteCloser, bufferSize uint) (*Default

// WriteBit writes the least significant bit of the input integer. Panics if the bitstream is closed
func (this *DefaultOutputBitStream) WriteBit(bit int) {
if this.availBits <= 1 { // availBits = 0 if stream is closed => force pushCurrent() => panic
this.current |= uint64(bit & 1)
this.pushCurrent()
if this.availBits <= 1 { // availBits = 0 if stream is closed => force push() => panic
this.push(this.current | uint64(bit&1))
this.current = 0
this.availBits = 64
} else {
this.availBits--
this.current |= (uint64(bit&1) << this.availBits)
Expand All @@ -84,9 +85,9 @@ func (this *DefaultOutputBitStream) WriteBits(value uint64, count uint) uint {
if count >= this.availBits {
// Not enough spots available in 'current'
remaining := count - this.availBits
this.pushCurrent()
this.push(this.current)
this.current = value << (64 - remaining)
this.availBits -= remaining
this.availBits = 64 - remaining
} else {
this.availBits -= count
}
Expand Down Expand Up @@ -172,8 +173,8 @@ func (this *DefaultOutputBitStream) WriteArray(bits []byte, count uint) uint {

for remaining >= 64 {
val := binary.BigEndian.Uint64(bits[start:])
this.current |= (val >> r)
this.pushCurrent()
this.push(this.current | (val >> r))
this.availBits = 64
this.current = val << a
start += 8
remaining -= 64
Expand All @@ -197,11 +198,9 @@ func (this *DefaultOutputBitStream) WriteArray(bits []byte, count uint) uint {
return count
}

// Push 64 bits of current value into buffer.
func (this *DefaultOutputBitStream) pushCurrent() {
binary.BigEndian.PutUint64(this.buffer[this.position:this.position+8], this.current)
this.availBits = 64
this.current = 0
// Push 64 bits into buffer.
func (this *DefaultOutputBitStream) push(val uint64) {
binary.BigEndian.PutUint64(this.buffer[this.position:this.position+8], val)
this.position += 8

if this.position >= len(this.buffer)-8 {
Expand Down

0 comments on commit 8cb467e

Please sign in to comment.