gopt

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 6 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 AtomicQueue

type AtomicQueue[T any] struct {
	// contains filtered or unexported fields
}

func NewAtomicQueue

func NewAtomicQueue[T any]() *AtomicQueue[T]

func (*AtomicQueue[T]) Len

func (q *AtomicQueue[T]) Len() int

func (*AtomicQueue[T]) Pop

func (q *AtomicQueue[T]) Pop() (val T, ok bool)

func (*AtomicQueue[T]) Push

func (q *AtomicQueue[T]) Push(val T)

type Bitset

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

func NewBitset

func NewBitset(size ...int) *Bitset

func (*Bitset) Append

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

func (*Bitset) At

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

func (*Bitset) Items

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

func (bs *Bitset) Len() int

func (*Bitset) Set

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

Set is used to set a bool at a specific index

type NumberConstraint

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

type RingBuffer

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

func NewRingBuffer

func NewRingBuffer[T any](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

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

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

func (*RingBuffer[T]) Cap

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

func (*RingBuffer[T]) Head

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

Head returns the oldest inserted value in the ring buffer

func (*RingBuffer[T]) Insert

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

Insert values into the ring buffer

func (*RingBuffer[T]) Len

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

func (*RingBuffer[T]) Max

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

func (*RingBuffer[T]) Min

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

func (*RingBuffer[T]) Tail

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

Tail returns the latest inserted value in the ring buffer

type Semaphore

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

func NewSemaphore

func NewSemaphore(limit int) *Semaphore

func (*Semaphore) Acquire

func (s *Semaphore) Acquire()

func (*Semaphore) Release

func (s *Semaphore) Release()

type ShardedMap

type ShardedMap[K string, V any] struct {
	// contains filtered or unexported fields
}

func NewShardedMap

func NewShardedMap[K string, V any](shards int) (sm *ShardedMap[K, V])

func (*ShardedMap[K, V]) Len

func (s *ShardedMap[K, V]) Len() int

func (*ShardedMap[K, V]) Load

func (s *ShardedMap[K, V]) Load(key K) (value V, ok bool)

func (*ShardedMap[K, V]) Store

func (s *ShardedMap[K, V]) Store(key K, value V)

Jump to

Keyboard shortcuts

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