Skip to content

Instantly share code, notes, and snippets.

View prydin's full-sized avatar

Pontus Rydin prydin

View GitHub Profile
package listener
import (
"encoding/hex"
"github.com/twmb/murmur3"
"unsafe"
)
func CalcTagDigest(metricName, source string, tags map[string]string) string {
a1, a2 := hashPair("metric", metricName)
@prydin
prydin / thread.txt
Created November 1, 2019 13:13
Thread dump
2019-11-01 09:09:29
Full thread dump Java HotSpot(TM) 64-Bit Server VM (10.0.1+10 mixed mode):
Threads class SMR info:
_java_thread_list=0x00007f9e1f019600, length=147, elements={
0x00007f9e1d807800, 0x00007f9e1d81f000, 0x00007f9e1e019800, 0x00007f9e20838800,
0x00007f9e1d823000, 0x00007f9e1e01a000, 0x00007f9e1e004000, 0x00007f9e1f807000,
0x00007f9e1f88d800, 0x00007f9e20805800, 0x00007f9e218e1800, 0x00007f9e2086b000,
0x00007f9e21113000, 0x00007f9e21906800, 0x00007f9e20dfd800, 0x00007f9e1e7f6000,
0x00007f9e1e7f7000, 0x00007f9e200b5800, 0x00007f9e1dd30800, 0x00007f9e21229800,
@prydin
prydin / Test.java
Last active December 19, 2019 21:03
A simple benchmark of synchronization
public class Test {
public static void main(String[] args) {
int value = 0;
long now = System.currentTimeMillis();
for(int i = 0; i < 1e8; i++) {
value++;
}
System.out.println("Unsynched version took " + (System.currentTimeMillis() - now) + "ms");
Object syncher = new Object();
now = System.currentTimeMillis();
@prydin
prydin / HashTest.java
Last active December 19, 2019 17:23
Performance test of hashmap and synch
import java.util.HashMap;
public class HashTest {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("foo", "bar");
long now = System.currentTimeMillis();
for(int i = 0; i < 1e7; i++) {
map.get("foo");
}
@prydin
prydin / DoubleCheck.java
Last active December 19, 2019 15:58
Typical double check
class DoubleCheck {
private SomeResource myResource;
public SomeResource getResource() {
if(myResource != null) {
return myResource
}
synchronized(this) {
if(myResource != null) {
return myResource
@prydin
prydin / BrokenDoubleCheck.java
Last active December 19, 2019 15:57
Bad way of doing double check
class BrokenDontUse {
private Map<String, SomeResource> aHashMap = new HashMap<>();
public SomeResource getResource(String name) {
SomeResource r = aHashMap.get(name);
if(r != null) {
return r;
}
synchronized(aHashMap) {
SomeResource r = aHashMap.get(name);
@prydin
prydin / lock.c
Created December 19, 2019 17:24
Locking pseudo code
int waiters = 0;
void acquireLock() {
if(atomicIncrement(waiters) == 0) {
rerurn;
}
callSlowAndPainfulLockingLogic();
}
@prydin
prydin / Questionable.java
Created December 19, 2019 17:26
Questionable use of Double Checj
class QuestionableUseOfDoubleCheck {
private long firstAccess = -1;
public long accessSomething() {
if (firstAccess != -1) {
return this.firstAccess;
}
synchronized (this) {
if (this.firstAccess != -1) {
return this.firstAccess;
@prydin
prydin / mandel.go
Created January 2, 2020 01:03
Simple mandelbrot generator in Go
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"math/cmplx"
"os"
@prydin
prydin / mandelthread.go
Created January 2, 2020 01:28
Parallel processing mandelbrot generator in Go
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"math/cmplx"
"os"