Module: StrokeDB::ClassOptimization::ClassMethods

Defined in:
lib/strokedb/util/class_optimization.rb

Instance Method Summary collapse

Instance Method Details

#deoptimize!(lang) ⇒ Object

Reverts method optimization done with optimize! Note: you may call this method only after optimize! was called.



54
55
56
57
58
# File 'lib/strokedb/util/class_optimization.rb', line 54

def deoptimize!(lang)
  optimized_methods(lang).each do |meth|
    alias_method(:"#{meth}", :"#{meth}_PureRuby")
  end
end

#optimize!(lang) ⇒ Object

Switches methods into optimized versions as declared in declare_optimized_methods. Pure ruby methods become accessible with suffix _PureRuby.



41
42
43
44
45
46
47
48
49
# File 'lib/strokedb/util/class_optimization.rb', line 41

def optimize!(lang)
  if block = @optimized_methods_init[lang.to_s]
    self.instance_eval(&block)
  end
  optimized_methods(lang).each do |meth|
    alias_method(:"#{meth}_PureRuby", :"#{meth}")
    alias_method(:"#{meth}",          :"#{meth}_#{lang}")
  end
end

#optimized_with(lang) ⇒ Object

Executes code in a block with optimizations turned on. This ensures that appropriate deoptimize! method is called.



63
64
65
66
67
68
# File 'lib/strokedb/util/class_optimization.rb', line 63

def optimized_with(lang)
  optimize!(lang)
  yield
ensure
  deoptimize!(lang)
end

#with_optimizations(*langs) {|"pure Ruby"| ... } ⇒ Object

Iterates through all the optimizations. Non-optimized mode (“pure Ruby”) is yielded first. Useful for testing and benchmarks.

Example:

Klass.with_optimizations(:InlineC) do |lang|
  puts "Klass#some_method is written in #{lang}"
  puts Klass.new.some_method
end

Yields:

  • ("pure Ruby")


81
82
83
84
85
86
87
88
89
90
# File 'lib/strokedb/util/class_optimization.rb', line 81

def with_optimizations(*langs)
  langs.flatten!
  yield("pure Ruby")
  langs.each do |lang|
    optimized_with(lang) do
      yield(lang)
    end
  end
  self
end