func (e *int64Encoder) encodePacked() ([]byte, error) { if len(e.values) == 0 { return nil, nil } // Encode all but the first value. Fist value is written unencoded // using 8 bytes. encoded, err := simple8b.EncodeAll(e.values[1:]) if err != nil { return nil, err } b := make([]byte, 1+(len(encoded)+1)*8) // 4 high bits of first byte store the encoding type for the block b[0] = byte(intCompressedSimple) << 4 // Write the first value since it's not part of the encoded values binary.BigEndian.PutUint64(b[1:9], e.values[0]) // Write the encoded values for i, v := range encoded { binary.BigEndian.PutUint64(b[9+i*8:9+i*8+8], v) } return b, nil }
func Test_TooBig(t *testing.T) { values := 1 in := make([]uint64, values) for i := 0; i < values; i++ { in[i] = 2<<61 - 1 } _, err := simple8b.EncodeAll(in) if err == nil { t.Fatalf("expected error, got nil") } }
func Test_Encode_NoValues(t *testing.T) { var in []uint64 encoded, _ := simple8b.EncodeAll(in) decoded := make([]uint64, len(in)) n, _ := simple8b.DecodeAll(decoded, encoded) if len(in) != len(decoded[:n]) { t.Fatalf("Len mismatch: got %v, exp %v", len(decoded), len(in)) } }
func BenchmarkEncode(b *testing.B) { total := 0 x := make([]uint64, 1024) for i := 0; i < len(x); i++ { x[i] = uint64(15) } b.ResetTimer() for i := 0; i < b.N; i++ { simple8b.EncodeAll(x) b.SetBytes(int64(len(x) * 8)) total += len(x) } }
func (e *int64Encoder) encodePacked() ([]byte, error) { encoded, err := simple8b.EncodeAll(e.values) if err != nil { return nil, err } b := make([]byte, 1+len(encoded)*8) // 4 high bits of first byte store the encoding type for the block b[0] = byte(intCompressedSimple) << 4 for i, v := range encoded { binary.BigEndian.PutUint64(b[1+i*8:1+i*8+8], v) } return b, nil }
func BenchmarkDecode(b *testing.B) { total := 0 x := make([]uint64, 1024) for i := 0; i < len(x); i++ { x[i] = uint64(10) } y, _ := simple8b.EncodeAll(x) decoded := make([]uint64, len(x)) b.ResetTimer() for i := 0; i < b.N; i++ { _, _ = simple8b.DecodeAll(decoded, y) b.SetBytes(int64(len(decoded) * 8)) total += len(decoded) } }