Skip to content

Instantly share code, notes, and snippets.

@josephg
josephg / lib.rs
Created September 17, 2024 07:48
shell out from rust to another program
/// Calls `dot -Tsvg` and passes in the contents of dot_content via stdin.
///
/// The output is read back to a string and returned.
pub fn generate_svg_with_dot(dot_content: String, dot_path: Option<OsString>) -> Result<String, Box<dyn Error>> {
let dot_path = dot_path.unwrap_or_else(|| "dot".into());
let mut child = Command::new(dot_path)
// .arg("-Tpng")
.arg("-Tsvg")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@josephg
josephg / Result
Created June 24, 2024 03:19
Benchmark
$ cargo run --release -- --bench
Compiling leading_zero_bench v0.1.0 (/home/seph/temp/leading_zero_bench)
Finished `release` profile [optimized] target(s) in 0.51s
Running `target/release/leading_zero_bench --bench`
lz time: [87.799 µs 87.983 µs 88.223 µs]
lz_branchless time: [27.554 µs 27.581 µs 27.611 µs]
--------------------------------------------------------------------------------
Command: target/profiling/am-repro
Massif arguments: --time-unit=B
ms_print arguments: massif.out.9109
--------------------------------------------------------------------------------
GB
12.66^ #
| @@@#
--------------------------------------------------------------------------------
Command: target/profiling/am-repro
Massif arguments: (none)
ms_print arguments: massif.out.3715
--------------------------------------------------------------------------------
MB
45.81^ #
| @@#:
@josephg
josephg / graph.py
Last active March 7, 2024 09:27
Graph diff
from dataclasses import dataclass
from enum import Enum
import heapq
Version = set[int] # The numbers in this set are indexes into the list of events in the graph.
@dataclass
class GraphEntry:
parents: set[int] # Set of indexes...
id: any
@josephg
josephg / Cargo.toml
Last active April 28, 2023 12:55
benchmark automerge complex trace
[package]
name = "automerge-test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
automerge = "0.4.1"
serde = { version = "1.0.160", features = ["derive"] }
~/temp $ mkdir a
~/temp $ cd a
~/temp/a $ git init
Initialized empty Git repository in /home/seph/temp/a/.git/
~/temp/a:master ✓ $ cat > foo
asdf
~/temp/a:master ✓ $ git add foo
~/temp/a:master ✗ $ git commit -m 'initial import'
[master (root-commit) 7fe725a] initial import
@josephg
josephg / main.rs
Created October 16, 2022 07:37
rotation puzzle
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::hash::Hash;
// Wrapper around u16. Using the lowest significant 9 bits to store a 3x3 state.
#[derive(Copy, Clone, Eq, PartialEq, Default, Hash)]
struct Bits(u16);
fn main() {
let mut map = HashMap::new();
@josephg
josephg / bench.js
Last active July 14, 2022 23:07
bench npm jumprope
// Read in a patch file and check that the patches all apply correctly.
// Run with node --expose-gc (file) using input files from here:
// https://github.com/josephg/crdt-benchmarks
const fs = require('fs')
const assert = require('assert')
const zlib = require('zlib')
const v8 = require('v8')
const Rope = require('jumprope')
@josephg
josephg / shelf.ts
Last active October 29, 2021 06:02
shelf
type Item = null | string | number | boolean | Item[] | {[k: string]: Item};
// Must match the shape of the data
type Version = number | [number, Version[]] | [number, {[k: string]: Version}];
type Path = (string | number)[]
interface Doc {
data: Item,
versions: Version,