Skip to content

Instantly share code, notes, and snippets.

@bjorng
bjorng / base64_bench
Created September 6, 2022 04:42
Benchmarking for encoding and decoding Base64 binaries
-module(bench). % -*- erlang -*-;
-mode(compile).
%% Based on the benchmark in https://github.com/erlang/otp/issues/5639.
main([]) ->
Data100B = crypto:strong_rand_bytes(100),
Data1MB = crypto:strong_rand_bytes(1024 * 1024),
io:format("== Testing with 100 B ==~n"),
test_encode(Data100B, 1_000_000),
@bjorng
bjorng / pt_benchmark.erl
Created October 17, 2018 08:08
Simple benchmarking of persistent terms
-module(pt_benchmark).
-compile([export_all,nowarn_export_all]).
b() ->
%% run(fun pt_imm/1, fun pdict_imm/1),
%% run(fun pt/1, fun pdict/1),
%% run(fun pt_striped/1, fun pdict_striped/1),
%% run(fun pt_record/1, fun pdict_record/1),
run(fun ets_imm/1, fun pt_imm/1),
run(fun ets/1, fun pt/1),
@bjorng
bjorng / hipe_bug.S
Created August 20, 2018 14:13
A bug in HiPE when using the new SSA-based compiler
Demonstrating the bug.
Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Eshell V10.0 (abort with ^G)
1> c(hipe_bug, [from_asm,native]).
{ok,hipe_bug}
2> hipe_bug:run().
size_object: matchstate term not allowedAborted (core dumped)
@bjorng
bjorng / TODO.md
Last active August 16, 2018 08:27
Informal to-do and bug list for new the new SSA-based compiler

TO DO

  • Source code:
foo(X, Y) ->
    case X =:= $T orelse X =:= $\s of
        true -> {ok, Y};
        _ -> error
 end.
@bjorng
bjorng / same_line.erl
Created August 16, 2018 08:24
Core transform to set all line numbers to the same value (42). Useful for simulating huge Elixir macros.
-module(same_line).
-export([core_transform/2]).
%%
%% erlc -pa . '+{core_transform,same_line}' <filename>.erl
%%
core_transform(Core, _Options) ->
F = fun(Node) ->
case cerl:get_ann(Node) of
@bjorng
bjorng / hipe_bug.S
Last active August 13, 2018 08:16
Bug in HiPE for map matching
Demonstrating the bug.
The hipe_bug.S file was generated using the new SSA-based compiler.
Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Eshell V10.0 (abort with ^G)
1> c(hipe_bug, [from_asm,native]), hipe_bug:run().
** exception error: {badmap,{cons,{arg,0},nil}}
in function hipe_bug:dig_out_fc/1
@bjorng
bjorng / beam-emu-vars
Last active September 27, 2017 11:02
Find out where important BEAM emulator variables are stored
#!/usr/bin/perl -w
use strict;
# Analyse beam_emu.s and try to find out the registers
# used for the important variables in process_main().
#
# Works for .s files from clang or gcc. For gcc, the -fverbose-asm
# option must be used.
#
# Example:
@bjorng
bjorng / otp-bisect-ts
Last active January 3, 2022 08:23
Automate running git bisect in the otp repository
#!/bin/sh
git clean -dxfq
./otp_build autoconf
./configure
# Build the minimum number of applications.
OTP_SMALL_BUILD=true
export OTP_SMALL_BUILD
for app in asn1 hipe ic inets jinterface snmp; do
echo "Build faster" >lib/$app/SKIP
@bjorng
bjorng / otp-asm-diff
Created April 25, 2017 10:36
Compare two directories of .S files, producing a summary of the change of function sizes
#!/usr/bin/env escript
%% -*- erlang -*-
-mode(compile).
-import(lists, [foreach/2]).
main([Old,New]) ->
Wc = filename:join(Old, "*.S"),
Files0 = filelib:wildcard(Wc),
Files = [filename:basename(F) || F <- Files0],
F = fun(Name) ->
@bjorng
bjorng / otp-diffable-asm
Last active August 31, 2017 05:31
Compile some OTP applications into a diff-friendly assembly format
#!/usr/bin/env escript
%% -*- erlang -*-
-mode(compile).
main(Args) ->
case Args of
[] ->
do_compile("asm");
[OutDir] ->
do_compile(OutDir);