4. Uncertainty
/* ruby/struct.c */
static VALUE rb_struct_equal(VALUE s, VALUE s2) {
/* ... */
if (RSTRUCT_LEN(s) != RSTRUCT_LEN(s2)) {
rb_bug("inconsistent struct"); /* should never happen */
}
/* ... */
}
O RLY?!
5. How could this code fail?
class ExpensiveToCreate
def self.instance
@instance ||= ExpensiveToCreate.new
end
end
6. How could this code fail?
class ExpensiveToCreate
def self.instance
unless defined?(@instance)
@instance = ExpensiveToCreate.new
end
end
end
7. How could this code fail?
class ExpensiveToCreate
def self.instance
T2 unless defined?(@instance)
@instance = ExpensiveToCreate.allocate
T1 @instance.initialize # big pause here
@instance
end
end
end
8. Avoid shared mutable state,
lazy initialization
MutableConstant.merge! :key => value
class MemoryCache
def self.cache
@cache ||= {} $global_lock.do_something!
end
end
10. data = []
M.times do |m|
Thread.new do
N.times do |n|
data << m * n
end
end
end
11. OK NOT OK
instance vars
method def
String
class def
Array
class vars
Hash
12. class WorkerTask
def run
@thread = Thread.new do
50.times do
digest = Digest::MD5.new
@range.step(1024) do |idx|
digest.update(@data[idx...idx+1024])
end
end
end
end
end
20. require 'akka'
class HelloWord
def hi
puts "hello actor world"
end
end
Actors.actorOf(HelloWord.new).hi
puts "initiating shutdown..."
Actors.delayedShutdown 1