nocopy

package
v1.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2024 License: MIT Imports: 3 Imported by: 0

README

Types with no-copy decoding

This sub-package contains a set of typed sclices which can be useful for encoding/decoding large numerical slices faster. This is relatively unsafe and non-portable as the encoding simply copies the memory of the slice, hence disregarding byte order of the encoder/decoders. However, this lets us to avoid allocating and copying memory when encoding/decoding, making this at least 10x faster than the safe implementation.

Warning

This implementation simply maps the byte slice provided in Unmarshal call to the Go structs which need to be decoded. This simply reuses the underlying byte array to store the data and does not perform a memory copy. This can be dangerous in many cases, be careful how this is used!

Benchmark

Array of 10K elements:

BenchmarkUint64s_Safe/marshal-8         	   10000	    167612 ns/op	   78325 B/op	      11 allocs/op
BenchmarkUint64s_Safe/unmarshal-8       	    5000	    286769 ns/op	   81975 B/op	       2 allocs/op
BenchmarkUint64s_Unsafe/marshal-8         	  100000	     10711 ns/op	   82055 B/op	       3 allocs/op
BenchmarkUint64s_Unsafe/unmarshal-8       	20000000	       109 ns/op	       0 B/op	       0 allocs/op

Usage

This is a drop-in type, so simply use one of the types available in the package (Bools, Int32s, Uint64s ...) and Marshal or Unmarshal using the binary package.

// Marshal some numbers
v := nocopy.Int32s{4, 5, 6, 1, 2, 3}
encoded, err := binary.Marshal(&v)

// Unmarshal the numbers
var o nocopy.Int32s
err = binary.Unmarshal(encoded, &o)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bools

type Bools []bool

Bools represents a type serialized in an unsafe, non portable manner. Moreover, when decoding it simply reuses the underlying byte array to store the data and does not perform a memory copy. This can be dangerous in many cases, be careful how this is used.

func (*Bools) GetBinaryCodec

func (s *Bools) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type ByteMap added in v1.0.8

type ByteMap map[string][]byte

ByteMap represents a map where keys are strings but the values are slices of bytes. It is encoded in an unsafe, non portable mapper.

func (*ByteMap) GetBinaryCodec added in v1.0.8

func (d *ByteMap) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type Bytes

type Bytes []byte

Bytes represents a type serialized in an unsafe, non portable manner. Moreover, when decoding it simply reuses the underlying byte array to store the data and does not perform a memory copy. This can be dangerous in many cases, be careful how this is used.

func (*Bytes) GetBinaryCodec

func (s *Bytes) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type Dictionary added in v1.0.4

type Dictionary map[string]string

Dictionary represents a map where both keys and values are strings. It is serialized in an unsafe, non portable manner.

func (*Dictionary) GetBinaryCodec added in v1.0.4

func (d *Dictionary) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type Float32s

type Float32s []float32

Float32s represents a slice serialized in an unsafe, non portable manner.

func (*Float32s) GetBinaryCodec

func (s *Float32s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Float32s) Len

func (s Float32s) Len() int

func (Float32s) Less

func (s Float32s) Less(i, j int) bool

func (Float32s) Swap

func (s Float32s) Swap(i, j int)

type Float64s

type Float64s []float64

Float64s represents a slice serialized in an unsafe, non portable manner.

func (*Float64s) GetBinaryCodec

func (s *Float64s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Float64s) Len

func (s Float64s) Len() int

func (Float64s) Less

func (s Float64s) Less(i, j int) bool

func (Float64s) Swap

func (s Float64s) Swap(i, j int)

type HashMap added in v1.0.10

type HashMap map[uint64][]byte

HashMap represents a map where keys are uint64 but the values are slices of bytes. It is encoded in an unsafe, non portable mapper.

func (*HashMap) GetBinaryCodec added in v1.0.10

func (d *HashMap) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type Int16s

type Int16s []int16

Int16s represents a slice serialized in an unsafe, non portable manner.

func (*Int16s) GetBinaryCodec

func (s *Int16s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Int16s) Len

func (s Int16s) Len() int

func (Int16s) Less

func (s Int16s) Less(i, j int) bool

func (Int16s) Swap

func (s Int16s) Swap(i, j int)

type Int32s

type Int32s []int32

Int32s represents a slice serialized in an unsafe, non portable manner.

func (*Int32s) GetBinaryCodec

func (s *Int32s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Int32s) Len

func (s Int32s) Len() int

func (Int32s) Less

func (s Int32s) Less(i, j int) bool

func (Int32s) Swap

func (s Int32s) Swap(i, j int)

type Int64s

type Int64s []int64

Int64s represents a slice serialized in an unsafe, non portable manner.

func (*Int64s) GetBinaryCodec

func (s *Int64s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Int64s) Len

func (s Int64s) Len() int

func (Int64s) Less

func (s Int64s) Less(i, j int) bool

func (Int64s) Swap

func (s Int64s) Swap(i, j int)

type String

type String string

String represents a type serialized in an unsafe, non portable manner. Moreover, when decoding it simply reuses the underlying byte array to store the data and does not perform a memory copy. This can be dangerous in many cases, be careful how this is used.

func (*String) GetBinaryCodec

func (s *String) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

type Uint16s

type Uint16s []uint16

Uint16s represents a slice serialized in an unsafe, non portable manner.

func (*Uint16s) GetBinaryCodec

func (s *Uint16s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Uint16s) Len

func (s Uint16s) Len() int

func (Uint16s) Less

func (s Uint16s) Less(i, j int) bool

func (Uint16s) Swap

func (s Uint16s) Swap(i, j int)

type Uint32s

type Uint32s []uint32

Uint32s represents a slice serialized in an unsafe, non portable manner.

func (*Uint32s) GetBinaryCodec

func (s *Uint32s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Uint32s) Len

func (s Uint32s) Len() int

func (Uint32s) Less

func (s Uint32s) Less(i, j int) bool

func (Uint32s) Swap

func (s Uint32s) Swap(i, j int)

type Uint64s

type Uint64s []uint64

Uint64s represents a slice serialized in an unsafe, non portable manner.

func (*Uint64s) GetBinaryCodec

func (s *Uint64s) GetBinaryCodec() binary.Codec

GetBinaryCodec retrieves a custom binary codec.

func (Uint64s) Len

func (s Uint64s) Len() int

func (Uint64s) Less

func (s Uint64s) Less(i, j int) bool

func (Uint64s) Swap

func (s Uint64s) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL