Skip to content

Instantly share code, notes, and snippets.

View CAFxX's full-sized avatar

Carlo Alberto Ferraris CAFxX

View GitHub Profile
@CAFxX
CAFxX / golang_minimize_allocations.md
Last active April 25, 2024 19:14
Minimize allocations in Go

📂 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.

⚠️ Never apply these tricks blindly (i.e. without measuring the actual performance benefit/impact). Most of these tricks cause a tradeoff between reducing memory allocations and other aspects (including e.g. higher peak memory usage, higher CPU usage, lower maintainability, higher probability of introducing subtle bugs). Only apply these tricks if the tradeoff in every specfic case is globally positive.

Protobuf

@CAFxX
CAFxX / 00_cpu_wishlist.md
Last active March 10, 2024 12:58
CPU ISA and implementation wishlists
@CAFxX
CAFxX / go_wishlist.md
Last active January 24, 2024 00:11
Go wishlist

Language/syntax

Shorthand error definition

Instead of things like var ErrFoo = errors.New("foo") or return fmt.Errorf("foo: %d", n) I would like a shorthand syntax that allows to define a new error type.

Simple error

@CAFxX
CAFxX / persistent_pipes_linux.md
Last active January 4, 2024 04:32
Persistent pipes/circular buffers for Linux

📂 Persistent "pipes" in Linux

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

@CAFxX
CAFxX / textproto.go
Last active September 26, 2023 01:49
textproto.CanonincalMIMEHeaderKey with memoization and GC
package textproto
import (
"net/textproto"
"runtime"
"sync"
)
// CanonincalMIMEHeaderKey is like textproto.CanonicalMIMEHeaderKey but it
// memoizes results to avoid repeated allocations of the same string.
@CAFxX
CAFxX / batch_getter.go
Last active July 27, 2023 01:42
Request batcher
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))
@CAFxX
CAFxX / Colorized and formatted go tool objdump output.png
Last active July 7, 2023 17:51
Colorized and formatted go tool objdump output
Colorized and formatted go tool objdump output.png
@CAFxX
CAFxX / golang-automatic-resource-release-with-finalizers.md
Last active July 7, 2023 17:36
golang: how to automatically release resources

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