Skip to content

Instantly share code, notes, and snippets.

Avatar

Niklas Hambüchen nh2

View GitHub Profile
@nh2
nh2 / FoldMapA.hs
Created Nov 18, 2016
foldMapA, Applicative-action folding in Haskell
View FoldMapA.hs
-- Written for GHC 8.0
import Data.Foldable (foldr')
-- The other day I had the feeling that if I can write
--
-- minumumABy :: (Applicative f)
-- => (b -> b -> Ordering) -> [a] -> (a -> f b) -> f (Maybe b)
--
@nh2
nh2 / rigid-transform-with-scale.py
Last active Apr 23, 2021
Rigidly (+scale) aligns two point clouds with know point-to-point correspondences, in Python with numpy
View rigid-transform-with-scale.py
import numpy as np
import numpy.linalg
# Relevant links:
# - http://stackoverflow.com/a/32244818/263061 (solution with scale)
# - "Least-Squares Rigid Motion Using SVD" (no scale but easy proofs and explains how weights could be added)
# Rigidly (+scale) aligns two point clouds with know point-to-point correspondences
# with least-squares error.
View hetzner-dedicated-wipe-and-install-nixos.sh
#!/usr/bin/env bash
# Installs NixOS on a Hetzner server, wiping the server.
#
# This is for a specific server configuration; adjust where needed.
#
# Prerequisites:
# * Update the script to adjust SSH pubkeys, hostname, NixOS version etc.
#
# Usage:
@nh2
nh2 / configuration.nix
Last active Jan 8, 2021
NixOS configuration for nh2's mail server presentation (https://nh2.me/recent/Running-your-own-mailserver.pdf)
View configuration.nix
# Edit this configuration file to define what should be installed on
# your system. Help is available in the configuration.nix(5) man page
# and in the NixOS manual (accessible by running ‘nixos-help’).
{ config, pkgs, ... }:
let
floatingIPv4 = "95.216.183.106";
floatingIPv6 = "2a01:4f9:c01f:65::1";
in
{
@nh2
nh2 / mmap-memory-usage.c
Created Dec 7, 2020
Demo of how mmap() affects VIRT, RES, SHR memory usage in Linux
View mmap-memory-usage.c
// Demo of how mmap() affects VIRT, RES, SHR memory usage in Linux.
//
// A useful resource for more background:
// https://techtalk.intersec.com/2013/07/memory-part-2-understanding-process-memory/
// Required for:
// * `O_TMPFILE`
// * `fallocate()`
#define _GNU_SOURCE
@nh2
nh2 / SocketUtils.hs
Created Jul 13, 2019
Check whether a port is open in Haskell
View SocketUtils.hs
module SocketUtils
( isPortOpen
, simpleSockAddr
) where
import Data.Word (Word8)
import Foreign.C.Error (Errno(..), eCONNREFUSED)
import GHC.IO.Exception (IOException(..))
import Network.Socket (PortNumber, socket, connect, close', Family(AF_INET), SocketType(Stream), SockAddr(SockAddrInet), tupleToHostAddress)
import UnliftIO.Exception (try, bracket, throwIO)
@nh2
nh2 / tcp-nodelay.md
Last active Nov 18, 2020
Understanding TCP_NODELAY
View tcp-nodelay.md

I believe the following is the best way to work with Nagle's algorithm / TCP_NODELAY / TCP_CORK.

It is described in this RedHat manual and the verdict is:

  • Set TCP_NODELAY = 1, always.
  • If you can batch data for sending by creating a buffer manually, or using writev(), prefer that.
  • If you cannot (e.g. "when using different libraries that provides abstractions for layers" from the above manual):
    • Set TCP_CORK = 1, then write the data, then set TCP_CORK = 0.
    • This builds a packet in kernel space and then flushes it out.
View index-only-scan-not-working.md

Using postgresql 10 on a spinning disk.

Table layout

postgres=# \d+ t;
                                                 Table "public.t"
  Column  |  Type   | Collation | Nullable |            Default            | Storage  | Stats target | Description 
----------+---------+-----------+----------+-------------------------------+----------+--------------+-------------
 id       | integer |           | not null | nextval('t_id_seq'::regclass) | plain    |              | 
@nh2
nh2 / outlook-dkim.md
Created Oct 21, 2020
Outlook apparently cannot deterministically run DKIM
View outlook-dkim.md

It seems Outlook is totally bugged.

I set up a mail server and sent 4 emails in a row, for the first one Microsoft said:

dkim=pass (signature was verified)

For the second one it said:

@nh2
nh2 / SlopProblem.hs
Last active Oct 19, 2020
Repro for GHC making crazy amounts of slop with +RTS -N2, see https://gitlab.haskell.org/ghc/ghc/-/issues/18849
View SlopProblem.hs
-- Run with:
-- ghc --make -O -threaded -fforce-recomp SlopProblem.hs && command time ./SlopProblem +RTS -N1 -s
--
-- My output with ghc 8.6.5 on a 4-core machine:
-- ...
-- 336,707,456 bytes maximum residency (14 sample(s))
-- 611,774,592 bytes maximum slop
-- ...
-- ... 1716344maxresident)k
-- The slop reduces to 1 MB when `-N1` is used, and the resident memory usage