Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
seanlinsley / malloc_trim.md
Created June 25, 2023 18:18
Prevent memory bloat on Linux with malloc_trim(0)

The default allocator on Linux has a tendency to free unused memory less aggressively than Mac and Windows. This creates the appearance of memory bloat. Calling malloc_trim(0) may not prevent out of memory events of that single process, but it helps you see the real memory usage, and frees up memory for use by other processes.

Relevant links:

// sourced from https://github.com/golang/go/issues/13271, with additions at the bottom of the file
package main
import (
"testing"
//
"reflect"
"unsafe"
@seanlinsley
seanlinsley / iterable_weak_set.js
Last active August 2, 2023 03:58
Iterable WeakSet in JavaScript
// spec: https://github.com/tc39/proposal-weakrefs
// the spec contains an [iterable WeakMap implementation](https://github.com/tc39/proposal-weakrefs#iterable-weakmaps)
// NOTE: this WeakSet implementation is incomplete, only does what I needed
// In Firefox Nightly, visit about:config and enable javascript.options.experimental.weakrefs
class IterableWeakSet extends Set {
add(el) {
super.add(new WeakRef(el))
}
forEach(fn) {
@seanlinsley
seanlinsley / chromedriver.rb
Created March 14, 2019 00:55
Ensures that chromedriver always matches the version of Chrome that's currently installed
# https://github.com/flavorjones/chromedriver-helper/issues/79
# http://chromedriver.storage.googleapis.com/index.html
require 'open-uri'
require 'shellwords'
executable = if RUBY_PLATFORM =~ /darwin/
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
else
'google-chrome'
# Sprockets no longer creates undigested assets, so we have to emulate the old behavior to keep vendored JS libraries happy.
# This automatically uses the newest asset in cases where the same file exists multiple times (like with Capistrano).
#
# https://github.com/rails/sprockets-rails/issues/49
#
# digested: 4-star-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.jpg
# undigested: 4-star.jpg
assets_without_digest = -> do
digest = /(-{1}[a-z0-9]{64}*\.{1}){1}/
assets = Dir.glob File.join Rails.root, 'public/assets/**/*'
@seanlinsley
seanlinsley / example_usage.js.erb
Last active July 10, 2021 03:13
Handlebars + ERB
Backbone.View.extend({
template: Handlebars.compile(
<%= embed('template.hbs').inspect %>
// some .hbs.erb files are also loaded
),
// ...
})
# written for https://github.com/rails/rails/pull/30919
# 1. find all gems that depend on a particular gem
# 2. download them all
# 3. search through their source!
auth = 'Authorization:YOUR_API_KEY'
gems = JSON.parse(`curl -H '#{auth}' https://rubygems.org/api/v1/gems/activerecord/reverse_dependencies.json`); nil
gems.size
,.---._
,,,, / `,
\\\\ / '\_ ;
|||| /\/``-.__\;'
::::/\/_
{{`-.__.-'(`(^^(^^^(^ 9 `.========='
{{{{{{ { ( ( ( ( (-----:=
{{.-'~~'-.(,(,,(,,,(__6_.'=========.
::::\/\
|||| \/\ ,-'/,
# This asks the user if they want a read-only or read-write PSQL session. PSQL
# provides `\prompt`, but that doesn't provide a way to conditionally prompt
# based on whether it was being run interactively, or from bash-completion.
#
# Save this file to ~/.psqlprompt and add the below to ~/.psqlrc:
#
# \set read_only `bash ~/.psqlprompt`
# set default_transaction_read_only = :read_only;
#
function psql_prompt() {
begin
# your code
rescue
tries ||= 0
(tries += 1) < 3 ? retry : raise
end