Skip to content

Instantly share code, notes, and snippets.

View jimblandy's full-sized avatar

Jim Blandy jimblandy

View GitHub Profile
@jimblandy
jimblandy / threads-memory.c
Created April 23, 2020 21:34
Program to create N boring threads that do nothing, for measuring minimal per-thread memory use
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NUM_THREADS 500
#define BYTES_PER_THREAD 1024 * 1024 // 1 MB
void *child_main(void *num_threads_ready) {
@jimblandy
jimblandy / transforms.html
Created February 12, 2020 22:40
playing with SVG transforms
<html>
<head></head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400">
<rect x="50" y="50" width="100" height="220" fill="none" stroke="black"/>
<rect x="30" y="20" width="60" height="190" fill="none" stroke="gray"/>
<g id="outer">
<g id="inner" transform="rotate(10) translate(200,0)">
<rect id="red" x="50" y="50" height="100" width="100" fill="red"/>
<rect id="blue" x="50" y="170" height="100" width="100" fill="blue"/>
@jimblandy
jimblandy / the-heck-emacs.sh
Created July 2, 2019 16:53
Script for figuring out what (interesting) system calls Emacs is doing
#!/usr/bin/env bash
strace -p "$(pgrep emacs)" \
-e '!pselect6,recvmsg,poll,writev,rt_sigprocmask,rt_sigreturn,read,write,timerfd_settime' \
-e 'signal=!SIGIO'
# -e '!pselect6,rt_sigprocmask,rt_sigreturn,timerfd_settime' \
@jimblandy
jimblandy / weak-references.md
Last active May 15, 2019 00:29
Take a stab at specifying the conditions under which weak references can be cut

From the spec's point of view, all objects live forever. Garbage collection is just an optimization, permitted only because it is undetectable. Even though weak references are generally seen as making GC visible, we can actually specify weak references in a way that preserves the traditional approach: the spec will still assume all objects live forever, and will merely describe when it is permitted to remove certain edges to them.

An object is "relevant" if replacing it with any arbitrary other object could have a visible effect on the future execution of the program.

@jimblandy
jimblandy / jit-comment.patch
Created April 15, 2019 18:21
Fix comment in jit::CanEnterBaselineAtBranch
diff --git a/js/src/jit/BaselineJIT.cpp b/js/src/jit/BaselineJIT.cpp
--- a/js/src/jit/BaselineJIT.cpp
+++ b/js/src/jit/BaselineJIT.cpp
@@ -276,19 +276,19 @@ MethodStatus jit::CanEnterBaselineAtBran
InterpreterFrame* fp) {
if (!CheckFrame(fp)) {
return Method_CantCompile;
}
// This check is needed in the following corner case. Consider a function h,
@jimblandy
jimblandy / bah.js
Created March 27, 2019 21:32
This should print two completions before 'callback', right???
let g = newGlobal({newCompartment: true});
g.eval(`
async function f() {
debugger;
await Promise.resolve(3);
return "ok";
}
`);
let dbg = Debugger(g);
diff --git a/js/src/gc/Barrier.h b/js/src/gc/Barrier.h
--- a/js/src/gc/Barrier.h
+++ b/js/src/gc/Barrier.h
@@ -24,17 +24,17 @@
* write barrier should be invoked whenever a write can cause the set of things
* traced through by the GC to change. This includes:
* - writes to object properties
* - writes to array slots
* - writes to fields like JSObject::shape_ that we trace through
* - writes to fields in private data
@jimblandy
jimblandy / squeeze-solution.rs
Created February 28, 2019 21:57
Solution to the Squeeze challenge.
// You'll need this at the top of your crate:
// #![feature(optin_builtin_traits)]
//! The `squeeze` function.
//!
//! Omit unit values. That is:
//! - `squeeze(x, ())` returns `x`.
//! - `squeeze((), x)` returns `x`.
//! - `squeeze(x, y)` returns `(x, y)`, when neither `x` nor `y` is `()`.
//! - `squeeze((), ())` returns `()`.
@jimblandy
jimblandy / squeeze.rs
Created February 27, 2019 02:52
How would you go about implementing `squeeze`?
#[cfg(test)]
mod test {
use super::squeeze;
#[test]
fn test_squeeze() {
// The idea here is that any non-() type would work.
assert_eq!(squeeze(1i32, 2i32), (1, 2));
assert_eq!(squeeze(true, false), (true, false));
assert_eq!(squeeze("foo", Some("bar")), ("foo", Some("bar")));
@jimblandy
jimblandy / neg2.el
Last active February 2, 2019 21:38
Conversion to base -2, in Emacs Lisp.
;;; Don't just eval-buffer this; it's got a mix of definitions and example evaluations.
(defun neg2-range (width)
(if (zerop width) (cons 0 0)
(let ((range (neg2-range (1- width)))
(offset (expt -2 (1- width))))
(if (< offset 0)
(incf (car range) offset)
(incf (cdr range) offset))
range)))