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-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...
  
@CAFxX
CAFxX / gojit.go
Last active November 22, 2018 01:10
Visualize what a Golang JIT would look like
package main
import "go/jit"
func main() {
// jit.Run is a shorthand for jit.Compile, jit.Link and jit.Load
c, err := jit.Run(`
// - compiler is the same as the one used to build the executable
// the jit is running in
// - imports are allowed (but are restricted to the imports of
@CAFxX
CAFxX / batching.go
Last active April 10, 2018 22:50
Batching(Read|Writ)er
type WriteBatcher struct {
l sync.Mutex
pending bool
buf []byte
err error
}
func (w *WriteBatcher) Write(p []byte) (int, error) {
w.l.Lock()
defer w.l.Unlock()
@CAFxX
CAFxX / multitenant_cpu_fairness.md
Last active May 31, 2018 23:08
Multi-tenant CPU fairness

Multi-tenant CPU fairness

Idea

We want to:

  • make sure that CPU usage in one container can't significantly affect CPU availability in a different container, regardless if the other container is below or above its CPU shares
  • minimizing false-positive CPU overload notifications
  • maximize utilization of unused CPU resources

To this end we would like to find a scheme that progressively gives more freedom in CPU resource consumption to processes that stay below their share of CPU resources (including using more than their allocated shares) while restricting processes that use more than their share of CPU resources, especially if CPU-bound, to use only their allocation.

@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 / Makefile
Last active July 8, 2022 02:07
aw - Write whole files atomically in Linux
CFLAGS=-O2 -flto -ffunction-sections -fdata-sections -Wl,--gc-sections
CFLAGSNATIVE=-march=native -mtune=native
CFLAGSSTATIC=-static
build: aw.c
gcc -o aw aw.c $(CFLAGS)
build-static: aw.c
gcc -o aw aw.c $(CFLAGS) $(CFLAGSSTATIC)
@CAFxX
CAFxX / keybase.md
Created August 13, 2018 07:55
keybase.md

Keybase proof

I hereby claim:

  • I am cafxx on github.
  • I am cafxx (https://keybase.io/cafxx) on keybase.
  • I have a public key ASCqHgrCYkJIHf07RopDcCeYPXI3kV2St041A11PnDoGPQo

To claim this, I am signing this object:

@CAFxX
CAFxX / intern.go
Last active September 26, 2018 01:23 — forked from karlseguin/intern.go
String interning in Golang
package intern
import (
"sync"
)
type Pool struct {
sync.RWMutex
lookup map[string]string
}
name old time/op new time/op delta
BuildString_Builder/1Write_NoGrow-2 43.4ns ± 2% 43.4ns ± 1% ~ (p=0.392 n=13+13)
BuildString_Builder/3Write_NoGrow-2 163ns ± 1% 161ns ± 1% -0.77% (p=0.001 n=13+14)
BuildString_Builder/3Write_Grow-2 61.1ns ± 1% 61.7ns ± 2% +0.98% (p=0.001 n=15+15)
BuildString_ByteBuffer/1Write_NoGrow-2 75.4ns ± 2% 76.4ns ± 1% +1.24% (p=0.000 n=13+12)
BuildString_ByteBuffer/3Write_NoGrow-2 230ns ± 2% 335ns ±15% +45.70% (p=0.000 n=13+13)
BuildString_ByteBuffer/3Write_Grow-2 181ns ± 2% 247ns ±15% +36.28% (p=0.000 n=14+15)
GenericNoMatch-2 380ns ± 2% 407ns ± 6% +7.07% (p=0.000 n=15+15)
GenericMatch1-2 5.37µs ± 4% 5.28µs ± 4% -1.68% (p=0.023 n=15+15)
GenericMatch2-2 21.1µs ± 3% 20.8µs ± 2% ~ (p=0.098 n=15+15)
@CAFxX
CAFxX / partitioner.go
Last active November 15, 2018 01:09
Sarama BalancedAffinityPartitioner
package partitioner
/*
A partitioner that assigns affinity from keys to partitions, while at the same time
attempting to spread the load across partitions. Affinity, to this end, is temporary.
This means that the affinity of a key to a partition changes over time.
There are three parameters that affect how often the affinity changes:
- every timeWindow affinity changes for all keys
- when, within a timeWindow, a key produces more than switchEveryBytes bytes or
switchEveryMessages messages the affinity of that key changes