Skip to content

Commit

Permalink
huff0: Reduce bounds checking (#734)
Browse files Browse the repository at this point in the history
Apparently, Go 1.19 understands that v := b.in[b.off-4 : b.off] has
length four, but then inserts an additional check for v = v[:4].
Removing the latter ensures the comments saying "2 bounds checks" are
correct again (two checks in a single line).
  • Loading branch information
greatroar committed Jan 7, 2023
1 parent ded586e commit 272358c
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions huff0/bitreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func (b *bitReaderBytes) fillFast() {

// 2 bounds checks.
v := b.in[b.off-4 : b.off]
v = v[:4]
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
b.value |= uint64(low) << (b.bitsRead - 32)
b.bitsRead -= 32
Expand All @@ -88,8 +87,7 @@ func (b *bitReaderBytes) fill() {
return
}
if b.off > 4 {
v := b.in[b.off-4:]
v = v[:4]
v := b.in[b.off-4 : b.off]
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
b.value |= uint64(low) << (b.bitsRead - 32)
b.bitsRead -= 32
Expand Down Expand Up @@ -179,7 +177,6 @@ func (b *bitReaderShifted) fillFast() {

// 2 bounds checks.
v := b.in[b.off-4 : b.off]
v = v[:4]
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
b.bitsRead -= 32
Expand All @@ -200,8 +197,7 @@ func (b *bitReaderShifted) fill() {
return
}
if b.off > 4 {
v := b.in[b.off-4:]
v = v[:4]
v := b.in[b.off-4 : b.off]
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
b.value |= uint64(low) << ((b.bitsRead - 32) & 63)
b.bitsRead -= 32
Expand Down

0 comments on commit 272358c

Please sign in to comment.