Skip to content

Instantly share code, notes, and snippets.

@thedarkone
thedarkone / gist:3645105
Created September 5, 2012 21:27
Ruby volatile APIs
# using accessors:
attr_volatile :table, :size_control
def check_for_resize
while (current_table = table) && MAX_CAP > (table_size = current_table.size) && RESIZING != (size_ctrl = size_control) && size_ctrl < @counter.sum
try_in_resize_lock(current_table, size_ctrl) do
self.table = rebuild(current_table)
(table_size << 1) - (table_size >> 1) # 75% load factor
end
@thedarkone
thedarkone / gist:3668383
Created September 7, 2012 18:27
This is not thread safe even on MRI
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
h = {:a => start}
h[:a] += bignum # get into the Bignum territory
iterations = 100_000
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
class H < Hash
def [](key)
super if key.kind_of?(Symbol)
end
An exception occurred running at_exit handlers
no block given (LocalJumpError)
Backtrace:
ERROR: the VM is exiting improperly running at_exit handlers
intended operation: :exception
associated value: nil
destination scope: unknown
An exception occurred
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
def recur(n)
if n == 0
bench
else
recur(n-1)
end
end
def bench