From: tenderlove@... Date: 2015-12-03T21:43:24+00:00 Subject: [ruby-core:71818] [Ruby trunk - Feature #11769] [Open] optimize case / when for `nil` Issue #11769 has been reported by Aaron Patterson. ---------------------------------------- Feature #11769: optimize case / when for `nil` https://bugs.ruby-lang.org/issues/11769 * Author: Aaron Patterson * Status: Open * Priority: Normal * Assignee: ---------------------------------------- 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-------------------------------- 0001-optimize-case-when-for-nil.patch (3.4 KB) -- https://bugs.ruby-lang.org/