forked from robskie/chd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slicereader_fast.go
39 lines (31 loc) · 1.07 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
//go:build 386 || amd64 || arm || arm64
// +build 386 amd64 arm arm64
package chd
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
start, end uint64
}
func (b *sliceReader) Read(size uint64) []byte {
b.start, b.end = b.end, b.end+size
return b.b[b.start:b.end]
}
func (b *sliceReader) ReadUint64Array(n uint64) []uint64 {
b.start, b.end = b.end, b.end+n*8
return unsafeslice.Uint64SliceFromByteSlice(b.b[b.start:b.end])
}
func (b *sliceReader) ReadUint16Array(n uint64) []uint16 {
b.start, b.end = b.end, b.end+n*2
return unsafeslice.Uint16SliceFromByteSlice(b.b[b.start:b.end])
}
func (b *sliceReader) ReadInt() uint64 {
return uint64(binary.LittleEndian.Uint64(b.Read(8)))
}