Skip to content

Instantly share code, notes, and snippets.

View kjk's full-sized avatar

Krzysztof Kowalczyk kjk

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
/* This code was written by Krzysztof Kowalczyk (http://blog.kowalczyk.info)
and is placed in public domain. */
#!/usr/bin/env python
# This code was written by Krzysztof Kowalczyk (http://blog.kowalczyk.info)
# and is placed in public domain.
def v2fhelper(v, suff, version, weight):
parts = v.split(suff)
if 2 != len(parts):
return v
version[4] = weight
@kjk
kjk / test_gob.go
Created December 18, 2013 01:36
Shows that one cannot append data to a gob file - the decoder will error out with 'extra data in buffer` when trying to decode data written by append.
package main
import (
"encoding/gob"
"fmt"
"io"
"log"
"math/rand"
"os"
)
@kjk
kjk / show_caller_name.go
Last active December 31, 2015 19:39
Shows that we can get the caller's function name without access to source code. go build -o test show_caller_name.go; rm show_caller_name.go; ./test
package main
import (
"bytes"
"fmt"
"runtime"
)
var (
dunno = []byte("???")
import os
import pefile
# needs https://code.google.com/p/pefile/ to be installed
# i.e. download it, extract and python setup.py install
d = os.path.join("win", "Debug")
files = os.listdir(d)
def is_pe(s):
if s.endswith(".dll"):
/* Copyright 2014 the SumatraPDF project authors (see AUTHORS file).
License: Simplified BSD (see COPYING.BSD) */
/* NOTE: this is unfinished work in progress */
/* NoFreeAllocator (ScratchAllocator ?) is designed for quickly and easily
allocating temporary memory that doesn't outlive the stack frame
in which it was allocated.
Consider this piece of code:
@kjk
kjk / go_smaz_faster.md
Created December 11, 2014 01:33
Improving smaz benchmarking

Before:

Krzysztofs-MacBook-Pro-4:go-smaz kkowalczyk$ go test -bench=.
PASS
BenchmarkCompression	     500	   3451735 ns/op	  39.60 MB/s
BenchmarkDecompression	     500	   2740847 ns/op	  27.58 MB/s

After:

Krzysztofs-MacBook-Pro-4:go-smaz kkowalczyk$ go test -bench=.

Latency numbers every programmer should know

1 typical CPU instruction ..................   1 ns
L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs

SSD random read ........................ 150,000 ns = 150 µs

@kjk
kjk / 3.5-chromium.txt
Last active March 11, 2023 11:03
clang-format styles
kjkmacpro:sumatrapdf kjk$ clang-format -dump-config -style=Chromium
---
Language: Cpp
# BasedOnStyle: Chromium
AccessModifierOffset: -1
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
@kjk
kjk / windows.h__.js
Created October 4, 2016 00:08
windows.h.js INFINITY
var ffi = require('ffi'),
ref = require('ref'),
Struct = require('ref-struct'),
Library = require('./Library'),
Type = ref.Type,
NULL = ref.NULL,
isNull = ref.isNull;
var groups = ['libs', 'types', 'structs', 'callbacks', 'enums'];