📂 Minimize allocations in Go
A collection of tips for when you need to minimize the number of allocations in your Go programs.
Use the go profiler to identify which parts of your program are responsible for most allocations.
A collection of tips for when you need to minimize the number of allocations in your Go programs.
Use the go profiler to identify which parts of your program are responsible for most allocations.
This document lists ISA or processor implementation wishlists/ideas.
They are mostly random ideas; they are not reviewed, guaranteed to work, or even just make sense, but at some point I had good use cases for each one of them.
ISA wishlists found elsewhere.
In a project I'm working on I ran into the requirement of having some sort of persistent FIFO buffer or pipe in Linux, i.e. something file-like that could accept writes from a process and persist it to disk until a second process reads (and acknowledges) it. The persistence should be both across process restarts as well as OS restarts.
AFAICT unfortunately in the Linux world such a primitive does not exist (named pipes/FIFOs do not persist
package textproto | |
import ( | |
"net/textproto" | |
"runtime" | |
"sync" | |
) | |
// CanonincalMIMEHeaderKey is like textproto.CanonicalMIMEHeaderKey but it | |
// memoizes results to avoid repeated allocations of the same string. |
package batchgetter | |
type Getter[I, T any] interface { | |
Get(context.Context, []I) ([]T, error) | |
} | |
type BatchGetter[I, T any] struct { | |
parent Getter[I, T] | |
batchWait time.Duration | |
package maps | |
type ReadMostlyMap[K comparable, V any] struct { | |
mu sync.Mutex | |
m atomic.Pointer // map[K]V | |
} | |
func map2ptr[K comparable, V any](m map[K]V) unsafe.Pointer { | |
im := any(m) | |
return *(*unsafe.Pointer)(unsafe.Pointer(&im)) |
Suppose you have a type that needs explicit destruction to release resources:
type MyType struct {
...
}
func New() *MyType {
// acquire resources...
Verifying that I control the following Nostr public key: npub1j67s9mwffj6ue909esy4ldyhte9xheu5nh2lwed2qycqfdmfjmuq40chpa |