forked from alecthomas/mph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slicereader_fast.go
44 lines (36 loc) · 1.15 KB
/
slicereader_fast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//go:build 386 || amd64 || arm
// +build 386 amd64 arm
package mph
import (
"encoding/binary"
"github.com/alecthomas/unsafeslice"
)
// Read values and typed vectors from a byte slice without copying where
// possible. This implementation directly references the underlying byte slice
// for array operations, making them essentially zero copy. As the data is
// written in little endian form, this of course means that this will only
// work on little-endian architectures.
type sliceReader struct {
b []byte
pos uint64
}
func (b *sliceReader) Read(size uint64) []byte {
start := b.pos
b.pos += size
return b.b[start:b.pos]
}
func (b *sliceReader) ReadUint64Array(n uint64) []uint64 {
start := b.pos
b.pos += n * 8
return unsafeslice.Uint64SliceFromByteSlice(b.b[start:b.pos])
}
func (b *sliceReader) ReadUint16Array(n uint64) []uint16 {
start := b.pos
b.pos += n * 2
return unsafeslice.Uint16SliceFromByteSlice(b.b[start:b.pos])
}
// Despite returning a uint64, this actually reads a uint32. All table indices
// and lengths are stored as uint32 values.
func (b *sliceReader) ReadInt() uint64 {
return uint64(binary.LittleEndian.Uint32(b.Read(4)))
}