From: ko1@... Date: 2015-12-07T07:02:36+00:00 Subject: [ruby-core:71872] [Ruby trunk - Feature #11769] optimize case / when for `nil` Issue #11769 has been updated by Koichi Sasada. Assignee set to Koichi Sasada ---------------------------------------- Feature #11769: optimize case / when for `nil` https://bugs.ruby-lang.org/issues/11769#change-55274 * Author: Aaron Patterson * Status: Open * Priority: Normal * Assignee: Koichi Sasada ---------------------------------------- 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/