Have flatecut use a 64-bit bit buffer, not 32

name    old time/op  new time/op  delta
pkg:github.com/google/wuffs/lib/flatecut goos:linux goarch:amd64
Cut-56   919µs ± 0%   867µs ± 0%  -5.70%  (p=0.008 n=5+5)
pkg:github.com/google/wuffs/lib/zlibcut goos:linux goarch:amd64
Cut-56  2.34ms ± 1%  2.28ms ± 0%  -2.46%  (p=0.008 n=5+5)
This commit is contained in:
Nigel Tao committed 2019-04-20 09:59:52 +10:00
1 parent fe3767ad6c
commit b54f3b4df3
1 file changed
+15 -13
+15 -13
View File
@@ -84,9 +84,11 @@ const (
mostNegativeInt32 = -0x80000000 mostNegativeInt32 = -0x80000000
) )
func loadU32LE(b []byte) uint32 { func loadU64LE(b []byte) uint64 {
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808 _ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
} }
type bitstream struct { type bitstream struct {
@@ -96,7 +98,7 @@ type bitstream struct {
// The low nBits bits of the 'bits' field hold the next bits (in LSB-first // The low nBits bits of the 'bits' field hold the next bits (in LSB-first
// order). // order).
bits uint32 bits uint64
nBits uint32 nBits uint32
} }
@@ -105,13 +107,13 @@ func (b *bitstream) take(nBits uint32) int32 {
if b.index >= len(b.bytes) { if b.index >= len(b.bytes) {
return mostNegativeInt32 return mostNegativeInt32
} }
b.bits |= uint32(b.bytes[b.index]) << b.nBits b.bits |= uint64(b.bytes[b.index]) << b.nBits
b.nBits += 8 b.nBits += 8
b.index++ b.index++
} }
mask := ((uint32(1)) << nBits) - 1 mask := ((uint32(1)) << nBits) - 1
ret := b.bits & mask ret := uint32(b.bits) & mask
b.bits >>= nBits b.bits >>= nBits
b.nBits -= nBits b.nBits -= nBits
return int32(ret) return int32(ret)
@@ -162,15 +164,15 @@ type huffman struct {
func (h *huffman) decode(b *bitstream) int32 { func (h *huffman) decode(b *bitstream) int32 {
if b.nBits >= 8 { if b.nBits >= 8 {
// No-op. // No-op.
} else if b.index < (len(b.bytes) - 4) { } else if b.index < (len(b.bytes) - 8) {
// This is "Variant 4" of // This is "Variant 4" of
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/ // https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
u := loadU32LE(b.bytes[b.index:]) u := loadU64LE(b.bytes[b.index:])
b.bits |= u << b.nBits b.bits |= u << b.nBits
b.index += int((31 - b.nBits) >> 3) b.index += int((63 - b.nBits) >> 3)
b.nBits |= 24 b.nBits |= 56
} else if b.index < len(b.bytes) { } else if b.index < len(b.bytes) {
b.bits |= uint32(b.bytes[b.index]) << b.nBits b.bits |= uint64(b.bytes[b.index]) << b.nBits
b.nBits += 8 b.nBits += 8
b.index++ b.index++
} else { } else {
@@ -202,12 +204,12 @@ func (h *huffman) slowDecode(b *bitstream) int32 {
if b.index >= len(b.bytes) { if b.index >= len(b.bytes) {
return mostNegativeInt32 return mostNegativeInt32
} }
b.bits = uint32(b.bytes[b.index]) b.bits = uint64(b.bytes[b.index])
b.nBits = 8 b.nBits = 8
b.index++ b.index++
} }
code |= b.bits & 1 code |= uint32(b.bits & 1)
b.bits >>= 1 b.bits >>= 1
b.nBits -= 1 b.nBits -= 1