Skip to content

Instantly share code, notes, and snippets.

@blanchonvincent
blanchonvincent / task.go
Created November 21, 2019 17:50
Medium - Tracing
func main() {
ctx, task := trace.NewTask(context.Background(), "main start")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
r := trace.StartRegion(ctx, "reading file")
defer r.End()
@blanchonvincent
blanchonvincent / allocation.go
Last active October 31, 2019 12:42
Medium - mark and sweep
type struct1 struct {
a, b int64
c, d float64
e *struct2
}
type struct2 struct {
f, g int64
h, i float64
}
@blanchonvincent
blanchonvincent / simple_allocation.go
Last active September 18, 2019 20:20
Medium - Garbage Collector
func BenchmarkAllocationEveryMs(b *testing.B) {
// need permanent allocation to clear see when the heap double its size
var s *[]int
tmp := make([]int, 1100000, 1100000)
s = &tmp
var a *[]int
for i := 0; i < b.N; i++ {
tmp := make([]int, 10000, 10000)
a = &tmp
@blanchonvincent
blanchonvincent / main.go
Created September 14, 2019 05:55
Medium - Keep Alive
type File struct { d int }
func main() {
p := openFile("t.txt")
content := readFile(p.d)
println("Here is the content: "+content)
}
func openFile(path string) *File {
@blanchonvincent
blanchonvincent / tokenized.go
Created September 8, 2019 04:24
Medium - Compiler
package main
import (
"fmt"
"go/scanner"
"go/token"
"io/ioutil"
)
func main() {
@blanchonvincent
blanchonvincent / main.go
Last active September 6, 2019 17:18
Medium - Instrumentation statements
package main
import "math/rand"
func main() {
println(run())
}
//go:noinline
func run() int {
@blanchonvincent
blanchonvincent / main.go
Last active September 3, 2019 11:43
Medium - compiler
package main
func main() {
a := 1
b := 2
if true {
add(a, b)
}
}
@blanchonvincent
blanchonvincent / starvation.go
Created August 22, 2019 08:57
Medium - Mutex starvation
func main() {
done := make(chan bool, 1)
var mu sync.Mutex
// goroutine 1
go func() {
for {
select {
case <-done:
return
@blanchonvincent
blanchonvincent / channel.go
Created August 16, 2019 07:26
Medium - Monitor pattern - solution with channel
type Item = int
type waiter struct {
n int
c chan []Item
}
type state struct {
items []Item
wait []waiter
@blanchonvincent
blanchonvincent / empty_interface_test.go
Created August 9, 2019 18:18
Medium - Empty interface
func emptyInterface(i interface {}) {
s := i.(*MultipleFieldStructure)
y = s
}