Inflector.constantize uses eval which is slow in JRuby and Ruby 1.9
Reported by enebo | June 13th, 2008 @ 06:25 PM | in 2.1.3
constantize in lib/active_support/inflector.rb uses eval which is slow in both JRuby and Ruby 1.9. This patch passes all tests and also significantly speeds up constantize on these ruby impl (without hurting perf of Ruby 1.8).
Comments and changes to this ticket
-
Chris Lloyd August 7th, 2008 @ 12:24 AM
- → Tag changed from to patch
+1
Passes all tests. I've attached a benchmark which shows this is actually slightly faster with simple constant names running 1.8.6.
It slows down with multiple namespaces. Parity occurs with 3 nested namespaces (A::B::C) but the perf. difference on even up to 7 nested namespaces is minimal. Looks good.
-

Repository August 7th, 2008 @ 01:34 AM
- → State changed from new to resolved
(from [88eec8327b179cd41314e85868879b46bcf2530e]) JRuby: improve constantize performance. [#410 state:resolved] http://github.com/rails/rails/co...
-

Repository August 7th, 2008 @ 01:34 AM
(from [ed8a882e47e07b470b71cacd8cd50e251dca4d27]) JRuby: improve constantize performance. [#410 state:resolved] http://github.com/rails/rails/co...
-
Jeremy Kemper August 7th, 2008 @ 01:36 AM
- → Tag changed from patch to activesupport patch
- → Milestone changed from to 2.1.1
- → Assigned user changed from to Jeremy Kemper
-

Michael Johnston September 11th, 2008 @ 08:37 AM
This fails (after about 30 rounds of constantize / const_missing / parent)
@@@ruby class Outer TOM=5 end
module Bob class Mary
class Foo < ::Outer end def self.doit i = ::Bob::Mary::Foo.new puts "dereference: #{::Bob::Mary::Foo::TOM}" puts [constantize: #{](:Bob::Mary::Foo::TOM".constantize}") endend end
Bob::Mary.doit
for an example in the wild see the init function of picnic -

Michael Johnston September 11th, 2008 @ 08:38 AM
sigh. lets try that again.
class Outer TOM=5 end module Bob class Mary class Foo < ::Outer end def self.doit i = ::Bob::Mary::Foo.new puts "dereference: #{::Bob::Mary::Foo::TOM}" puts "constantize: #{"::Bob::Mary::Foo::TOM".constantize}" end end end Bob::Mary.doitfor an example in the wild see the init function of picnic
-

Michael Johnston September 11th, 2008 @ 08:51 AM
actually, this fails too:
class Outer2 module Why TOM2 = 5 end include Why end "Outer2::TOM2".constantizewhich is why "Logger::INFO".constantize fails which is (another reason) why picnic fails.
-
Jeremy Kemper September 11th, 2008 @ 05:08 PM
- → State changed from resolved to open
Michael, could you add a failing test case? Do you have a workaround that doesn't use eval?
-
Jeremy Kemper October 24th, 2008 @ 03:19 AM
- → Milestone changed from 2.1.1 to 2.1.3
-
Ripta Pasay December 3rd, 2008 @ 04:46 PM
It's been awhile, so attached is my own attempt a failing test case. I added an "Extension" module to Ace::Base; it's the simplest I could come up with that fails. The "Extension" doesn't break the rest of the test cases.
-
Frederick Cheung December 13th, 2008 @ 06:24 PM
This seems to do the trick, speed seems on par with the previous implementation. Not 100% sure about the ruby 1.9 specific changes, the inflector tests do still pass but I'm not quite up to date on how const_defined? etc.. have changed
-
Frederick Cheung December 14th, 2008 @ 10:08 AM
Oops, ruby 1.9 changes were a bit off. This should be better
-

Repository December 15th, 2008 @ 07:05 PM
- → State changed from open to resolved
(from [eca79e6bf052041c018c7ba08750238f8b2ecb7a]) Make constantize look into ancestors
[#410 state:resolved]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
Conflicts:
activesupport/lib/active_support/inflector.rb -

Repository December 15th, 2008 @ 07:05 PM
(from [262fef7ed57520b857605a0105fe7ba9265654f6]) Make constantize look into ancestors
[#410 state:resolved]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...
-

Repository December 15th, 2008 @ 07:05 PM
(from [87790e00ec4a0b24146de3757d1d6892689b05e4]) Make constantize look into ancestors
[#410 state:resolved]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net http://github.com/rails/rails/co...
-

Repository December 16th, 2008 @ 02:22 AM
- → State changed from resolved to open
(from [d7b7ff0556ea5a66aeda455141ba06b558671e3d]) Revert "Make constantize look into ancestors"
[#410 state:open]
This reverts commit 87790e00ec4a0b24146de3757d1d6892689b05e4. http://github.com/rails/rails/co...
-

Repository December 16th, 2008 @ 02:22 AM
(from [981314f45cbb42d1b5403ac0d984f37c82226438]) Revert "Make constantize look into ancestors"
[#410 state:open]
This reverts commit eca79e6bf052041c018c7ba08750238f8b2ecb7a. http://github.com/rails/rails/co...
-

Repository December 16th, 2008 @ 02:22 AM
(from [19be3d35b38b6685789d8d343617d465a3652717]) Revert "Make constantize look into ancestors"
[#410 state:open]
This reverts commit 262fef7ed57520b857605a0105fe7ba9265654f6. http://github.com/rails/rails/co...
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
Repository is at http://github.com/rails/rails
Check out the development master (Edge Rails):
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..
