Skip to content

Instantly share code, notes, and snippets.

@oconnor663
oconnor663 / demo.py
Last active July 2, 2024 01:09
Python resource leak demo
def open_ten_files():
files = [open("/dev/null") for _ in range(10)]
silly_map1 = {"files": files}
silly_map2 = {"files": files}
silly_map1["other_map"] = silly_map2
silly_map2["other_map"] = silly_map1
return silly_map1
mylist = []
max_len = 5
@oconnor663
oconnor663 / lock.cpp
Created June 24, 2024 18:21
folly::Synchronized race condition examples
#include <cstdint>
#include <cstdio>
#include <mutex>
#define GLOG_USE_GLOG_EXPORT
#include <folly/Synchronized.h>
class IntBumper {
public:
IntBumper(uint64_t &x) : m_ptr(&x) {}
@oconnor663
oconnor663 / lib.rs
Created March 1, 2024 08:46
Too Many Linked Lists, Miri failure
use std::ptr;
pub struct List<T> {
head: Link<T>,
tail: *mut Node<T>, // DANGER DANGER
}
type Link<T> = Option<Box<Node<T>>>;
struct Node<T> {
@oconnor663
oconnor663 / lifetimes.rs
Last active February 25, 2024 18:49
A first look at Rust lifetimes
fn main() {
let my_string = String::from("foo");
let mut my_vec = Vec::<&String>::new();
my_vec.push(&my_string);
// drop(my_string);
println!("{:?}", my_vec);
}
@oconnor663
oconnor663 / invariant.rs
Created February 11, 2024 05:27
invariant lifetimes
fn foo<'left, 'right>(_: &mut &'left i32, _: &mut &'right i32)
where
'left: 'right,
{
}
// fn foo<'both>(_: &mut &'both i32, _: &mut &'both i32) {}
fn main() {
let a = 42;
@oconnor663
oconnor663 / temporary_lifetime_extension.cpp
Last active January 27, 2024 20:03
references in structs and temporary lifetime extension
#include <iostream>
struct Foo {
const int &x;
};
struct ConvertedFoo {
const int &x;
// Foo is implicitly convertible to ConvertedFoo
@oconnor663
oconnor663 / bench.py
Created January 17, 2024 19:39
short-input hash benchmarks in Python
import time
from blake3 import blake3
from hashlib import sha256, sha512, blake2s
input_bytes = b"hello world"
print(f"input bytes: {input_bytes}")
warmup_iterations = 1_000
measure_iterations = 1_000_000
@oconnor663
oconnor663 / index.md
Last active December 20, 2023 18:15
Why Iterators in Rust should not be Copy

Here's a valid Rust snippet:

fn main() {
    let mut range = 0..5;
    for num in &mut range {
        if num == 2 {
            break;
        }
    }

GCC 13.1.1 (Arch Linux) seems to mis-align __m512i vectors on the stack when -fsanitize=address is enabled. repro.c (below in this Gist) is a minimized repro. Compile it like this:

gcc repro.c -g -mavx512f -fsanitize=address

When I execute it I get the following:

$ ./a.out

AddressSanitizer:DEADLYSIGNAL

0eNrtnU1vI0cOhv9KoLN70CTrc8455LC3zW1hBP7QeoQ48kC2BxsE/u8r2ZLdsVvdfGR7ImF8SeARTVNkNZusevnWX5PTy9vp18VsfvPb6dXV75PPfz39y/Xk8386P64+u56ffG1urpqLxex89fP/Jp9NjyZ/rv53dzQ5Ob2+ury9mTYrua+z+cXk883idno0mZ1dzR/UXc8u5ieXq9+9+fPrdPJ5MruZ/jE5msxP/lj9tDiZXU6Wmmbz8+lSudwdH02m85vZzWz68Pv3P/z52/z2j9PpYinw+Juns4tmejk9u1nMzpqvV5fTpdKvV9fL37yar02VdG9qu9S/tGe+lJ3dm/XXRFb/WUzPu39i9Q2lrAy4WEyn876P7paa5tPZxZfTq9vFyj4px3dHL4zUv329Zu2Cl+blT/HewKb9FO969NijnuubpaaLLzfNvb9earJ7PdKnJLiV1O1KIrTE+pQkaEmvkgwt6XVsgZb0KqnQktynRFpoSr8WgbbUXi0KbenXgldt74oTvG771dCVK72rTuja3aKGrl7pXXlSdsiCkkAa7PzR1c8Wt6fFHtGXafLIYl+ilErd2usPbalbe58iFWpNvxql1vQ+R2rUmn41AVqjvc+RRmjNFjWJWtP7HGmm1vSrKdSa/uVHV3G/GqOrWHuXn9FVvEUNXcXau/yMruItaugqtt7lZ3GHxHlf6X7X+tFwpdT/bbOzDg3rOtR0WYceTc5ni4evuiweV1/nZna5Lsaf/6I+1tfrv3N5Mj//7+xyVdJvkxYkrUjakHRA0hFJJySdkXRB0pVFBwaTRVNYOIXFU1hAhUVUWEiFxVRYUIVFVVlUFT6jLKrKoqosqsqiqiyqyqKqLKrKomosquaNqqGsbiirG8rqhrK6oaxuKKsbyuqGsrqhrG4oqxvL6sayurGsbiyrG8vqxrK6saxuLKsby+rGsrqxrG4sqxvL6sayurGsbiyrG8vqxrK6saxuLKsby+rGsnp4amY84uudaUHiLZJ2P6qRPaq