Skip to content

Commit 07a5dff

Browse files
committed
Use in more places.
1 parent 75ce7ef commit 07a5dff

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

flate/huffman_bit_writer.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
package flate
66

77
import (
8-
"encoding/binary"
98
"fmt"
109
"io"
1110
"math"
11+
12+
"github.com/klauspost/compress/internal/le"
1213
)
1314

1415
const (
@@ -438,7 +439,7 @@ func (w *huffmanBitWriter) writeOutBits() {
438439
n := w.nbytes
439440

440441
// We over-write, but faster...
441-
binary.LittleEndian.PutUint64(w.bytes[n:], bits)
442+
le.Store64(w.bytes[n:], bits)
442443
n += 6
443444

444445
if n >= bufferFlushSize {
@@ -854,7 +855,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
854855
bits |= c.code64() << (nbits & 63)
855856
nbits += c.len()
856857
if nbits >= 48 {
857-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
858+
le.Store64(w.bytes[nbytes:], bits)
858859
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
859860
bits >>= 48
860861
nbits -= 48
@@ -882,7 +883,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
882883
bits |= c.code64() << (nbits & 63)
883884
nbits += c.len()
884885
if nbits >= 48 {
885-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
886+
le.Store64(w.bytes[nbytes:], bits)
886887
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
887888
bits >>= 48
888889
nbits -= 48
@@ -905,7 +906,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
905906
bits |= uint64(extraLength) << (nbits & 63)
906907
nbits += extraLengthBits
907908
if nbits >= 48 {
908-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
909+
le.Store64(w.bytes[nbytes:], bits)
909910
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
910911
bits >>= 48
911912
nbits -= 48
@@ -931,7 +932,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
931932
bits |= c.code64() << (nbits & 63)
932933
nbits += c.len()
933934
if nbits >= 48 {
934-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
935+
le.Store64(w.bytes[nbytes:], bits)
935936
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
936937
bits >>= 48
937938
nbits -= 48
@@ -953,7 +954,7 @@ func (w *huffmanBitWriter) writeTokens(tokens []token, leCodes, oeCodes []hcode)
953954
bits |= uint64((offset-(offsetComb>>8))&matchOffsetOnlyMask) << (nbits & 63)
954955
nbits += uint8(offsetComb)
955956
if nbits >= 48 {
956-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
957+
le.Store64(w.bytes[nbytes:], bits)
957958
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
958959
bits >>= 48
959960
nbits -= 48
@@ -1107,7 +1108,7 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
11071108
// We must have at least 48 bits free.
11081109
if nbits >= 8 {
11091110
n := nbits >> 3
1110-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
1111+
le.Store64(w.bytes[nbytes:], bits)
11111112
bits >>= (n * 8) & 63
11121113
nbits -= n * 8
11131114
nbytes += n
@@ -1136,7 +1137,7 @@ func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) {
11361137
// Remaining...
11371138
for _, t := range input {
11381139
if nbits >= 48 {
1139-
binary.LittleEndian.PutUint64(w.bytes[nbytes:], bits)
1140+
le.Store64(w.bytes[nbytes:], bits)
11401141
//*(*uint64)(unsafe.Pointer(&w.bytes[nbytes])) = bits
11411142
bits >>= 48
11421143
nbits -= 48

flate/level1.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package flate
22

33
import (
4-
"encoding/binary"
54
"fmt"
65
"math/bits"
6+
7+
"github.com/klauspost/compress/internal/le"
78
)
89

910
// fastGen maintains the table for matches,
@@ -126,26 +127,26 @@ func (e *fastEncL1) Encode(dst *tokens, src []byte) {
126127
l = e.matchlenLong(s+4, t+4, src) + 4
127128
} else {
128129
// inlined:
129-
a := src[s+4:]
130-
b := src[t+4:]
131-
for len(a) >= 8 {
132-
if diff := binary.LittleEndian.Uint64(a) ^ binary.LittleEndian.Uint64(b); diff != 0 {
130+
a := src[s:]
131+
b := src[t:]
132+
left := len(a)
133+
for left >= 8 {
134+
if diff := le.Load64(a, l) ^ le.Load64(b, l); diff != 0 {
133135
l += int32(bits.TrailingZeros64(diff) >> 3)
134-
break
136+
goto endMatch
135137
}
136138
l += 8
137-
a = a[8:]
138-
b = b[8:]
139+
left -= 8
139140
}
140-
if len(a) < 8 {
141-
b = b[:len(a)]
142-
for i := range a {
143-
if a[i] != b[i] {
144-
break
145-
}
146-
l++
141+
a = a[l:]
142+
b = b[l:]
143+
for i := range a {
144+
if a[i] != b[i] {
145+
break
147146
}
147+
l++
148148
}
149+
endMatch:
149150
}
150151

151152
// Extend backwards

internal/le/unsafe_enabled.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,40 @@ import (
99
)
1010

1111
// Load16 will load from b at index i.
12-
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
1312
func Load16[I Indexer](b []byte, i I) uint16 {
1413
//return binary.LittleEndian.Uint16(b[i:])
1514
//return *(*uint16)(unsafe.Pointer(&b[i]))
1615
return *(*uint16)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
1716
}
1817

1918
// Load32 will load from b at index i.
20-
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
2119
func Load32[I Indexer](b []byte, i I) uint32 {
2220
//return binary.LittleEndian.Uint32(b[i:])
2321
//return *(*uint32)(unsafe.Pointer(&b[i]))
2422
return *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
2523
}
2624

2725
// Load64 will load from b at index i.
28-
// If the compiler can prove that b is at least 1 byte this will be without bounds check.
2926
func Load64[I Indexer](b []byte, i I) uint64 {
3027
//return binary.LittleEndian.Uint64(b[i:])
3128
//return *(*uint64)(unsafe.Pointer(&b[i]))
3229
return *(*uint64)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(b))) + uintptr(i)*unsafe.Sizeof(b[0])))
3330
}
3431

3532
// Store16 will store v at b.
36-
// If the compiler can prove
3733
func Store16(b []byte, v uint16) {
3834
//binary.LittleEndian.PutUint16(b, v)
3935
*(*uint16)(unsafe.Pointer(unsafe.SliceData(b))) = v
4036
}
4137

38+
// Store32 will store v at b.
4239
func Store32(b []byte, v uint32) {
4340
//binary.LittleEndian.PutUint32(b, v)
4441
*(*uint32)(unsafe.Pointer(unsafe.SliceData(b))) = v
4542
}
43+
44+
// Store64 will store v at b.
45+
func Store64(b []byte, v uint64) {
46+
//binary.LittleEndian.PutUint64(b, v)
47+
*(*uint64)(unsafe.Pointer(unsafe.SliceData(b))) = v
48+
}

0 commit comments

Comments
 (0)