Skip to content

Instantly share code, notes, and snippets.

View daniel-j-h's full-sized avatar
🤗

Daniel J. H. daniel-j-h

🤗
View GitHub Profile
@daniel-j-h
daniel-j-h / pgo.sh
Last active February 19, 2024 05:51
pgo: profile guided optimization with gcc
# Instrument binaries, pgo data to /data/pgo, serial make is important to not confuse the pgo generator
env CXXFLAGS='-march=native -fprofile-dir=/data/pgo -fprofile-generate=/data/pgo' cmake .. -DCMAKE_BUILD_TYPE=Release
make -j 1
# Run instrumented program, generate and write pgo data
./runIt
# Use profile data and feed into gcc, correct for threading counter noise, serial make is important to not confuse the pgo generator
env CXXFLAGS='-march=native -fprofile-dir=/data/pgo -fprofile-use=/data/pgo -fprofile-correction' cmake .. -DCMAKE_BUILD_TYPE=Release
make -j 1
@daniel-j-h
daniel-j-h / ld.gold.sh
Last active September 22, 2023 00:54
default to ld.gold on Ubuntu'ish
update-alternatives --install "/usr/bin/ld" "ld" "/usr/bin/ld.gold" 20
update-alternatives --install "/usr/bin/ld" "ld" "/usr/bin/ld.bfd" 10
update-alternatives --config ld
ld --version
GNU gold
export CPP=cpp-5 gcc-5 g++-5
env CXXFLAGS='-march=native -flto -fuse-linker-plugin' cmake .. -DCMAKE_BUILD_TYPE=Release
@daniel-j-h
daniel-j-h / Map.kt
Last active June 16, 2023 07:37
Adapting an imperative map (Google Map, Mapbox Map, etc.) for a declarative UI such as Android Compose; below shows an example for a Mapbox map, in https://github.com/android/compose-samples/tree/e6994123804b976083fa937d3f5bf926da4facc5/Crane#crane-sample you will find an example for a Google Map
package com.example.view
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.mapbox.geojson.Point
@daniel-j-h
daniel-j-h / lucas2018-to-geojson.py
Created February 21, 2023 21:36
LUCAS 2018 dataset as a GeoJSON file for simple and easy visualization - https://esdac.jrc.ec.europa.eu/content/lucas-2018-topsoil-data
#!/usr/bin/env python3
import sys
import csv
import json
import argparse
from pathlib import Path
def main(args):
@daniel-j-h
daniel-j-h / default.nix
Created July 7, 2016 22:47
Nix C++ compiler, CMake, Boost skeleton --- stable ABI
# Nix skeleton for compiler, cmake, boost.
# Dependencies (boost and others you specify) are getting built with selectec compiler (for ABI compatibility).
# Examples:
# nix-shell --argstr compiler gcc5 --run 'mkdir build && cd build && cmake .. && cmake --build .'
# nix-shell --argstr compiler gcc6 --run 'mkdir build && cd build && cmake .. && cmake --build .'
# nix-shell --argstr compiler clang_38 --run 'mkdir build && cd build && cmake .. && cmake --build .'
{ nixpkgs ? import <nixpkgs> {}, compiler ? "gcc6" }:
let
@daniel-j-h
daniel-j-h / 1-notes.md
Last active February 10, 2023 01:04
SMT Solvers and Constraint Programming

On Logic Solvers

From SAT to SMT and back.

There is a Gist, see references [@gist].

Satisfiability (SAT)

Is a given formula (i.e. $\lnot A \land (B \lor C)$) satisfiable?

@daniel-j-h
daniel-j-h / ct-strings.cc
Created July 15, 2016 21:42
Compile-time strings and string concatenation
#include <stdexcept>
#include <utility>
class Str {
public:
template <std::size_t N>
constexpr Str(const char(&a)[N]) : p(a), s(N - 1) {}
constexpr char operator[](std::size_t n) const {
@daniel-j-h
daniel-j-h / readme.md
Created November 5, 2022 17:56
Re-key your encrypted volumes

Re-key your encrypted volumes

When you encrypt volumes like your laptop's SSD or USB sticks on Linux there's a high chance you're using Linux Unified Key Setup (LUKS) in the background. To decrypt volumes, LUKS uses your secret passphrase and puts it through a function to derive the actual volume decryption key. The key derivation function is supposed to be expensive to compute so that brute-force attacks are hard to pull off.

There are three problems with the LUKS setup in practice

  1. The key derivation function gets benchmarked and tuned to your system to keep the decryption process short e.g. below a second because no one wants to wait too long
  2. Therefore, if you encrypt volumes with a weak device like a laptop, the key derivation function will we weak
  3. If you encrypted a device in the past and are still using its volume, you will not benefit from more recent key derivation function algorithms which have stronger guarantees against brute force attacks e.g. using GPUs

That's why you should re-key y

@daniel-j-h
daniel-j-h / A-trees.py
Last active July 28, 2022 10:50
Trees with SQLAlchemy
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# http://docs.sqlalchemy.org/en/latest/orm/self_referential.html
import json
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, CheckConstraint
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
@daniel-j-h
daniel-j-h / tiler.py
Last active February 3, 2022 07:04
Tiles GeoTIFF for zoom level
#!/usr/bin/env python3
'''
pip install tqdm numpy pillow mercantile 'rasterio==1.0b1' 'rio-tiler==1.0a7'
'''
import os
import argparse