Skip to content

Instantly share code, notes, and snippets.

@jjb
jjb / file.md
Last active April 30, 2024 11:26
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 / install.sh
Created April 25, 2024 04:15
Installing psych gem with macports
sudo port install libyaml yaml-cpp
bundle config build.psych --with-opt-include=/opt/local/include --with-opt-lib=/opt/local/lib
bundle
# OR, standalone
gem install psych -- --with-opt-include=/opt/local/include --with-opt-lib=/opt/local/lib
@jjb
jjb / code.rb
Created April 18, 2024 00:40
Hacking ruby patterns to be first-class objects that can be passed around
# need to "pin" the variable, but this only works with the lambda because
# pattern matching falls back to case matchint (=== on any object in ruby,
# which is NOT "identical object" as in JS)
def matches1(pat,x)
case x
in ^pat
puts true
else
puts false
end
sudo port selfupdate
sudo port install ruby32 # will include readline
sudo port select --set ruby ruby32
### >>> OPEN A NEW TERMINAL!!! <<<
### otherwise, the configuration won't point to the correct version of readline, no matter what you do.
### not sure if this is "through" the ruby binary, or something else in the environment
### i just saved you 17 hours of your life, you're welcome
curl -OL https://github.com/thoughtbot/gitsh/releases/download/v0.14/gitsh-0.14.tar.gz
@jjb
jjb / gist:7389552
Last active March 16, 2024 18:48
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 / .gitconfig
Created February 6, 2024 16:36
git cleanup
[alias]
delete-squashed = "!f() { local targetBranch=${1:-master} && git checkout -q $targetBranch && git branch --merged | grep -v \"\\*\" | xargs -n 1 git branch -d && git for-each-ref refs/heads/ \"--format=%(refname:short)\" | while read branch; do mergeBase=$(git merge-base $targetBranch $branch) && [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == \"-\"* ]] && git branch -D $branch; done; }; f"
@jjb
jjb / file.md
Created January 26, 2024 00:35
trying to get github markdown note highlight things to work in indents

Note

here is some important info

  1. item 1

    [!NOTE] here is some important info

  2. item 2
@jjb
jjb / file.md
Created June 20, 2023 02:15
How to see which rubocop file is being evaluated

If you are in a situation where rubocop is hanging on a file but you don't know which one, afaik there is no way to have rubocop pring each filename as it runs it. None of the formatters do this and there is no verbose mode. Even --debug does not do this.

Here is a hack to (very slowly) invoke rubocop once for each candidate file:

bundle exec rubocop --list-target-files > files.txt
cat files.txt | xargs -L1 -n1 bundle exec rubocop # this works on macos/bsd, use --verbose instead of -t on linux

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: