Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / drive_irb.cr
Last active December 1, 2024 19:54
Driving IRB with Crystal
#!/usr/bin/env crystal
reader, writer = IO.pipe
# Write your script to drive IRB here
spawn do
sleep 1.second
writer.puts "1+1"
sleep 1.second
@jgaskins
jgaskins / text_to_timestamp.sql
Created November 15, 2024 17:01
Parse Postgres TIMESTAMPs from JSON blobs
CREATE FUNCTION text_to_timestamp(text) RETURNS TIMESTAMP
LANGUAGE sql IMMUTABLE AS
$$
SELECT CASE
WHEN $1 ~ '^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?$' THEN
CAST($1 AS timestamp without time zone)
END
$$;
CREATE TABLE t (
@jgaskins
jgaskins / env_types.rb
Last active November 10, 2024 02:11
Type coercion for ENV
class << ENV
def integer(key)
if value = self[key]
value.to_i
end
end
def boolean(key)
case value = self[key]
when /true/i
@jgaskins
jgaskins / 0-output.txt
Created September 5, 2024 20:01
Optimized Phlex-like components with Armature
➜ armature git:(master) ✗ crystal run --release bench/code_component.cr
<div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div><div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div><div class="users"><h2>Fox Mulder message</h2><p id="user-Fox Mulder"><span class="title">they:</span><span class="message">nested hello Fox Mulder</span></p></div>
name 929.18k ( 1.08µs) (± 0.87%) 96.0B/op fastest
@jgaskins
jgaskins / 0-results.txt
Last active September 5, 2024 19:24
Benchmarking Phlex-like components in Armature
➜ armature git:(master) ✗ crystal run --release bench_code_component.cr
name 815.75k ( 1.23µs) (± 5.66%) 336B/op fastest
@jgaskins
jgaskins / 0-results.txt
Last active June 13, 2024 04:31
Benchmark pattern matching in Crystal using Arrays and Tuples
➜ Code crystal run --release bench_tuple_vs_array_for_pattern_matching.cr
true
array 27.17k ( 36.80µs) (± 1.58%) 156kB/op 96.02× slower
tuple 2.61M (383.32ns) (± 0.85%) 0.0B/op fastest
[true]
@jgaskins
jgaskins / Cargo.toml
Last active May 28, 2024 04:42
Comparing Crystal and Rust with HTTP, JSON, and Redis
[package]
name = "post-example"
version = "0.1.0"
edition = "2021"
[dependencies]
rocket = { version = "*", features = ["json"] }
uuid = { version = "0.8", features = ["v4", "serde"] }
chrono = "0.4"
redis = "*"
@jgaskins
jgaskins / rack-request_stats.gemspec
Last active May 16, 2024 13:12
Rack request stats
#!/usr/bin/env ruby
%w(lib shared).each do |dir|
$LOAD_PATH.unshift(__dir__) unless $LOAD_PATH.include?(__dir__)
end
Gem::Specification.new do |spec|
spec.name = "rack-request_stats"
spec.version = "0.1.0"
spec.authors = ["Jamie Gaskins"]
@jgaskins
jgaskins / active_support-executor-example.rb
Last active May 1, 2024 22:36
ActiveSupport::Executor example
require "active_support/executor"
require "active_record"
require "pg"
ActiveRecord::Base.establish_connection(
uri: "postgres:///",
adapter: :postgresql,
pool: 5,
)
@jgaskins
jgaskins / 0.output.txt
Created March 9, 2024 23:12
Minimal Redis cache implementation in pure Ruby, outperforming Hiredis by 2-3x
$ ruby -v bench_redis.rb
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
KEY : bench-redis
VALUE SIZE: 102400
ITERATIONS: 10000
GET
Rehearsal -------------------------------------------