intpacker

package module
v0.0.0-...-c9c8336 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2021 License: MIT Imports: 0 Imported by: 0

README

Intpacker

Go Reference

Unfortunately I don't believe this is possible at the moment because doing addition/subtraction has the potential to overflow into the next number.


Just have a few ints to track, but don't want the overhead of channels or mutexex? Neither did I. So I decided to pack many numbers into a single number.

This is experimental.

Performance

Early indications are that it is good for avoiding mutexes and their overhead by being able to use the sync/atomic package on multiple numbers at a time.

Tested with go1.15.2 windows/amd64 on a i7-8750H CPU @ 2.20GHz, 2208 Mhz, 6 Cores, 12 Logical Processors

go test -benchmem -bench .                                                                                        
goos: windows
goarch: amd64
pkg: github.com/ograycode/intpacker
BenchmarkUint32AtomicAdd-12             118378352               10.1 ns/op             0 B/op          0 allocs/op
BenchmarkUint32WithMutex-12             63579248                17.1 ns/op             0 B/op          0 allocs/op
BenchmarkUint32AtomicAddParallel-12     45477213                24.6 ns/op             0 B/op          0 allocs/op
BenchmarkUint32WithMutexParallel-12     16742941                72.4 ns/op             0 B/op          0 allocs/op
PASS

TODO

  • More tests.
  • More benchmarks.
  • How to pack and unpack more types, like signed ints.
  • How to fully support negative numbers.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Uint32

type Uint32 struct {
	// contains filtered or unexported fields
}

Uint32 type for easy unpacking and usage with sync.atomic

func NewUint32

func NewUint32(x, y uint32) *Uint32

NewUint32 packs two uint32 into one uint64

Overflows of uint32 are not fully supported.

func (*Uint32) Ptr

func (t *Uint32) Ptr() *uint64

Ptr returns a pointer for the underlying uint64 value.

func (*Uint32) Uint64

func (t *Uint32) Uint64() uint64

Uint64 returns the uint64 underlying value.

func (*Uint32) Unpack

func (t *Uint32) Unpack() (uint32, uint32)

Unpack the value into two uint32s

Jump to

Keyboard shortcuts

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