Issue #7035 has been updated by luislavena (Luis Lavena).


=begin
I like the idea.

Considering recent changes like (({respond_to?})) and protected methods, I think this change is not introducing a big backward incompatible change. Most of the code out in the wild uses (({defined?})) to check for definitions, not considering the result value at all.
=end

----------------------------------------
Feature #7035: defined? should return cached, frozen strings
https://bugs.ruby-lang.org/issues/7035#change-29460

Author: headius (Charles Nutter)
Status: Open
Priority: Normal
Assignee: 
Category: core
Target version: 2.0.0


Yehuda and I have been looking into allocation rates in Rails under both MRI and JRuby, and one of the big standouts is defined? logic returning a new String every time (for success cases). We could think of no reason why defined? needs to return a new String, and neither of us know of any code in the wild that takes the resulting string and modifies it.

For systems that use defined? heavily, it would seem best to only ever return the same instance of a cached, frozen String, rather than a new String every time that is only used for its boolean-ness and thrown away. Eliminating these extra Strings would reduce allocation and GC burden on MRI, with only the tiniest behavioral change nobody will ever notice.

An alternative with a larger behavioral change would be to have defined? always return Symbol, rather than String. The additional danger here is if anyone is using the String result as a String for comparison purposes, which I have definitely seen in the wild.

In any case, I could come up with no justification for returning a new String every time, so I have implemented the cached, frozen logic in JRuby: https://github.com/jruby/jruby/commit/b03d0bc89aefca13deaff7a568e5d9118a9ca2a8


-- 
http://bugs.ruby-lang.org/