Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Value of Being Lazy

Erik Berlin
November 24, 2015

The Value of Being Lazy

…or How I Made OpenStruct 10X Faster

Presented at Rails Israel 2015.

Erik Berlin

November 24, 2015
Tweet

More Decks by Erik Berlin

Other Decks in Programming

Transcript

  1. THE VALUE OF BEING LAZY
 or How I Made OpenStruct

    10X Faster Erik Michaels-Ober @sferik
  2. You can use classes to create new objects: object =

    Object.new
 object.class #=> Object
  3. You can use classes to create new classes: klass =

    Class.new
 klass.class #=> Class
  4. Usually, we create classes like this: class Point attr_accessor :x,

    :y def initialize(x, y) @x, @y = x, y end end
  5. In this way, OpenStruct is similar to Hash: point =

    Hash.new point[:x] = 1
 point[:y] = 2
  6. You can even initialize OpenStruct with a Hash: point =

    OpenStruct.new(x: 1, y: 2) point.x #=> 1
 point.y #=> 2
  7. require "benchmark/ips"
 Point = Struct.new(:x, :y) def struct Point.new(0, 1)

    end
 def ostruct OpenStruct.new(x: 0, y: 1) end
 Benchmark.ips do |x| x.report("ostruct") { ostruct } x.report("struct") { struct } end
  8. def initialize(hash = nil) @table = {} if hash hash.each_pair

    do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end
  9. def new_ostruct_member(name) name = name.to_sym unless respond_to?(name) define_singleton_method(name) { @table[name]

    } define_singleton_method("#{name}=") { |x| @table[name] = x } end name end
  10. def method_missing(mid, *args) len = args.length if mname = mid[/.*(?==\z)/m]

    @table[new_ostruct_member(mname)] = args[0] elsif len == 0 if @table.key?(mid) new_ostruct_member(mid) @table[mid] end end end
  11. def initialize(hash = nil) @table = {} if hash hash.each_pair

    do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end end
  12. lazy_integers = (1..Float::INFINITY).lazy lazy_integers.collect { |x| x ** 2 }.

    select { |x| x.even? }. reject { |x| x < 1000 }. first(5) #=> [1024, 1156, 1296, 1444, 1600]
  13. require "prime" lazy_primes = Prime.lazy lazy_primes.select { |x| (x -

    2).prime? }. collect { |x| [x - 2, x] }. first(5) #=> [[3, 5], [5, 7], [11, 13], [17, 19], [29, 31]]
  14. module Enumerable def repeat_after_first unless block_given? return to_enum(__method__) { size

    * 2 - 1 if size } end each.with_index do |*val, index| index == 0 ? yield *val : 2.times { yield *val } end end end
  15. require "prime" lazy_primes = Prime.lazy lazy_primes.repeat_after_first. each_slice(2). select { |x,

    y| x + 2 == y }. first(5) #=> [[3, 5], [5, 7], [11, 13], [17, 19], [29, 31]]