Skip to content

Instantly share code, notes, and snippets.

Avatar

Danny Guinther tdg5

View GitHub Profile
@tdg5
tdg5 / README.md
Last active Nov 2, 2015
Patch to install Ruby with tail call optimization enabled by default
View README.md

Installation with RVM:

  $ rvm install 2.2.3 --patch https://gist.githubusercontent.com/tdg5/0fba32242ccc39e71577/raw/b0d869e006761c74dac4ed9dbb779a56b424b63c/ruby_tco.patch
@tdg5
tdg5 / cassandra_cleanup.java
Last active Oct 30, 2015
Various bits of the cassandra cleanup process code
View cassandra_cleanup.java
/*
https://github.com/apache/cassandra/blob/cassandra-2.0/src/java/org/apache/cassandra/db/compaction/CompactionManager.java#L523
*/
private void doCleanupCompaction(final ColumnFamilyStore cfs, Collection<SSTableReader> sstables, CounterId.OneShotRenewer renewer) throws IOException
{
assert !cfs.isIndex();
Keyspace keyspace = cfs.keyspace;
Collection<Range<Token>> ranges = StorageService.instance.getLocalRanges(keyspace.getName());
if (ranges.isEmpty())
{
@tdg5
tdg5 / tail_positions.rb
Created Oct 12, 2015
Script to explore in which situations tail call elimination can occur in Ruby
View tail_positions.rb
require "tco_method"
# Wrap method definitons in a lambda to facilitate recompiling with tail call
# optimization enabled.
tail_calls = lambda do |this|
def start_tail_call_chain
implicit_tail_call
end
def implicit_tail_call
@tdg5
tdg5 / unary_ampersand_keeping_refs.rb
Last active Oct 7, 2015
Example seeming to demonstrate that unary ampersand operator holds on to obj references in Ruby 2.1.x
View unary_ampersand_keeping_refs.rb
# Create some special classes to facilitate tracking allocated objects.
class TrackedArray < Array; end
class TrackedString < String; end
STRANG = "a" * 5000
class ClingyObjects
def generate(should_cling = false)
strs = TrackedArray.new
30000.times { strs << TrackedString.new(STRANG) }
@tdg5
tdg5 / gc_friendly_tco.rb
Created Oct 7, 2015
Example demonstrating that tail-call optimization in MRI Ruby allows for earlier garbage collection of unreachable objects.
View gc_friendly_tco.rb
# Create some special classes to facilitate tracking allocated objects.
class TrackedArray < Array; end
class TrackedString < String; end
STRANG = "a" * 5000
# Other than a few extra escapes, the code below can be understood more or less
# as is. Any weirdness is to facilitate interpretting with/without tail-call
# optimization in a DRY manner.
KLASS = <<-CODE
class GcFriendlyTco
@tdg5
tdg5 / better_oo_through_tco.rb
Last active Sep 30, 2015
Better Object-Oriented Design Through... Tail-Call Optimization?
View better_oo_through_tco.rb
# Abstract UnionList base class
class AbstractUnionList
private :initialize
def length
how_many?(0)
end
def how_many?(counter)
raise NotImplementedError
View pry_hooks.rb
%w[
after_read
before_eval
after_eval
when_started
before_session
after_session
].each do |hook|
Pry.config.hooks.add_hook(hook.to_sym, "my_#{hook}_hook") do |_, _, _|
puts "hello from #{hook}"
@tdg5
tdg5 / partially_implemented_interface.rb
Created Apr 15, 2015
PartiallyImplementedInterface mixin
View partially_implemented_interface.rb
module PartiallyImplementedInterface
def self.not_implemented_error(klass, method)
raise NotImplementedError, not_implemented_message(klass, method), caller(2)
end
def self.not_implemented_message(klass, method_name)
"#{method_name} must be implemented by subclasses of #{klass.name}!"
end
private_class_method :not_implemented_message
@tdg5
tdg5 / itr_pbt_deep_dup_build.rb
Last active Aug 29, 2015
iterative pbt_deep_dup_build
View itr_pbt_deep_dup_build.rb
def self.pbt_deep_dup_build(clone, original)
mapped = Set.new
edges = [[clone, original]]
while edges.any?
builder, twin = edges.shift
pair_hash = "#{builder.class.name}:#{builder.hash}~#{twin.hash}".hash
next if mapped.include?(pair_hash)
mapped << pair_hash
pbt_dup_build(builder, twin)
@tdg5
tdg5 / tco_ivar.rb
Created Mar 21, 2015
Instance variable reference from tail recursive call
View tco_ivar.rb
require "tco_method"
class TCOInstance
extend TCOMethod::Mixin
attr_reader :tco_cycles
def initialize
@tco_cycles = 0
end
You can’t perform that action at this time.