Skip to content

Instantly share code, notes, and snippets.

View havenwood's full-sized avatar
:octocat:

Shannon Skipper havenwood

:octocat:
  • Ashland, Oregon
  • 01:22 (UTC -07:00)
View GitHub Profile
@havenwood
havenwood / sync_pipe.rb
Created March 23, 2025 04:18
An example refinement method like `IO.pipe`'s block form but with no buffering.
class IO
module UnbufferedPipe
refine IO.superclass do
def unbuffered_pipe(*, **)
IO.pipe(*, **) do |read_io, write_io|
read_io.sync = true
write_io.sync = true
yield read_io, write_io
end
@havenwood
havenwood / ppp
Created March 14, 2025 06:45
`ppp` is a zsh script for inspecting basic filename whitespace
#!/usr/bin/env -S zsh -e
help() {
print -u2 -- "Usage: $ZSH_ARGZERO [options] [directory...]
Options:
-h, --help Output this help message
-v, --version Output program name and version"
exit ${1:-0}
}
@havenwood
havenwood / wired
Created March 3, 2025 18:20
Adjust wired limits to allocate more memory to the GPU with Apple Silicon
#!/usr/bin/env -S zsh -e
zparseopts -D -F -- \
g:=opt_gb -reserve-gb:=opt_gb \
p:=opt_percent -reclaim-percent:=opt_percent \
h=opt_help -help=opt_help \
v=opt_version -version=opt_version
if (( ${#opt_help} )); then
print -- "Usage: $(basename $0) [options]
@havenwood
havenwood / factorial.rb
Last active March 5, 2025 04:59
An example Ruby unary method implementation of a Schönhage–Strassen optimized factorial
##
# This refinement uses a binary-splitting algorithm for fairly
# efficient factorial calculation with results cached by default.
#
# Caching dramatically improves performance when portions of the
# factorial calculation are repeated.
class UnaryFactorial < Module
class NegativeError < StandardError
def initialize(message = 'Factorials cannot be negative') = super
end
@havenwood
havenwood / map.rb
Created February 16, 2025 00:31
A Ractor-backed `Map` riffing on https://bugs.ruby-lang.org/issues/21121#note-1
class Ractor
module Channel
def self.new(&block)
channel = Ractor.new do
loop do
Ractor.yield Ractor.receive
end
end
return block.call(channel) if block_given?
@havenwood
havenwood / models.md
Last active March 12, 2025 22:39
LLM models high level overview

Models

In order to offload to GPU a model must fit in VRAM.

The following table lists models assuming Q4_K_M quantization. You can always use a model that fits in a smaller VRAM size.

VRAM Models
384GB - DeepSeek V3 671
128GB - Mistral Large 2411 123b
64GB - Qwen2.5 72b
@havenwood
havenwood / data_literal.rb
Created January 9, 2025 16:41
A `~{meaning: 42}`-style Data literal implemented in Ruby
module DataLiteral
refine Hash do
def to_data(name = nil)
if name
unless Object.const_defined?(name)
Object.const_set(name, Data.define(*keys))
end
Object.const_get(name).new(*values)
else
@havenwood
havenwood / install-ruby-3.4.1-mmtk.sh
Last active December 25, 2024 09:48
A little script to install Ruby 3.4.0 with the experimental MMTk garbage collector
#!/usr/bin/env zsh -e
version=3.4.1
brew install gmp jemalloc libffi libyaml openssl@3 readline zlib
command -v rustc >/dev/null 2>&1 || brew install rust
mkdir -p "$HOME/src"
cd "$HOME/src"
curl "https://cache.ruby-lang.org/pub/ruby/3.4/ruby-$version.tar.xz" | tar -x
cd "ruby-$version"
@havenwood
havenwood / integer.rb
Created December 2, 2024 16:52
A Ruby implementation of Integer#each_digit
class Integer
def each_digit(base = 10, &block)
raise Math::DomainError, 'out of domain' if negative?
raise TypeError, "wrong argument type #{base.class} (expected Integer)" unless base.respond_to?(:to_int)
radix = base.to_int
raise ArgumentError, 'negative radix' if radix.negative?
raise ArgumentError, "invalid radix #{radix}" if radix < 2
return enum_for(__method__, base) unless block_given?
@havenwood
havenwood / str.rb
Created November 13, 2024 21:55
Example for @isene on #ruby IRC
without = %w[b c]
without_regexp = Regexp.union(without)
split_index = 7
dropped = split_index.pred
str = 'abcdaba123abcd'
pos = str
.each_grapheme_cluster
.with_index
.lazy