Skip to content

Instantly share code, notes, and snippets.

var cowPool sync.Pool
func acquireCow(r io.Reader) *cow {
if c, ok := cowPool.Get().(*cow); ok {
c.r = r
@gobwas
gobwas / counter.go
Created September 29, 2017 09:53
Ref counter based on Go GC
package main
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
)
@gobwas
gobwas / stack.go
Created June 18, 2017 21:02
Stack growth test
package main
import (
"encoding/binary"
"flag"
"fmt"
"log"
"os"
"reflect"
"runtime"
@gobwas
gobwas / iface_test.go
Last active October 3, 2017 16:19
Alloc-free interface usage
// This example shows how interfaces could be used without
// pushing inner data to heap.
//
// Note that it was checked with Go 1.8.3 and may become unuseful
// in the future releases of Go.
package main
import (
"encoding/binary"
"testing"
func log2(n uint) uint {
const uintBits = 32 << (^uint(0) >> 63)
var (
seen, bit uint
m, l, r uint
)
r = uintBits
for l < r {
m = l + (r-l)/2
@gobwas
gobwas / contains.go
Created February 1, 2017 14:09
Contains uint64 sybmol
// Say you want to search 0x22 symbol in two bytes of 0x2122
// you make mask for your search as 0x2222
// then:
//
// 00100001 00100010
// ^ 00100010 00100010
// -------------------
// 00000011 00000000 00000011 00000000
// & 01111111 11111111 (cut topmost bit) & 10000000 10000000 (save restore mask)
// ------------------- -------------------
@gobwas
gobwas / timer.go
Created January 24, 2017 15:15
Timer race condition prove.
package main
import (
"flag"
"fmt"
"sync"
"time"
)
var withLock = flag.Bool("lock", false, "use lock on resets")
@gobwas
gobwas / tmux-cheatsheet.markdown
Created October 20, 2016 06:46 — forked from MohamedAlaa/tmux-cheatsheet.markdown
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@gobwas
gobwas / sort.go
Last active October 5, 2016 18:48
Sorting Problems
package main
import (
"fmt"
)
func main() {
l := []int{1, -20, 7, 6, 0, 9}
quicksort(l, 0, 6)
fmt.Println(l)
@gobwas
gobwas / combination.go
Last active September 7, 2016 14:29
Combination
func loop(v []int, r func([]int)) {
var i, j, n uint
n = uint(len(v))
a := make([]int, 0, len(v))
for ; i < (1 << n); i++ {
a = a[:0]
for j = 0; j < n; j++ {
if i&(1<<j) != 0 {
a = append(a, v[j])