This project is archived and is in readonly mode.
STI slow, result of ActiveRecord#find_sti_class should be cached!
Reported by thieso (at gmail) | August 18th, 2010 @ 04:07 PM | in 3.0.2
Hi,
after getting perftools.rb running with 1.9.2 and rails3rc i found that a lot of cycles are burned inside find_sti_class (actually compute_type). this is caused by looking up a constant fails to load:
base.rb:1141
# Build a list of candidates to search for
candidates = []
name.scan(/::|$/) { candidates.unshift "#{$`}::#{type_name}" }
candidates << type_name
class A < ActiveRecord::Base
end
class B < A
end
this will set candidates to ["A::B", "B"], now "A::B" will go though the long codepath of constantize - and it will everytime a B is instantiated from the database as the const A::B will never come into existance and we don't do negative caching.
my suggestion would be to add a class level cache to the find_sti_class method. that will improve performace of largish queries with sti.
i would write a patch myself, but failed to get the AR testsuite pass on my box.
re,
thies
Comments and changes to this ticket
-
thieso (at gmail) August 18th, 2010 @ 04:36 PM
- no changes were found...
-
thieso (at gmail) August 18th, 2010 @ 04:38 PM
- Tag changed from active_record, performance, sti to active_record, patch, performance, sti
-
Jeremy Kemper August 19th, 2010 @ 05:04 PM
- Milestone cleared.
- State changed from new to open
- Assigned user set to Jeremy Kemper
- Importance changed from to Low
Good idea. However, Thieso's patch will break in development mode since the memoized classes will be reloaded. We can use
ActiveSupport::Dependencies.constantize(name)
to memoize the lookup within the duration of a request. -
Jeremy Kemper August 19th, 2010 @ 05:59 PM
- State changed from open to resolved
-
Andrew White August 19th, 2010 @ 09:54 PM
We can speed this up by another 30% by optimizing the case where store_full_sti_class is true. This has been the default for some time now so should be the case for the vast majority of apps.
-
Repository August 19th, 2010 @ 10:03 PM
(from [ad063263bcaffb97f3e8d625d24c19d51b95b839]) Optimize find_sti_class when store_full_sti_class is true
[#5403]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/ad063263bcaffb97f3e8d625d24c19... -
Repository August 19th, 2010 @ 10:03 PM
(from [0b73f2af0e08c21ff75199505804993160117452]) Optimize find_sti_class when store_full_sti_class is true
[#5403]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/0b73f2af0e08c21ff7519950580499...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>