From: Joseph Jones Date: 2015-12-17T21:06:00-07:00 Subject: [ruby-core:72251] [Ruby trunk - Feature #11769] [Open] optimize case / when for `nil` --56738628_6b68079a_16c Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Joseph Jones liked your message with Boxer. On December 3, 2015 at 14:43:= 24 MST, tenderlove=40ruby-lang.org wrote:Issue =2311769 has been reported= by Aaron Patterson.----------------------------------------=46eature =23= 11769: optimize case / when for =60nil=60https://bugs.ruby-lang.org/issue= s/11769* Author: Aaron Patterson* Status: Open* Priority: Normal* Assigne= e: ----------------------------------------Hi,I've noticed that when ther= e are certain values in a case / when statement it gets optimized to a ha= sh lookup. =46or example, code like this:=7E=7E=7Edef foo socket case str= =3D socket.read=5Fnonblock(10, exception: false) when :wait=5Freadable =23= do something else str endendputs RubyVM::InstructionSequence.of(method(:= foo)).disasm=7E=7E=7EThe above code will use =60opt=5Fcase=5Fdispatch=60 = instruction with a hash. However, if I use =60nil=60 in the case statemen= t like this:=7E=7E=7Edef foo socket case str =3D socket.read=5Fnonblock(1= 0, exception: false) when :wait=5Freadable =23 do something when nil =23 = got an EO=46 else str endendputs RubyVM::InstructionSequence.of(method(:f= oo)).disasm=7E=7E=7EThen the optimization is lost.I've attached a patch t= hat adds =60nil=60 to the optimized case such that the above code will us= e =60opt=5Fcase=5Fdispatch=60. My patch defines =60=3D=3D=3D=60 on =60nil= =60, then adds nil to the list of =22optimizable literals=22.---=46iles--= ------------------------------0001-optimize-case-when-for-nil.patch (3.4 = KB)-- https://bugs.ruby-lang.org/ --56738628_6b68079a_16c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Joseph Jones liked your message with Boxer.


= On December 3, 2015 at 14:43:24 MST, tenderlove=40ruby-lang.org wrote:
Issue =2311769 has been = reported by Aaron Patterson.

---------------------------------= -------
=46eature =2311769: optimize case / when for =60nil=60
= https://bugs.ruby-lang.org/issues/11769

* Author: Aaron Patter= son
* Status: Open
* Priority: Normal
* Assignee:
--= --------------------------------------
Hi,

I've noticed t= hat when there are certain values in a case / when statement it gets opti= mized to a hash lookup. =46or example, code like this:

=7E=7E= =7E
def foo socket
case str =3D socket.read=5Fnonblock(10, ex= ception: false)
when :wait=5Freadable
=23 do something else
str
end
end

puts RubyVM::Instru= ctionSequence.of(method(:foo)).disasm
=7E=7E=7E

The above= code will use =60opt=5Fcase=5Fdispatch=60 instruction with a hash. Howe= ver, if I use =60nil=60 in the case statement like this:

=7E=7E= =7E
def foo socket
case str =3D socket.read=5Fnonblock(10, ex= ception: false)
when :wait=5Freadable
=23 do something when nil
=23 got an EO=46
else
str
e= nd
end

puts RubyVM::InstructionSequence.of(method(:foo)).= disasm
=7E=7E=7E

Then the optimization is lost.

I've attached a patch that adds =60nil=60 to the optimized case such th= at the above code will use =60opt=5Fcase=5Fdispatch=60. My patch defines= =60=3D=3D=3D=60 on =60nil=60, then adds nil to the list of =22optimizabl= e literals=22.

---=46iles--------------------------------
0001-optimize-case-when-for-nil.patch (3.4 KB)


--
https://bugs.ruby-lang.org/
--56738628_6b68079a_16c--