Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A log of interesting posts and ideas accumulated over time.

Code Log

A log of resources and ideas ranging in topics from programming language design and Go internals to bit twiddling and optimization.


CRAPL academic-strength open-source licence


Coq proof language

Kimball Germane provides an introduction to the Coq theorem prover.

Coq implements a dependently-typed strongly-normalizing programming language that allows users to express formal specifications of programs. Coq assists the user in finding artifacts that meet a specification and from which it can extract a certified implementation in Haskell, Racket, or OCaml automatically. This talk will iterate through a series of increasingly-precise specifications of a commonly-used function and the experience of a Coq user meeting these specifications.

Defining natural numbers in terms of successor:

Inductive nat : Set :=
  | 0 : nat
  | S : nat -> nat.

Eval compute in 0.
Eval compute in (S 0).
Eval compute in (S (S (S 0))). (* 3 *)

Proving associativity of addition using induction:

Fixpoint add (a b : nat) : nat :=
  match a with
  | 0 => b
  | S n => S (add n b)

Theorem add_assoc : forall (a b c : nat),
  (add a (add b c)) = (add (add a b) c).
intros a b c.
induction a. simpl. reflexivity.
simpl. rewrite -> IHa. reflexivity.

Projects using Coq and resources:

(* notable projects
   Verified Software Toolchain
   ceL4 and CertiKOS - verified kernels

(* Resources
   Coq Reference Manual
   Coq'Art - Interactive Theorem Proving and Program Development
   Certified Programming with Dependent Types - Alan Chlipala
   Software Foundation

I wanted to talk a little about some cool projects that are being done with Coq. Vellvm is verified LLVM, so that's what it sounds like; Verified Software Toolchain and Ynot are frameworks to reason about C programs and imperative programs; Bedrock reasoning about assembly language; there are some verified kernels that are fully verified; and CompCert is a gigantic project which aimed to have a formalized C compiler and they succeeded.

One of the researchers at the University of Utah, John Regehr - they do hardening for compilers and fuzzying for C compilers and CompCert had the fewest number of bugs and the only bugs it had were in the unverified parts, which was the front-end parser, and since then, that project has verified their front-end parser. So there's good reason to think that there are not mistakes in CompCert.

They [CompCert] define the semantics for C and the semantics for assembly and their high-level proof is that the semantics are preserved across compilation. The chain includes register allocation, so it has stuff about graph coloring - on their website there's a diagram that shows all of the phases that they've proven.

He mentions the Idris programming language as being dependently typed.

Idris is a dependently typed programming language that is getting more popular and I wonder if they would extract to Idris differently where you had to provide a proof.

Gemini Guidance Computer


Saturn Launch Vehicle Digital Computer


Memory was in the form of 13-bit syllables, each with a 14th parity bit. Instructions were one syllable in size, while data words were two syllables (26 bits).

The LVDC was highly redundant and reliable:

For reliability, the LVDC used triple-redundant logic and a voting system. The computer included three identical logic systems. Each logic system was split into a seven-stage pipeline. At each stage in the pipeline, a voting system would take a majority vote on the results, with the most popular result being passed on to the next stage in all pipelines. This meant that, for each of the seven stages, one module in any one of the three pipelines could fail, and the LVDC would still produce the correct results.

There are 18 simple instructions.

Apparently, the LVDC was hand-compiled:

Young (American) programmers just out of college were then employed to manually compile the FORTRAN program into the assembly language of the embedded LVDC CPU, and presumably to make whatever other adjustments are needed when you pass from the computing environment of a large computer to a smaller embedded system.


Backtracking regexp in Go

Doug Clark wrote a regular expression library for Go that allows backtracking and does not have the constant-time guarantees of the built-in regexp package.

GMP pi computation

GMP can be used to compute pi and is the fastest implementation of those surveyed by Nick Craig-Wood.

curl --output gmp-chudnovsky.c
gcc -s -Wall -o gmp-chudnovsky gmp-chudnovsky.c -lgmp -lm

Ubuntu on Raspberry Pi 4

The Raspberry Pi 4 changed to 64-bit, so most operating systems other than the default Raspbian distribution are not currently compatible. CloudKernels walks through their process of building a 64-bit bootable Ubuntu image for the Pi 4.


C char array and pointer


Go memory model


Profiling Go programs


Go as a compiler construction language

If self compilation was an early goal of Go, it would have been more compiler oriented - a design I would greatly appreciate.

Go turned out to be a fine language in which to implement a Go compiler, although that was not its original goal. Not being self-hosting from the beginning allowed Go's design to concentrate on its original use case, which was networked servers. Had we decided Go should compile itself early on, we might have ended up with a language targeted more for compiler construction, which is a worthy goal but not the one we had initially.

Early Go development

The debugger was originally named ogle. Old versions of the FAQ mention that "'Ogle' would be a good name for a Go debugger."

A vector container package used to exist for sequential storage with specialized versions for int and string.

The first commit after the hello world programs contains an early annotated Go spec.

In Rob Pike's Gophercon 2014 talk "Hello, Gophers!", he discusses the language inspiration and development.

TODO: read original spec

Go compiler naming

The Go compiler borrows from the Plan 9 naming scheme:

The 6g (and 8g and 5g) compiler is named in the tradition of the Plan 9 C compilers, described in (see the table in section 2). 6 is the architecture letter for amd64 (or x86-64, if you prefer), while g stands for Go.

Plan 9 compilers:

  • 0a, 1a, 2a, 5a, 7a, 8a, ka, qa, va - assemblers
  • 0c, 1c, 2c, 5c, 7c, 8c, kc, qc, vc - C compilers
  • 0l, 1l, 2l, 5l, 7l, 8l, kl, ql, vl - loaders


The Go gopher formerly named Gordon

The well known mascot of Go is called simply "the Go gopher".

However, in its early days, it was known as "Gordon the Go Gopher". This can be seen on the homepage of Glenda, the Plan 9 Bunny, in the Internet Archive, from about 2009-12-06 to 2013-04-01.

Go SSA tools


Annotated bibliography of 106 papers relating to SSA form:

Plan 9 applications


Plan 9 even has a file system filter rot13fs.c to transform traffic with ROT13.

Functional higher-order functions in Go

Using higher-order functions borrowed from functional languages in Go such as apply, filter, and reduce is considered an anti-pattern and for loops are instead preferable.

Redis persistance using fork

RDB maximizes Redis performances since the only work the Redis parent process needs to do in order to persist is forking a child that will do all the rest. The parent instance will never perform disk I/O or alike.


RDB needs to fork() often in order to persist on disk using a child process. Fork() can be time consuming if the dataset is big, and may result in Redis to stop serving clients for some millisecond or even for one second if the dataset is very big and the CPU performance not great. AOF also needs to fork() but you can tune how often you want to rewrite your logs without any trade-off on durability.


Whenever Redis needs to dump the dataset to disk, this is what happens:

  • Redis forks. We now have a child and a parent process.
  • The child starts to write the dataset to a temporary RDB file.
  • When the child is done writing the new RDB file, it replaces the old one.

This method allows Redis to benefit from copy-on-write semantics.


Log rewriting uses the same copy-on-write trick already in use for snapshotting. This is how it works:

  • Redis forks, so now we have a child and a parent process.
  • The child starts writing the new AOF in a temporary file.
  • The parent accumulates all the new changes in an in-memory buffer (but at the same time it writes the new changes in the old append-only file, so if the rewriting fails, we are safe).
  • When the child is done rewriting the file, the parent gets a signal, and appends the in-memory buffer at the end of the file generated by the child.
  • Profit! Now Redis atomically renames the old file into the new one, and starts appending new data into the new file.

Forking in threaded applications



Binary combinatory logic esoteric lang

Binary combinatory logic is a formulation of combinatory logic using only the symbols 0 and 1.

<term> ::= 00 | 01 | 1 <term> <term>
  • 00 represents the K operator.
  • 01 represents the S operator.
  • 1 <term> <term> represents the application operator (<term1> <term2>).

GrammaTech CodeSonar and CodeSurfer



Local development certificate generation


GitHub public keys vulnerable

GitHub public keys are available for anyone to access at the URL Unless configured otherwise, ssh sends all public keys until one works. By storing all GitHub keys, a server can identify the client by their key.


macOS Catalina default shell is zsh

Starting in macOS Catalina, the default shell will be zsh. The version of bash used by macOS is stuck on 3.2 because newer versions newer versions are licensed under GPL v3.

Scripting OS X gives an in depth walkthrough on migrating to zsh:


Stack-based language compilation

"Compilation of Stack-Based Languages (Abschlußbericht)" by M. Anton Ertl and Christian Pirker (1998) describes techniques for compiling stack-based languages.

RAFTS is a framework for applying state of the art compiler technology to the compilation of stack-based languages like Forth and Postscript. The special needs of stack-based languages are an efficient stack representation, fast procedure calls, and fast compilation. RAFTS addresses the stack representation problem by allocating stack items to registers such that most stack accesses in the source program are register accesses in the machine language program, and by eliminating most stack pointer updates. To achieve fast calls, RAFTS performs these optimizations interprocedurally and also performs procedure inlining and tail call optimization. Fast compilation is achieved by selecting fast algorithms and implementing them efficiently.


The basic block code generation reduces the number of stack pointer updates to at most one per stack and basic block. It is possible to reduce the number much more. E.g., in procedures where all stack items are allocated to registers, no stack pointer update is needed at all. Like register allocation, stack pointer update minimization has to be performed interprocedurally to achieve a significant effect.


Chrome offline dinosaur source

The source for the offline dinosaur game in Chrome is available in Chromium and written in JavaScript.

Project idea: extract the dinosaur game into a standalone page.

The T-Rex appears to be named "Stan the Offline Dino" as referenced in several test files:

<head><title>Welcome to Stan the Offline Dino's Homepage</title></head>

Wikipedia documents the evolution of the dinosaur game:

If the user tries to browse when offline, a message is shown that they are not connected to the Internet. An illustration of the "Lonely T-Rex" dinosaur is shown at the top, designed by Sebastien Gabriel. From September 2014, tapping the dinosaur (in Android or iOS) or pressing space or ↑ (on desktop) launches a browser game called "T-Rex Runner" in which the player controls a running dinosaur by tapping the screen (in Android or iOS) or pressing space, ↑ or ↓ (on desktop) to avoid obstacles, including cacti and, from June 2015, pterodactyls. In 2016, another feature was added to the game. When the player reaches 700 points the game begins to switch between day (white background, black lines and shapes) and night (black background, white lines and shapes). During September 2018, for Google Chrome's 10th birthday, a birthday cake causing the dinosaur to wear a birthday hat when collected was added. Reaching a score of 900 will switch the colour scheme back to day, and the switch back and forth will occur at further subsequent milestones. The game is also available at the chrome://network-error/-106 and chrome://dino pages. The game's code is available on the Chromium site.

Conway's game of life


Query language research

BYU computer science professor Kimball Germane, specializes in programming languages and his current research is in utilizing the SQLite VM byte code to construct DSL query languages that are more expressive than allowed through SQL.


Google Easter eggs

Wikipedia lists many Easter eggs hidden in Google search. Included is a selection of those queries:

  • "<blink>", "blink tag", or "blink html" includes samples of the blink element in the results.
  • "conway's game of life" on a desktop browser generates a running configuration of the game to the right of the search results. The process can also be stopped and altered by the user.
  • "google in 1998" on a desktop browser will generate a layout similar to the one Google used for its search engine in 1998.
  • "is google down" returns with "No".
  • "kerning" will add spaces between the letters of the word "kerning" in the search results.
  • "keming" will remove spaces between the letters of the word "keming".
  • "<marquee>", "marquee tag", or "marquee html" will apply the marquee element to the results count at the top of the results.
  • "minesweeper" will have a playable game of minesweeper. Users can select between three modes: easy, medium and hard.
  • "pac-man", "google pacman" or "play pacman" will show the Pac-Man related interactive Google Doodle from 2010. Clicking Insert Coin twice will enable a second player, Ms. Pac-Man.
  • "pluto" describes Pluto as "Our favorite dwarf planet since 2006" in the Knowledge Graph.
  • "recursion" includes a "Did you mean: recursion" link back to the same page.
  • "text adventure" or "google easter eggs" using most popular modern browsers (except Safari) and opening the browser's developer console will trigger a text-based adventure game playable within the console.
  • "tic tac toe" will show a playable game of tic-tac-toe. Users can select to play against the browser at different levels - "easy", "medium" or "hard" (called "impossible") - or against a friend. An alternative way to find the game is to search "shall we play a game".


C main signatures

A common extension to C supported by Unix systems adds a third parameter for environment information.

int main(void)
int main(int argc, char *argv[])
int main(int argc, char *argv[], char *envp[])

Alternatively, the environment is available in <unistd.h> with extern char **environ;.

In C, a function without parameters accepts any number of arguments, so int main() accepts any arguments, whereas int main(void) accepts none. C++ treats those two forms identically.


Aheui - esoteric language in Hangul

Aheui is the first esolang designed for Hangul, the Korean writing system.

The language specification provides an introduction into Korean orthography and lists the function of each vowel and consonant.

Interpreters are implemented in a dozen languages, including Go and self interpreted in Aheui:

AVIS is a cell based editor for Aheui:

Aheui in a polyglot:


μ6 esoteric language

μ6 is a low-level esolang based on μ-recursive functions.

Google Chrome history database

Google Chrome history is stored locally as a SQLite3 database and can be easily exported.

cd ~/Library/Application\ Support/Google/Chrome/Default/
sqlite3 History

sqlite> .headers on
sqlite> .mode csv
sqlite> .output my-chrome-output.csv

sqlite> SELECT DATETIME(last_visit_time/1000000-11644473600, 'unixepoch', 'localtime'), url
        FROM urls
        ORDER BY last_visit_time DESC;


X11 for macOS

The XQuartz open source project is a version of the X11 windowing system for macOS.

APOD Automator script

Automator in macOS can be used to automatically download the current NASA Astronomy Picture of the Day and set it as the desktop background.

Go sync.Map

Map is like a Go map[interface{}]interface{} but is safe for concurrent use by multiple goroutines without additional locking or coordination. Loads, stores, and deletes run in amortized constant-time.

The Map type is specialized. Most code should use a plain Go map instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases:

  1. when the entry for a given key is only ever written once but read many times, as in caches that only grow, or
  2. when multiple goroutines read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Go map paired with a separate Mutex or RWMutex.

Go sync.Once

Once is an object that will perform exactly one action.

Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words, given var once Once, if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute.

Used by crypto/elliptic:

Go types package

Package go/types declares the data types and implements the algorithms for type-checking of Go packages.

Alan Donovan provides a detailed tutorial on the use of the package:

Info has maps to store the relationship between identifiers and objects. Only non-nil maps in Info are populated, letting API clients control the information needed from the type checker. The field Defs records declaring identifiers and Uses records referring identifiers.

type Info struct {
  Defs       map[*ast.Ident]Object
  Uses       map[*ast.Ident]Object
  Implicits  map[ast.Node]Object
  Selections map[*ast.SelectorExpr]*Selection
  Scopes     map[ast.Node]*Scope

TODO: Continue reading at

Referring page:

gosec - Go security checker

Gosec inspects source code for security problems by scanning the Go AST.

Floating point math associativity

GCC optimizes pow(a, 2) into a*a, but does not optimize pow(a, 6) or a*a*a*a*a*a into (a*a*a)*(a*a*a) because floating point math is not associative, though associativity and other optimizations can be enabled with compiler flags.

C++ compiler division by zero optimization

The C++ compiler does not throw a division by zero exception when d == 0.

int d = 0;
d /= d;

C++ does not have a "Division by Zero" Exception to catch. The behavior you're observing is the result of Compiler optimizations:

  1. The compiler assumes Undefined Behavior doesn't happen
  2. Division by Zero in C++ is undefined behavior
  3. Therefore, code which can cause a Division by Zero is presumed to not do so.
    • And, code which must cause a Division by Zero is presumed to never happen
  4. Therefore, the compiler deduces that because Undefined Behavior doesn't happen, then the conditions for Undefined Behavior in this code (d == 0) must not happen
  5. Therefore, d / d must always equal 1.


Go language proverbs

Rob Pike philosophizes at Gopherfest SV 2015 and provides the following proverbs for teaching or understanding Go:

  • Don't communicate by sharing memory, share memory by communicating.
  • Concurrency is not parallelism.
  • Channels orchestrate; mutexes serialize.
  • The bigger the interface, the weaker the abstraction.
  • Make the zero value useful.
  • interface{} says nothing.
  • Gofmt's style is no one's favorite, yet gofmt is everyone's favorite.
  • A little copying is better than a little dependency.
  • Syscall must always be guarded with build tags.
  • Cgo must always be guarded with build tags.
  • Cgo is not Go.
  • With the unsafe package there are no guarantees.
  • Clear is better than clever.
  • Reflection is never clear.
  • Errors are values.
  • Don't just check errors, handle them gracefully.
  • Design the architecture, name the components, document the details.
  • Documentation is for users.
  • Don't panic.

XKCD surveys



Yorick experimentation

Matrices in Yorick are column-major, so to transpose a column vector to a row vector, we increase the dimensionality of the matrix using a "-" pseudo-index.

> u=[1,2,3,4]
> u
> u(-,)

Outer product of column vectors uv = uvᵀ:

> u=[1,2,3,4]
> v=[1,10,100]
> v(-,)
> u*v(-,)
> transpose(u*v(-,))

Inner product (dot product), defined as ⟨u, v⟩ = uv, is represented in Yorick using the "+" sign. The plus sign selects "the dimension to be iterated over in the summation of the inner product."

> u=[1,2,3]
> v=[4,5,6]
> u(+)*v(+)

Matrix multiplication is composed of dot products at each position and is thus represented using the plus sign. The transpose matches "normal" matrix multiplication since Yorick is column-major.

> a=[[1,2],[3,4]]
> b=[[5,6],[7,8]]
> a(+,)*b(,+)
> transpose(a(+,)*b(,+))

The original NumPy authors were familiar with Yorick and borrowed the concept of broadcasting from Yorick.

JEH-Tech explains Yorick with fantastic diagrams.


Go terminal package

Package crypto/ssh/terminal provides support functions for dealing with terminals, as commonly found on UNIX systems.

Yorick syntax and building

Yorick has optional semicolons to enable easier to type statements into the terminal. When omitted, the lexer must insert semicolons so that the parser works correctly which complicates the grammar significantly. This context sensitivity is called a "lexical tie-in" and is discouraged.

Yorick can be built from source from its repository.

Simple hello world:

> print, "Hello, World!"
"Hello, World!"

Go astutil package

Package astutil contains common utilities for working with the Go AST.


Split a subdirectory into a separate repo

A subdirectory can be split into a separate repo, the inverse of a repo merge. Any history existing outside of that subdirectory will not appear in the split repo. This causes problems if that folder has been moved.

git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME BRANCH-NAME


VSCode TextMate grammar performance



Go Native Client support

Russ Cox describes in detail the process of implementing support for Native Client (NaCl) in Go 1.3 and the architecture restrictions that added complexity.

Go 1.13 is the last release that will run on NaCl.

Comparing binaries and source code


Control flow graph function matching

Joxean Koret proposes a heuristic based on the idea that "different basic blocks and edges are different interesting pieces of information". The Koret-Karamitas algorithm "КОКА" gets features at function, basic block, edge, and instruction level, assigns a different prime value to each different feature, and then generates a hash of the product.

Huku classifies basic blocks in 7 categories: normal, entry points, exit points, traps, self-loops, loop heads and loop tails. In the same way, he classifies 4 different kinds of edges: basis, forward, back edges and cross-links.

Huku uses instruction histograms to classify instructions in 4 categories based on their functionality: arithmetic, logic, data transfer, and redirection.

JCry - a ransomware written in Go

JCry is downloaded as a fake update to Adobe Flash Player through a compromised website. It drops encryption and decryption programs into Startup, then encrypts the 1MB of all files with significant extensions. It then demands payment for a decryption key through an onion link in a Tor browser.

Raspberry Pi arcade

Recently while in San Francisco, I stumbled upon a Raiden II arcade machine in Musée Mécanique. As a child, one of my favorite games was Raiden X, a Flash spinoff of the Raiden series, so it was fun to play the original game.

Project idea: create a dedicated arcade machine using a Raspberry Pi, monitor, joystick, and buttons to play arcade games like the Raiden series, Pac Man, or Dig Dug or Flash games like Raiden X. Software like MAME (Multiple Arcade Machine Emulator) exists for arcade games, so those would be simple, but the Flash format poses issues because support has been largely dropped because the runtime has security issues and the PC controls would need to be mapped to a joystick and buttons.

Origins of the Raspberry Pi

The Raspberry Pi was developed to introduce more people to programming and at a low cost. The cost of $35 was a goal early on and drove many design decisions. Later once produced in bulk, upgrades could be made while staying within the price range.

Transposing an 8x8 bit matrix

"Hacker's Delight", Chapter 7-3

This procedure treats the 8×8-bit matrix as 16 2×2-bit matrices and transposes each of the 16 2×2-bit matrices. The matrix is then treated as four 2×2 sub-matrices whose elements are 2×2-bit matrices and each of the four 2×2 sub-matrices are transposed. Finally, the matrix is treated as a 2×2 matrix whose elements are 4×4-bit matrices and the 2×2 matrix is transposed.

unsigned long long x;
x = x & 0xAA55AA55AA55AA55LL |
  (x & 0x00AA00AA00AA00AALL) << 7 |
  (x >> 7) & 0x00AA00AA00AA00AALL;
x = x & 0xCCCC3333CCCC3333LL |
  (x & 0x0000CCCC0000CCCCLL) << 14 |
  (x >> 14) & 0x0000CCCC0000CCCCLL;
x = x & 0xF0F0F0F00F0F0F0FLL |
  (x & 0x00000000F0F0F0F0LL) << 28 |
  (x >> 28) & 0x00000000F0F0F0F0LL;


Geohash in Go assembly


Find nth set bit



Efficient integer square root algorithm



Constant-time bits

Go version 1.13 guarantees execution time of Add, Sub, Mul, RotateLeft, and ReverseBytes in package math/bits to be independent of the inputs.

CL 170758:

// Variable time
func Add64(x, y, carry uint64) (sum, carryOut uint64) {
  yc := y + carry
  sum = x + yc
  if sum < x || yc < y {
    carryOut = 1
// Constant time
func Add64(x, y, carry uint64) (sum, carryOut uint64) {
  sum = x + y + carry
  carryOut = ((x & y) | ((x | y) &^ sum)) >> 63

Go crypto/subtle

Package subtle implements functions that are often useful in cryptographic code but require careful thought to use correctly such as constant-time comparisons or copies.

func ConstantTimeByteEq(x, y uint8) int
func ConstantTimeCompare(x, y []byte) int
func ConstantTimeCopy(v int, x, y []byte)
func ConstantTimeEq(x, y int32) int
func ConstantTimeLessOrEq(x, y int) int
func ConstantTimeSelect(v, x, y int) int


Go regression testing

Nearly every fixed bug or issue has an associated test created in the test/fixedbugs directory to prevent regressions. Each test is tagged with a comment on the first line indicating the mode of testing: run, compile, errorcheck, or build. If a test has an associated directory, it becomes rundir, compiledir, etc. The level of automation and thorough nature of these tests is impressive.

Go objdump

objdump disassembles executable files in Go's Plan 9 assembly syntax.


Go context concurrency pattern


Communicating sequential processes


Less is exponentially more


Retrospective on early Go development


Go interface implementation


Go design philosophy


"Go at Google: Language Design in the Service of Software Engineering"

Go grammar is mostly regular:

Compared to other languages in the C family, its grammar is modest in size, with only 25 keywords (C99 has 37; C++11 has 84; the numbers continue to grow). More important, the grammar is regular and therefore easy to parse (mostly; there are a couple of quirks we might have fixed but didn't discover early enough). Unlike C and Java and especially C++, Go can be parsed without type information or a symbol table; there is no type-specific context.

TODO: Expand on CSP and arena allocator.


Go 1.13

Selected features to be added in Go 1.13:

  • More number literal prefixes are supported.
  • The restriction that a shift count must be unsigned is removed.
  • math/bits: The execution time of Add, Sub, Mul, RotateLeft, and ReverseBytes is now guaranteed to be independent of the inputs.


Semantics of unary plus and minus


Go proposal for 128 bit integers


Go runtime errors

Runtime errors are distinguished by the no-op function RuntimeError() in the runtime.Error interface.

Google Go team and monorepos



Go experimental subpackages

utf8string provides an efficient way to index strings by rune rather than by byte.

apidiff determines whether two versions of the same package are compatible. sumdb/gosumcheck checks a go.sum file against a go.sum database server.

shiny/materialdesign provides named colors and icons specified by Material Design.

shiny/screen and shiny/driver provide interfaces and drivers for accessing a screen.

Red-black trees in functional languages

Chris Okasaki demonstrated that red-black trees can be efficiently and elegantly implemented in functional languages. He simplifies insert to have four unbalanced cases and one balanced case.

Red-black trees have become one of the most common persistent data structures in functional languages.–black_tree

Go reusable containers

  • container/ring provides a circular linked list.
  • container/heap provides a heap interface and functions to operate on the heap.
  • container/list provides a doubly linked list.

2019-08 (undated)

Git repository merging



Go hex dump

Dumper in encoding/hex writes a hex dump in the format of hexdump -C.


Go test helpers

t.Helper() can be called to mark the caller as a test helper function and skips printing file and line information for that function.

encoding/asci85 has a clever Errorf and comparison wrapper:

testEqual(t, "Encode(%q) = %q, want %q", p.decoded, strip85(string(buf)), strip85(p.encoded))

func testEqual(t *testing.T, msg string, args ...interface{}) bool {
  if args[len(args)-2] != args[len(args)-1] {
    t.Errorf(msg, args...)
    return false
  return true

Go binary.Varint

Unsigned integers are serialized 7 bytes at a time, starting with the least significant bits. The most significant bit indicates if there is a continuation byte.

Go bounds check elimination

Issue #14808 provides a list of bound check eliminations used or not used by Go compiler including the following:

var a[]int
use a[0], a[1], a[2] // three bound checks
// can be improved as
_ = a[3]             // early bounds check
use a[0], a[1], a[3] // no bound checks
// or
a = a[:3:len(a)]     // early bound check
use a[0], a[1], a[3] // no bound checks
// or
use a[3], a[2], a[1] // one bound check

Bounds check hints in the wild in binary.LittleEndian and binary.BigEndian:

func (littleEndian) Uint16(b []byte) uint16 {
  _ = b[1] // bounds check hint to compiler; see
  return uint16(b[0]) | uint16(b[1])<<8
func (littleEndian) PutUint16(b []byte, v uint16) {
  _ = b[1] // early bounds check to guarantee safety of writes below
  b[0] = byte(v)
  b[1] = byte(v >> 8)

Grid processing algorithms

Matrices using image processing algorithms to group coordinates in grid:

A max-heap can be used to order rectangles in grid by size:


Go json.RawMessage

RawMessage is a raw encoded JSON value implementing Marshaler and Unmarshaler used to delay decoding or precompute an encoding.

In the wild:

type clientResponse struct {
  Id     uint64           `json:"id"`
  Result *json.RawMessage `json:"result"`
  Error  interface{}      `json:"error"`

Example from json docs:

// use a precomputed JSON during marshal
h := json.RawMessage(`{"precomputed": true}`)
c := struct {
  Header *json.RawMessage `json:"header"`
  Body   string           `json:"body"`
}{Header: &h, Body: "Hello Gophers!"}
// delay parsing part of a JSON message
type Color struct {
  Space string
  Point json.RawMessage // delay parsing until we know the color space
type RGB struct {
  R, G, B uint8
var c Color
err := json.Unmarshal(`{"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}}`, &c)
var dst interface{}
switch c.Space {
  case "RGB":
  dst = new(RGB)
err = json.Unmarshal(c.Point, dst)


QLOCKTWO text-based clock

While visiting Zürich, Switzerland, I saw a store selling text-based clocks made by QLOCK2. The clocks have a grid of letters that light up to spell out the time.

For example, in 5:28 would be rounded to 5:30 and displayed in German as "ES IST HALB FÜNF".


In English, it would be "IT IS HALF PAST FIVE".


Project idea: develop a web browser extension to replace the new tab page with a more aesthetically pleasing page including a QLOCKTWO-style clock and artistic backgrounds. The search bar is redundant with the address bar and need not be included. Depending on the level of minimalism desired, feeds of the user's favorite websites could be displayed.


Apollo mission streams



Fast addition and subtraction


Constant-time cryptography

When writing constant-time code, timing should not depend on secret information.

Secret information may only be used in an input to an instruction if that input has no impact on what resources will be used and for how long. ... Today’s languages and compilers weren’t really built for this, so it's a challenge. ... The compiler might decide that your code would be faster if it used variable-time instructions. There are even cases where an optimizing compiler will see that you are trying to, say, avoid using an if statement, and the compiler puts the if statement back in because it knows it will be faster.

Code can be verified to be constant-time using a patch to Valgrind made by Adam Langley:

BigInt in ES2020

  • BigInt is a numeric primitive for arbitrary precision integers introduced in ES2020:
  • BigInt has its own type and can be defined with a n suffix (typeof 42n === 'bigint').
  • A BigInt is not strictly equal to a Number (===), but is abstractly equal (==).
  • When coerced to a boolean, BigInt follows the same logic as Number.
  • Binary +, -, *, and ** all work. / and % work, rounding towards zero.
  • Bitwise operations |, &, <<, >>, and ^ assume a two's complement representation for negative values.
  • Unary - negates, though unary + is not supported because asm.js expects +x to produce either a Number or an exception.
  • Unsigned right shift >>> is unsupported because BigInt is always signed.
  • BigInt64Array and BigUint64Array, make it easier to efficiently represent lists of 64-bit signed and unsigned integers.

Detecting signals in Go

src-d/go-git intercepts signals to exit cleanly from Git calls using os/signal and context.

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)



Library for performing set operations on 1-dimensional intervals, such as time ranges:


Tabletop Whale


Uber Go libraries




GNU Multiple Precision Arithmetic Library


Bit twiddling reference



Interstellar film script differences

IMSDb has a draft script from March 12, 2008 for Interstellar that drastically different from the final film version. In it, Murph is a boy and the Chinese passed through the wormhole long before NASA and figured out how to manipulate gravity.

Project idea: make a tool to format film scripts to be more pleasant to read. Scripts on IMSDb are consistently formatted, albeit with some inaccuracies, so could be mapped to another format.


Send channels and receive channels in Go

There are three types of channels: bidirectional chan, receive-only <-chan, and send-only chan<-. A bidirectional channel can be casted to either receive-only or send-only, but cannot be converted back.

First seen in the Go syntax definitions in GitHub's Semantic project:



Elm is a pure functional UI design DSL with strong static type checking and "no runtime exceptions in practice".

XMLisp - Lisp with XML syntax

Project idea: implement a Lisp-like language with implicit returns, higher order functions, and expressions as values. This would be a more capable successor to XMLang that would introduce type safety and would parse with encoding/xml rather than JSX.

<func name="fib" params="n int">
    <eq>n 0</eq>
      <eq>n 1</eq>
        <fib><sub>n 1</sub></fib>
        <fib><sub>n 2</sub></fib>

Go crypto/rand

crypto/rand operates on *big.Int, unlike math/rand.

rand.Reader is a global, shared instance of a cryptographically secure random number generator that reads from OS-specific APIs.


Ahead-of-time and just-in-time compilation


μ-recursive function


Whitespace is supposedly based on μ-recursive functions:

HaPyLi programming language

HaPyLi is a programming language designed to compile to Whitespace, with syntax derived from Haskell, Python, and Lisp. HaPyLi uses the Whitespace heap to store strings and globals. It supports inline Whitespace, but requires that all arguments and local variables be popped and exactly one value be pushed. The standard library includes alloc, similar to malloc in C, but there is no corresponding free implementation.

import "stdlib/base.hpl"

def power(x y) =
    (if (== y 1)
        (* x (power x (- y 1))))

def main() = (print-number (power 2 10))

Unfortunately, as the homepage is defunct, the compiler source is no longer available.

Marinus Oosters created a 99 bottles of beer program written in HaPyLi:

While developing HaPyLi, the author posted a question on Haskell monads during code generation:


Go math/bits proposal

All bit twiddling functions, except popcnt, are already implemented by runtime/internal/sys and receive special support from the compiler in order to "to help get the very best performance". However, the compiler support is limited to the runtime package and other Golang users have to reimplement the slower variant of these functions.

Go 1.13 signed shift counts

In Go 1.13, shift counts (<< and >>) are no longer required to be unsigned and when negative, a panic occurs.

This requires an estimated minimum of two extra instructions per non-constant shift: a test and a branch to be check at run-time, as done for make. The compiler can omit the check for unsigned and constant values and when it is able to prove that the operand is non-negative.

As a last resort, an explicit uint conversion or mask in the source code will allow programmers to force the removal of the check, just as an explicit mask of the shift count today avoids the oversize shiftcheck.


Assembly performing slower than high level programs

Go blank identifier uses

Disable unused declaration error:

_ = unused

To import a package solely for its side-effects (initialization), use the blank identifier as explicit package name:

import _ "lib/math"

Static type assertion:

type T struct{}
var _ I = T{}       // Verify that T implements I.
var _ I = (*T)(nil) // Verify that *T implements I.

Interspersing delimiters without branching

The Go Programming Language:

var s, sep string
for i := 1; i < len(os.Args); i++ {
  s += sep + os.Args[i]
  sep = " "

Semantic source code library by GitHub

Yorick programming language

"Yorick is an interpreted programming language for scientific simulations or calculations, postprocessing or steering large simulation codes, interactive scientific graphics, and reading, writing, or translating large files of numbers."

"Arrays are first-class objects that can be operated on with a single operation. Since the virtual machine understands arrays, it can apply optimized compiled subroutines to array operations, eliminating the speed penalty of the interpreter."

"Yorick is good at manipulating elements in N-dimensional arrays conveniently with its powerful syntax."

I was referred to Yorick by Matt Borthwick as it is his favorite programming language for physics. A trick to compute the product of the elements of an array and avoid overflows is to take the exponentiation of the sum of the natural logs of the elements: exp(sum(ln(arr))).


Go unnamed method receiver

A method can have a receiver without a name.

func (CmdReceivePack) Usage() string


Go embedded struct fields

Embedded struct fields have no name and promote fields and methods to another struct.

Discovered in encoding/json/encode.go:

type jsonError struct{ error }
type A struct {
  foo int
type B struct {
  bar int
b := B{A{10}, 3}
//,, b.A are accessible


Git branch cleanup tool

Project idea: after a PR has been merged and the branch deleted on the remote, any local clones of this branch remain and should be deleted.


  • Scan repo(s) for all branches
  • Exclude dev, master, currently checked out branch, and branches with an open PR
  • Delete all merged branches


Go sync.Pool


Password crossword

The 2013 Adobe breach has the credentials of millions of users and the passwords are encrypted insecurely using Triple DES.

In an XKCD comic, Randall Munroe creates a crossword puzzle of solving the password blocks using the given password hints.

Using statistics of the most common passwords, one could provide a word bank common passwords that could be used while solving such a crossword puzzle.

Go reflection to view and set unexported fields

Reflection is designed to allow any field to be accessed, but outside of the definition package, only exported fields can be modified (The Go Programming Language, Donovan and Kernighan).

However, using unsafe.Pointer and (*reflect.Value).UnsafeAddr, unexported values can be assigned to, though doing so potentially interferes with garbage collection.

Deleting value in Go map

An element can be deleted from a map using delete(m, key) similar to delete m[key] in Javascript.

Go modules synchronizer

When working in a project using modules, each package and sub-package requires specific dependency versions which protects a package from breaking changes in its dependencies, but it makes changing code in multiple packages simultaneously difficult.

Project idea: a tool that watches locally changed packages and updates interdependencies would greatly simplify this process.


Go runtime package

Operations to interact with runtime system and low-level reflect type information.

Discovered from attempt to print line numbers in error messages.


Text normalization

  • Text normalization in Go:
  • Detailing on NFC, NFD, NFKC, and NFKD methods of transforming text:
  • Normalizing to NFC compacts text, giving substantial savings to languages like Korean
    • Project idea: Make a keyboard with Hangul input that converts to NFC as you type, but allows for deletion by character rather than by block
  • Allows for normalization of look-alikes


Goto in Python

In Python, an April Fools joke added goto, label, and comefrom.

Discovered from a comparison of throw to comefrom in favor of Go's error handling decisions.

Git annotations for ls

There are answers here, but they don't appear to be efficient:

Note the branch annotations at the end:

drwxr-xr-x 1 0018121 Domain Users    0 Dec 14 14:33 MyProject/ (develop)
drwxr-xr-x 1 0018121 Domain Users    0 Dec 14 14:17 Data/
drwxr-xr-x 1 0018121 Domain Users    0 Dec 14 12:08 MyApp/ (master)
-rw-r--r-- 1 0018121 Domain Users 399K Aug  4 10:41 readme.txt

Git checkout shortcut

  1. Scan for git directories:
  2. List all branches for each repo and create aliases for each


Assembly in Go

Guide to the assembly used in Go:

The assembler's parser treats period and slash as punctuation, so the assembler allows the middle dot character U+00B7 and the division slash U+2215 in identifiers and rewrites them to plain period and slash. For example, fmt.Printf and math/rand.Int are rewritten to fmt·Printf and math∕rand·Int.





Code Search

Google Code Search performs regular expression matching with a trigram index.

Binary RegExp


Continuation-passing style

Functions in CPS take an extra argument, the continuation, a function of one argument. The result is returned by calling the continuation function with this value.

Procedure returns are calls to a continuation, intermediate values are all given names, argument evaluation order is made explicit, and tail calls call a procedure with the same continuation.

Functional and logic compilers often use CPS as an intermediate representation, whereas imperative or procedural compilers would use static single assignment form (SSA).

; Direct style
(define (pyth x y)
 (sqrt (+ (* x x) (* y y))))

; Continuation-passing style
(define (pyth& x y k)
 (*& x x (lambda (x2)
          (*& y y (lambda (y2)
                   (+& x2 y2 (lambda (x2py2)
                              (sqrt& x2py2 k))))))))

Go math/bits package

Arithmetic functions: add/sub with carry and mul/div with remainder. Bit manipulation: leading/trailing zeros count, bit count, one count, reverse, and rotate.


Git autocomplete

Git autocompletion can be installed by downloading the following file and sourcing in profile.

curl -o ~/.git-completion.bash



Git integration with Go

go-git is a highly extensible git implementation library written in pure Go.


Datalog Disassembly



Static single assignment form

In compiler design, SSA is a property of an intermediate representation, which requires that each variable is assigned exactly once, and every variable is defined before it is used.

Variables in the are split into versions so that every definition gets its own version.

y := 1
y := 2
x := y

Rewritten in SSA:

y1 := 1
y2 := 2
x1 := y2

Go loop bound reevaluation

Loop bound expressions can be optimized in some cases to be evaluated once:

Go 1.7 switched to using SSA for the compiler which generates more compact, more efficient code and provides a better platform for optimizations such as bounds check elimination.


Unicode property trie lookup


2019-05 (undated)

W3C CSS Color Module Level 4 changes


EDI Parsing




Powerline formats the shell prompt and vim status line into great looking segments. It uses patched fonts like FiraCode to render custom Unicode glyphs. Powerline Gitstatus is a segment for showing the status of a Git working copy.


Unlambda, AsciiDots, and Ook esoteric languages

Unlambda is a minimal functional language based on combinatory logic. It is the first functional Turing tarpit:

AsciiDots executes using dots travelling along ascii art paths taking inspiration from electrical engineering:

Ook! is a simple mapping of Brainf*** instructions to trinary combinations of Ook., Ook?, and Ook!:!




Customizing Windows command prompt



GHIDRA - NSA reverse engineering and decompilation tool


2018-12 (undated)

Trie data structure



Countering Trusting Trust

Originally written as email on 2019-09-25 to Dr. Peter Aldous following up with conversation from 2019-09-24:

I did some more research into Reflections on Trusting Trust by Thompson and came across this dissertation "Fully Countering Trusting Trust through Diverse Double-Compiling" by David A. Wheeler that counters such an attack.

Essentially, he proves that a questionable compiler can be verified by comparing a trusted compiler, compiled from source, then compiled by itself with the result of a questionable compiler executable compiling that first trusted compiler's source twice.

Below is a summary of his approach and a link to a more complete summary. Wheeler's site explains some details that the summary glosses over, but the dissertation itself if 199 pages long, so I haven't read that.

Suppose we have two completely independent compilers: A and T. More specifically, we have source code SA of compiler A, and executable code EA and ET. We want to determine if the binary of compiler A - EA - contains this trusting trust attack.

Here's Wheeler's trick:

  • Step 1: Compile SA with EA, yielding new executable X.
  • Step 2: Compile SA with ET, yielding new executable Y.

Since X and Y were generated by two different compilers, they should have different binary code but be functionally equivalent. So far, so good.


  • Step 3: Compile SA with X, yielding new executable V.
  • Step 4: Compile SA with Y, yielding new executable W.

Since X and Y are functionally equivalent, V and W should be bit-for-bit equivalent.

As linked from Wheeler's paper, University of Michigan researchers discovered and implemented a hardware backdoor that can be installed by a single employee at the processor's fabrication facility and triggered by a sequence of obscure commands that charge a capacitor, then eventually trigger and grant OS access. This is a scary prospect because of how monumentally difficult it would be to detect such a backdoor.

This xkcd comic introduced me to Reflections on Trusting Trust: The reference can be found in the hover text. In the explain xkcd wiki, Wheeler's paper is mentioned:

Reflections on Trusting Trust

Originally written as email on 2019-09-25 to Dad and David

I recently ran across the influential paper "Reflections on Trusting Trust" by Ken Thompson. He describes a vulnerability in which the C compiler source can be modified to install a backdoor into the Unix login command. This compiler source is then compiled into an executable and distributed. If the bugged compiler compiles Unix, will recognize and install this backdoor. It is also bugged so that when it compiles itself, it plants the backdoor thus perpetuating the vulnerability into future versions of the compiler. This exploit is extremely hard to detect because it only exists in the executable, so it won't be seen when looking at the source. In effect, we can't trust any software we did not write ourselves, including compilers.

Here's a link to the paper. It's very short and highly understandable:

I hope you find this interesting and stimulating. I've included an email that I sent to my CS 236 professor following up on a discussion we had yesterday. The conversation started with him describing how I could parse German sentences (which he happens to know) with material from class and morphed into security practices and concerns. This paper has nothing to do with CS 236, but I figure I'll be having many more conversations with him.


Webkit color implementation



JSX usage outside of React

JSX can be used outside of React as Robert Prehn outlines in his article.

You could use this to do anything that lends itself to functional composition. Some ideas:

  • You could create DSLs with an XML-like syntax within your JavaScript.
  • You could abuse the JSX transform to compile XML configuration or seed data into JavaScript objects (please don't).
  • You could even make a whole XML-syntax functional programming language that compiles to JS (just stop).



Using C preprocessor language agnostically

The C preprocessor is independent of the C language and thus can be used with languages that do not have compile-time evaluation.

-E  Stop after the preprocessing stage; do not run the compiler
    proper. The output is in the form of preprocessed source code,
    which is sent to the standard output.

-P  Inhibit generation of linemarkers in the output from the
    preprocessor. This might be useful when running the preprocessor
    on something that is not C code, and will be sent to a program
    which might be confused by the linemarkers.

The source file must be named with a .c suffix and the output can be redirected to a file.

gcc -E -P hello.js.c > hello.js
#define HOWDY

function hello(name) {
  console.log('Hello, ' + name);
#ifdef HOWDY
  console.log('Howdy, ' + name + '!');


Apollo Guidance Computer source



Implementing arbitrary precision integers

Project idea: arbitrary precision integers could be stored using contiguous integers with carry and borrow used to implement addition and subtraction across boundaries. Multiplication and division would be more difficult.

This idea was sparked by something in a CS 224 Computer Systems lecture.


youtube-dl video downloader

youtube-dl is a highly configurable command-line program written in Python to download videos from and other video sites.

2017-08-28 (latest)

Pi series

The sum of inverse squares is pi squared over six:

\frac{\pi^2}{6} = \sum_{n=1}^{\infty} \frac{1}{n^2}

The arctangent power series of 1 is equal pi/4:

\arctan(x) = 1 - \frac{x^3}{3} + \frac{x^5}{5} - \frac{x^7}{7} + ...
\arctan(1) = \frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + ...


Initial Go commits

The first four commits in the Go repository reference the evolution of C, a callback to when Rob Pike worked with Brian Kernighan in the 1980s at Bell Labs.

convert to Draft-Proposed ANSI C, Brian Kernighan committed on Apr 1, 1988

#include <stdio.h>

    printf("hello, world\n");

convert to C, Brian Kernighan committed on Jan 19, 1974

main() {
    printf("hello, world");

hello, world, Brian Kernighan committed on Jul 18, 1972

main( ) {
    extrn a, b, c;
    putchar(a); putchar(b); putchar(c); putchar('!*n');
a 'hell';
b 'o, w';
c 'orld';

Navigating history on GitHub to earliest commit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.