Skip to content

Instantly share code, notes, and snippets.

@KodrAus
KodrAus / Profile Rust on Linux.md
Last active August 12, 2024 12:37
Profiling Rust Applications

Profiling performance

Using perf:

$ perf record -g binary
$ perf script | stackcollapse-perf.pl | rust-unmangle | flamegraph.pl > flame.svg

NOTE: See @GabrielMajeri's comments below about the -g option.

sudo su
apt-get install -y autoconf automake libtool nasm make pkg-config
cd /tmp
wget https://github.com/mozilla/mozjpeg/releases/download/v3.1/mozjpeg-3.1-release-source.tar.gz
tar -xvf mozjpeg-3.1-release-source.tar.gz
cd mozjpeg
autoreconf -fiv
mkdir build && cd build
sh ../configure
make install
@tonymorris
tonymorris / free-classy-prisms.hs
Created April 28, 2017 07:35
Free monad with classy prisms on grammar
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DeriveFunctor #-}
import Control.Lens
import Prelude hiding (readFile, writeFile, print)
import qualified Prelude as Prelude(readFile, writeFile, print)
@amosr
amosr / House.v
Last active September 11, 2019 10:45
Coq text adventure
Set Implicit Arguments.
Inductive Place
:= Kitchen | Bedroom | Hallway | Outside.
Inductive Card := NORTH | EAST | SOUTH | WEST.
Definition moveTo (p : Place) (c : Card) : option Place :=
match p, c with

The original code (~7.2s on my laptop).

import System.Random
import System.CPUTime

rainfall :: [Int] -> Int
rainfall xs = sum (zipWith (-) mins xs)
@damncabbage
damncabbage / Examples.ex
Created June 6, 2018 08:50
Elixir "Traversing Error Mountain" Code Samples
defmodule Examples do
def zero do
files = ["abc.json", "def.json", "ghi.json"]
contents = files
|> Enum.map(fn (file) ->
File.read(file)
end)
# => [{:ok, "..."}, {:error, :enoent}, {:ok, "..."}]
@amosr
amosr / tower-tuple.v
Created June 19, 2018 05:21
Coq encoding of Towers of Hanoi
(* A very simple Coq encoding of the "Towers of Hanoi" puzzle game. *)
Require Import Coq.Lists.List.
Import ListNotations.
Require Import Coq.Arith.Compare_dec.
(* Proof of a sorted list *)
Inductive Sorted : list nat -> Prop :=
| sorted'nil : Sorted []
@dlaehnemann
dlaehnemann / flamegraph_rust.md
Last active February 14, 2024 14:14
flamegraphing rust binaries' cpu usage with perf

A quadratic space is a real vector space V with a quadratic form Q(x), e.g. V = R^n with Q as the squared length. The Clifford algebra Cl(V) of a quadratic space is the associative algebra that contains V and satisfies x^2 = Q(x) for all x in V. We're imposing by fiat that the square of a vector should be the quadratic form's value and seeing where it takes us. Treat x^2 = Q(x) as a symbolic rewriting rule that lets you replace x^2 or x x with Q(x) and vice versa whenever x is a vector. Beyond that Cl(V) satisfies the standard axioms of an algebra: it lets you multiply by scalars, it's associative and distributive, but not necessarily commutative.

Remarkably, this is all you need to derive everything about Clifford algebras.

Let me show you how easy it is to bootstrap the theory from nothing.

We know Cl(V) contains a copy of V. Since x^2 = Q(x) for all x, it must also contain a copy of some nonnegative reals.

@zeux
zeux / simplifier.cpp
Created February 18, 2019 04:03
SIMD sloppy simplifier for "Flavors of SIMD" blog post
// This file is part of meshoptimizer library; see meshoptimizer.h for version/license details
#include "meshoptimizer.h"
#include <assert.h>
#include <float.h>
#include <math.h>
#include <string.h>
#ifndef TRACE
#define TRACE 0