Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / file.md
Last active September 10, 2026 18:28
Using Jemalloc 5 with Ruby.md

For years, people have been using jemalloc with ruby. There were various benchmarks and discussions. Legend had it that Jemalloc 5 didn't work as well as Jemalloc 3.

Then, one day, hope appeared on the horizon. @wjordan offered a config for Jemalloc 5.

Ubuntu/Debian

FROM ruby:3.1.2-bullseye
RUN apt-get update ; \

This recipe is a work in progress and has never been run as-is.

  • timeouts are in ms
  • lock timeout: in postgres, when a statement that wants a restrictive lock waits on another lock, other statements that want locks can't jump the queue. so even though the statement that is waiting might only take a very short amount of time, when it starts running, while it is waiting no other statements can begin. So we set the lock timeout pretty low and retry if we don't get it.
  • statement timeout: we set a short statement timeout before statements which do lock and which we expect to take a short amount of time, just in case something about our assumptions/understanding is wrong and the statement ends up taking a long time. if this happens the statement will bail early without causing harm, and we can investigate what is wrong with
@jjb
jjb / file.md
Created May 13, 2026 23:07
attempts to replace Timeout.timeout with socket timeout ~2020-2025

(copy/pasted responses from claude)


I found a strong lead. Let me dig further — the Ruby net-http issue references an earlier patch that was rejected, and there's likely a similar story in net-http and possibly net-ssh.Getting closer. There's a key ticket — Ruby Feature #14430 — about replacing Timeout.timeout in net/http with Socket.tcp. Let me find the actual PR that landed and was reverted.Now let me look for the actual merge and revert pattern — particularly whether something was merged in net-http and then rolled back.Let me look for the actual net-http PR that landed and was reverted — there's a clear pattern of attempts here.Let me check ruby/net-http directly for a merged-then-reverted change.Let me check directly in the ruby/net-http repository for any merged-then-reverted activity around this.Found it. Mohamed Hafez submitted three PRs in April 2021 across net-http, net-smtp, and net-ftp. Let me confirm these were reverted.Let me directly check the net-http issue and PR #224 that tracked t

@jjb
jjb / code.rb
Last active March 25, 2025 13:36
delete old branches on github
#!/usr/bin/env ruby
require 'date'
require 'shellwords'
REMOTE = 'origin'
PROTECTED_BRANCHES = ['master']
YEARS_OLD = 2
def run_command(cmd)
@jjb
jjb / gist:996292
Created May 27, 2011 22:11
How to securely acquire the Mozilla root certificate bundle for use with curl, Net::HTTP, etc.

If you want to use curl or net-http/open-uri to access https resources, you will often (always?) get an error, because they don't have the large number of root certificates installed that web browsers have.

You can manually install the root certs, but first you have to get them from somewhere. This article gives a nice description of how to do that. The source of the cert files it points to is hosted by the curl project, who kindly provide it in the .pem format.

problem: Sadly, ironically, and comically, it's not possible to access that file via https! Luckily, the awesome curl project does provide us with the script that they use to produce the file, so we can do it securely ourselves. Here's how.

  1. git clone https://github.com/bagder/curl.git
  2. cd curl/lib
  3. edit mk-ca-bundle.pl and change:
@jjb
jjb / gist:7389552
Last active December 22, 2024 15:58
Ruby 2.1 memory configuration

This all applies to Ruby 2.1. In some cases a setting is not available in 2.0, this is noted. There is also a different with 1.9, 1.8, and REE --- these are not noted.

All the relevant code is in https://github.com/ruby/ruby/blob/master/gc.c

RUBY_HEAP_MIN_SLOTS

default: 10000

The number of heap slots to start out with. This should be set high enough so that your app has enough or almost enough memory after loading so that it doesn't have to allocate more memory on the first request (althogh this probably isn't such a big deal for most apps).

(todo: figure out how big a slot is. i think the answer can be infered from this code.)

@jjb
jjb / code.rb
Last active December 16, 2024 19:35
Script to delete old git branches
# frozen_string_literal: true
require 'time'
# Get the list of branches with their last commit date
branches = `git for-each-ref --format '%(refname:short) %(committerdate)' refs/heads/`
months_ago = 12
threshold = Time.now - (months_ago * 30 * 24 * 60 * 60)
@jjb
jjb / shipyard-shell.sh
Created June 22, 2023 17:48
Shipyard Shell
#!/usr/bin/env sh
# usage
# shipyard-shell PRNUMBER SERVICENAME
# shipyard-shell 6187 postgres
# shipyard-shell 6187 api
pr=$1
service=$2
identifier=`shipyard get environments | grep pr$pr | xargs | cut -w -f1` # xargs to remove leading and trailing whitespace

Here's something I spent a long time debugging, so I'm making this gist to help others find the solution when searching.

Summary

postgres 12 on MacOS will cause crashes when forking. The solution is to do one of these two things:

  • add gssencmode: "disable" to database.yml
  • if using homebrew: uninstall postgres, update homebrew, reinstall postgres

Here's some discussion:

@jjb
jjb / gist:7175694
Last active July 20, 2024 18:17
How to configure Textmate to use the rbenv ruby and gems
  1. Preferences -> Variables
  2. add this to the front of PATH: $HOME/.rbenv/bin:$HOME/.rbenv/shims:
  3. set TM_RUBY to this: $HOME/.rbenv/shims/ruby
  4. populate RBENV_VERSION with your version. example: 2.1.0-preview1
  5. (and make sure the checkboxes for each of those variables is selected)