Actions
Feature #11769
closedoptimize case / when for `nil`
Description
Hi,
I've noticed that when there are certain values in a case / when statement it gets optimized to a hash lookup. For example, code like this:
def foo socket
case str = socket.read_nonblock(10, exception: false)
when :wait_readable
# do something
else
str
end
end
puts RubyVM::InstructionSequence.of(method(:foo)).disasm
The above code will use opt_case_dispatch
instruction with a hash. However, if I use nil
in the case statement like this:
def foo socket
case str = socket.read_nonblock(10, exception: false)
when :wait_readable
# do something
when nil
# got an EOF
else
str
end
end
puts RubyVM::InstructionSequence.of(method(:foo)).disasm
Then the optimization is lost.
I've attached a patch that adds nil
to the optimized case such that the above code will use opt_case_dispatch
. My patch defines ===
on nil
, then adds nil to the list of "optimizable literals".
Files
Actions
Like0
Like0Like0Like0Like0