Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / 000-highlights.md
Last active May 28, 2021 02:28
Benchmarking Redis drivers: Ruby gem, Hiredis, and a custom Ruby implementation

Drivers

Name Description
ruby The redis gem with the plain-Ruby driver
hiredis The redis gem with the hiredis driver (compiled as a C extension)
myredis Pure-Ruby, optimized for the specific operation

Benchmarks

@jgaskins
jgaskins / benchmark.rb
Last active June 25, 2022 14:54
Benchmarking redis gem vs hiredis vs custom Redis client
require 'bundler/inline'
require 'socket'
class MyRedis
CRLF = "\r\n"
def initialize
@connection = TCPSocket.new('localhost', 6379)
@connection.sync = false
end
@jgaskins
jgaskins / app.cr
Created January 18, 2021 05:31
Roda-like routing mixin for Crystal
require "http"
require "./route"
class App
include HTTP::Handler
include Route
def call(context)
route context do |r, response|
@jgaskins
jgaskins / bench_allocations.cr
Created December 3, 2020 04:54
Benchmarking allocation time of Crystal objects with various quantities of instance variables
require "benchmark"
value = nil
Benchmark.ips do |x|
x.report "1 ivar" { value = OneIVar.new }
x.report "2 ivars" { value = TwoIVars.new }
x.report "4 ivars" { value = FourIVars.new }
x.report "8 ivars" { value = EightIVars.new }
x.report "16 ivars" { value = SixteenIVars.new }
end
@jgaskins
jgaskins / bench_parse_int.cr
Created June 28, 2020 19:26
Compare parsing an int as intermediate strings vs using a byte parser directly on the IO
require "benchmark"
int = 0
[1, 12, 1234, 12345678, 1234567812345678].each do |number|
puts "Parsing #{number}..."
io = IO::Memory.new
io << ":#{number}\r\n" # Redis integer format
io.rewind
Benchmark.ips do |x|
@jgaskins
jgaskins / redis.cr
Created May 28, 2020 14:24
Simple Redis implementation in Crystal
require "socket"
module Redis
alias Type = String | Int64 | Nil | Array(Type)
class Future
property! value : Type
end
module Commands
@jgaskins
jgaskins / redis_ext.cr
Created May 28, 2020 00:20
Adding stream support to Crystal Redis
class Redis
module Commands
def xadd(stream, id, params : Hash)
params = params.each_with_object(Array(String).new(params.size * 2)) do |(key, value), array|
array << key << value.to_s
end
string_command(["XADD", stream, id] + params)
end
def xrange(stream, start, finish)
@jgaskins
jgaskins / event_handlers.js
Created January 31, 2020 14:45
Managing your own DOM event handlers in JS
/* Data structure to hold our event handlers. It's important that when you
remove an element from the DOM that you delete its associated event
handler id to avoid memory leaks.
{
12345: { // DOM element's event handler id
click: [
function(event) { ... },
]
},
@jgaskins
jgaskins / fetch.cypher
Created November 17, 2019 04:39
Fetching a remote user
// The $url is passed in as the only parameter to this query. The query
// handles everything else.
CALL apoc.load.jsonParams($url, {Accept: 'application/json'}, null, null, {}) YIELD value AS data
// Upsert the person into the DB, using the :Person label in case they aren't
// a fully-fledged account yet
MERGE (person:Person { id: data.id })
ON CREATE SET
person.created_at = datetime()
@jgaskins
jgaskins / set_operations.rb
Created October 13, 2019 19:38
SQL result set operations for ActiveRecord
module SetOperations
def union_scope(*scopes)
apply_operation :UNION, scopes
end
def intersect_scope(*scopes)
apply_operation :INTERSECT, scopes
end
def except_scope(*scopes)