You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(I call ActiveRecord::Base.last method in benchmark because only first call to csv_report is slow)
Profiling shows that 98% of time is spent in Paperclip::Storage::Database#setup_attachment_class method.
Here is its implementation:
defsetup_attachment_classinstance.class.ancestors.eachdo |ancestor|
# Pick the top-most definition like# Paperclip::AttachmentRegistry#definitions_fornames_for_ancestor=ancestor.attachment_definitions.keysrescue[]ifnames_for_ancestor.member?(name)@attachment_class=ancestorendendend
This method is doing something very very very nasty. It iterates over all ancestors (which in typical rails application returns collection with size above 70 elements) and it call attachment_definitions method. Of course most of the elements don't have this method so for most of them there will be raise error. Raising and rescuing errors in ruby are relatively slow so they shouldn't be used for normal flow.
One call takes ~0.08s - this is huge! It would be better to check if ancestor respond to this method before calling it:
puts Benchmark.measure {
ActiveRecord::Base.ancestors.each do |ancestor|
next unless ancestor.respond_to?(:attachment_definitions)
# call ancestor.attachment_definitions
end
}
0.000000 0.000000 0.000000 ( 0.000169)
Also there is no need to call keys method in setup_attachment_class method. attachment_definitions returns hash and you can call has_key? method directly on hash without creating intermediate Arra (also Hash#has_key? has O(1) complexity and Array#member? O(N)).
The text was updated successfully, but these errors were encountered:
I noticed big speed regression for generated #attachment getter. Here are benchmarks comparing paperclip_database version 2.2.2 and 2.3.1:
2.2.2
2.3.1
(I call ActiveRecord::Base.last method in benchmark because only first call to csv_report is slow)
Profiling shows that 98% of time is spent in
Paperclip::Storage::Database#setup_attachment_class
method.Here is its implementation:
This method is doing something very very very nasty. It iterates over all ancestors (which in typical rails application returns collection with size above 70 elements) and it call
attachment_definitions
method. Of course most of the elements don't have this method so for most of them there will be raise error. Raising and rescuing errors in ruby are relatively slow so they shouldn't be used for normal flow.Here is some more usefull metrics:
One call takes ~0.08s - this is huge! It would be better to check if ancestor respond to this method before calling it:
Also there is no need to call
keys
method insetup_attachment_class
method.attachment_definitions
returns hash and you can callhas_key?
method directly on hash without creating intermediate Arra (also Hash#has_key? has O(1) complexity and Array#member? O(N)).The text was updated successfully, but these errors were encountered: