gopt

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 4 Imported by: 0

README

go-optimizations

A collection of low-level, performance-oriented optimizations for Go focused on reducing allocations or CPU time in hot paths.

This package intentionally leverages the unsafe package and other non-idiomatic techniques where they provide measurable performance benefits. As a result, correctness, safety, and portability are the responsibility of the consumer.

If you need safety, simplicity, or long-term stability, this package is likely not appropriate.

Important Warning

This package uses unsafe extensively.

By using this library, you acknowledge that:

  • APIs may violate Go’s memory safety guarantees if misused
  • Incorrect usage can lead to undefined behavior, memory corruption, or hard-to-debug crashes
  • Behavior may change across Go versions, architectures, or GC implementations

If you are unsure whether an optimization is safe for your use case, do not use it.

Goals

  • Reduce heap allocations
  • Reduce CPU cycles in hot paths
  • Avoid unnecessary copying
  • Provide opt-in, explicit performance trade-offs
  • Keep all unsafe behavior well-documented and isolated

This is not a general-purpose utility library. Each optimization targets a specific, measurable bottleneck.

Versioning & Compatibility

  • Go version compatibility is best-effort
  • Minor Go releases may change runtime behavior and break assumptions
  • No backward-compatibility guarantees are provided

Pin the module version and re-run benchmarks and tests when upgrading Go.

Testing Recommendations

When using this package:

  • Test under load
  • Run with -race
  • Add regression tests around every call site
  • Validate behavior across Go versions
  • Prefer benchmarks over intuition

If an optimization does not produce a measurable improvement in your workload, remove it.

Documentation

Overview

Package gopt contains various optimizations for go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BytesToString

func BytesToString(b []byte) (s string)

Types

type Bitset added in v1.2.3

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

func NewBitset added in v1.2.3

func NewBitset(size ...int) *Bitset

func (*Bitset) Append added in v1.2.3

func (bs *Bitset) Append(elm ...bool)

func (*Bitset) At added in v1.2.3

func (bs *Bitset) At(idx int) bool

func (*Bitset) Items added in v1.2.3

func (bs *Bitset) Items(yield func(bool) bool)

Items is a iterator for the bitset

var bitSet BitSet
for b := range bitSet.Items {
	// handle b
}

func (*Bitset) Len added in v1.2.3

func (bs *Bitset) Len() int

func (*Bitset) Set added in v1.2.3

func (bs *Bitset) Set(idx int, state bool)

Set is used to set a bool at a specific index

type NumberConstraint added in v1.1.1

type NumberConstraint interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

type RingBuffer added in v1.1.1

type RingBuffer[T NumberConstraint] struct {
	// contains filtered or unexported fields
}

func NewRingBuffer added in v1.1.1

func NewRingBuffer[T NumberConstraint](cap int, initialValues ...T) *RingBuffer[T]

NewRingBuffer created a new ring buffer. note that the ring buffer is not thread safe

func (*RingBuffer[T]) At added in v1.1.1

func (rb *RingBuffer[T]) At(i int) T

At function to make indexing the ring buffer simpler. this is eq to slice[i]

func (*RingBuffer[T]) BackingSlice added in v1.1.1

func (rb *RingBuffer[T]) BackingSlice() []T

func (*RingBuffer[T]) Cap added in v1.1.1

func (rb *RingBuffer[T]) Cap() int

func (*RingBuffer[T]) Head added in v1.1.1

func (rb *RingBuffer[T]) Head() T

Head returns the oldest inserted value in the ring buffer

func (*RingBuffer[T]) Insert added in v1.1.1

func (rb *RingBuffer[T]) Insert(vals ...T)

Insert values into the ring buffer

func (*RingBuffer[T]) Len added in v1.1.1

func (rb *RingBuffer[T]) Len() int

func (*RingBuffer[T]) Max added in v1.2.3

func (rb *RingBuffer[T]) Max() T

func (*RingBuffer[T]) Min added in v1.2.3

func (rb *RingBuffer[T]) Min() T

func (*RingBuffer[T]) Tail added in v1.2.0

func (rb *RingBuffer[T]) Tail() T

Tail returns the latest inserted value in the ring buffer

Jump to

Keyboard shortcuts

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