Skip to content

Instantly share code, notes, and snippets.

@quux00
quux00 / bytes.Buffer.go
Last active August 29, 2015 14:24
bytes.Buffer definition
type Buffer struct {
buf []byte // contents are the bytes buf[off : len(buf)]
off int // read at &buf[off], write at &buf[len(buf)]
runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.
lastRead readOp // last read operation, so that Unread* can work correctly.
}
(pprof) list varint.VarintEncode
5.89s 26.37s (flat, cum) 39.97% of Total
. . 40://
190ms 190ms 41:func VarintEncode(w io.Writer, v uint64) error {
230ms 4.35s 42: ba := [1]byte{}
410ms 410ms 43: nexp := 0
. . 44: ntot := 0
210ms 210ms 45: for (v & 0xFFFFFFFFFFFFFF80) != 0 {
280ms 280ms 46: ba[0] = byte((v & 0x7F) | 0x80)
-> 2.51s 15.19s 47: n, _ := w.Write(ba[:])
func ReadVarIntToUint(r io.Reader) (uint64, error) {
var (
varbs []byte
ba [1]byte
u uint64
n int
err error
)
varbs = make([]byte, 0, 10)
@quux00
quux00 / gist:b16238a795878248e06b
Created July 2, 2015 01:30
list-binary.Read.pprof
(pprof) list binary.Read
Total: 4.27mins
5.02s 25.50s (flat, cum) 9.96% of Total
. . 137:// When reading into a struct, all non-blank fields must be exported.
330ms 330ms 138:func Read(r io.Reader, order ByteOrder, data interface{}) error {
. . 139: // Fast path for basic types and slices.
430ms 2.85s 140: if n := intDataSize(data); n != 0 {
240ms 6.85s 141: var b [8]byte
130ms 130ms 142: var bs []byte
20ms 20ms 143: if n > len(b) {
@quux00
quux00 / write_varint_snippet.go
Last active August 29, 2015 14:24
Revised VarintEncode function
func VarintEncode(w io.Writer, v uint64) error {
bs := make([]byte, 0, 10)
for (v & 0xffffffffffffff80) != 0 {
bs = append(bs, byte((v&0x7f)|0x80))
v >>= 7
}
bs = append(bs, byte(v&0x7f))
n, err := w.Write(bs)
@quux00
quux00 / ReadVarIntToUint.pprof
Last active August 29, 2015 14:23
Go pprof listing for ReadVarIntToUint
(pprof) list varint.ReadVarIntToUint
Total: 4.27mins
ROUTINE ======================== g/q/o/o/b/varint.ReadVarIntToUint
19.20s 1.86mins (flat, cum) 43.51% of Total
370ms 370ms 60:func ReadVarIntToUint(r io.Reader) (uint64, error) {
. . 61: var (
130ms 130ms 62: varbs []byte
230ms 4.65s 63: ba [1]byte
190ms 6.51s 64: u uint64
. . 65: n int
@quux00
quux00 / testrange.go
Created June 30, 2015 02:06
Test all possible int64's for variable int encoding/decoding algorithm
type testrange struct {
start int64
end int64
}
func zigzagExhaustiveTest() {
runtime.GOMAXPROCS(8)
var wg sync.WaitGroup
package quux00.curator;
import java.util.List;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.LockInternalsDriver;
import org.apache.curator.framework.recipes.locks.PredicateResults;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
@quux00
quux00 / TryOutInterProcessMutex.java
Last active April 9, 2021 15:50
Test of ZK Curator InterProcessMutex
package quux00.curator;
import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
@quux00
quux00 / pom.xml
Created October 19, 2014 15:56
pom for JMH based string tokenization benchmarks
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mechsympathy</groupId>
<artifactId>mechsympathy</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mechsympathy</name>
<url>http://maven.apache.org</url>