Skip to content

Instantly share code, notes, and snippets.


Keenan Brock kbrock

View GitHub Profile
kbrock / code.txt
Last active Oct 22, 2020
Converts from using a foreign relationship table to using a materialized path in the same table (using ancestry)
View code.txt
relationships = Relationship.where(relationship: 'genealogy', resource_type: 'VmOrTemplate').select(:id, :resource_id, :ancestry).to_a
rel_by_rel_id = relationships.index_by(:id) # relationship => resource_id
rel_by_vm_id = relationships.index_by(:resource_id) # resource_id => ancestry
Vm.where(:id => do |vm|
ancestry = rel_by_vm_id[].ancestry.split("/").map { |rel_id| rel_by_rel_id[rel_id].resource_id.to_s }.join("/")
vm.update_attribute(:ancestry => ancestry)
View a_invoke.bash
curl -X POST http://admin:smartvm@localhost:3000/api/service_catalogs/#{id}/service_templates/1 -d '{"action" : "order"}'
kbrock /
Last active May 3, 2020
keyboard info.json layout ideas for qmk

The [original] is modified in the following ways:

  • tweaked options to be {} not [] for json syntax reasons
  • added "R" for demonstrative purposes

Here are a few ideas. I tried to keep them as independent as possible:

  1. assume width, height, matrix, and display
  2. remove replaces 4. condense options want to keep nodes separate.
  3. assume, replaces, condense
kbrock / compare_vs_include.rb
Created Sep 26, 2019
Range.include was slow, running metrics
View compare_vs_include.rb
#!/usr/bin/env ruby
require "benchmark/ips"
require "date"
BEGIN_OF_JULY =, 7, 1)
END_OF_JULY =, 7, 31)
DAY_IN_JULY =, 7, 15)
kbrock /
Created May 30, 2018
Compare string to array
method nil speed str speed
?split:[] 9,761,148.9 i/s 2,869,972.9 i/s 3.40x
&.split||[] 9,271,343.0 i/s same 2,879,248.1 i/s 3.39x
&&split||[] 9,096,687.7 i/s same 2,941,303.8 i/s 3.32x
to_s.split 4,288,858.0 i/s 2.28x 2,822,293.7 i/s 3.46x
ruby -v
kbrock /
Last active May 30, 2018
Comparing Array blank vs empty
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
nil slower empty slower full slower
x&.empty? 11985357.7 i/s same 11617138.6 i/s same 11247144.5 i/s same
x.blank? 9909235.8 i/s 1.21x 10488507.9 i/s 1.14x 10418916.8 i/s 1.15x
x.nil? || x.empty? 10194622.1 i/s 1.18x 9584369.0 i/s 1.25x 9667811.7 i/s 1.24x
x.try!(:empty?) 5437472.5 i/s 2.20x 3493090.9 i/s 3.43x 3523153.2 i/s 3.40x
kbrock /
Last active Apr 26, 2018
Comparing performance implementation of determining whether a field is an integer (for ancestry

note: in the tests, we are setting a different variable @x than the guard clause @x2. I tested a terciary ?: vs the guard clause return if defined?() and they were about the same for pass and fail case

test int i/s slower text i/s slower
eq2 6,315,053.7 i/s same 6,323,409.7 i/s base
neq2 6,281,137.2 i/s same 3,707,761.0 i/s 1.71x
eq 5,978,924.2 i/s same 4,572,438.8 i/s 1.38x
include 4,368,299.8 i/s 1.45x 4,349,531.4 i/s 1.45x
neq 4,194,756.5 i/s 1.51x 4,170,358.7 i/s 1.52x

Looking at the difference between using ActiveSupport::Notification, and rolling our own metrics counters.

When logging, Notifications are 66% slower than manually adding the metrics code. The metrics code is actually 50% slower when not logging the results (no subscriber). Having no subscriber does speed up performance for all the cases.

The difference for timing for payload is probably due to allocations. Having a fixed payload ended up being equivalent to the no payload case. This is a reminder for us to pay attention to what we pass in the payload.

kbrock /
Last active Aug 29, 2017 — forked from aishfenton/serializer_benchmarks.rb
Performance comparison of different ruby serializer methods
Json oj 40.4 i/s
MessagePack 38.3 i/s same
binary json 22.0 i/s 1.84x slower
Json yajl 19.0 i/s 2.13x slower
ruby json 15.6 i/s 2.59x slower
ruby marshal 13.9 i/s 2.90x slower
ruby yaml 0.4 i/s 111.99x slower
Yaml Psych 0.4 i/s 112.09x slower
You can’t perform that action at this time.