Skip to content

Instantly share code, notes, and snippets.

@bdotdub
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdotdub/10953302 to your computer and use it in GitHub Desktop.
Save bdotdub/10953302 to your computer and use it in GitHub Desktop.
This don't make nooo sense. In theory, it should be almost semantically equivalent, `Futurable1` making only one more access to `Celluloid::Actor[:Futurable1]`. The `||=` is essentially checking for `nil` before assigning. Why it's such a big difference, I'd love to know.
require "benchmark"
require "celluloid"
class Futureable1
include Celluloid
class << self
def p
if Celluloid::Actor[:Futureable1].nil?
Celluloid::Actor[:Futureable1] = Futureable1.pool(size: 2)
end
Celluloid::Actor[:Futureable1]
end
end
def get
sleep 0.5
end
end
require "benchmark"
require "celluloid"
class Futureable2
include Celluloid
class << self
def p
Celluloid::Actor[:Futureable2] ||= Futureable2.pool(size: 2)
end
end
def get
sleep 0.5
end
end
Benchmark.measure { 50.times { Futureable1.p.async.get } }.real
# => 12.045974
 
Benchmark.measure { 50.times { Futureable2.p.async.get } }.real
# => 0.002454
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment