globalize/globalize

_read_attribute override causes 20% slowdown of whole application

dla-c-box opened this issue · 1 comments

def _read_attribute(attr_name, options = {}, &block)

(^^from this problematic PR from 2018, and further modified by other PRs)

A long time ago (this was already in Rails 4), there has been a performance improvement in Rails on a critical method (called on all model attribute access, e.g. a simple call like “customer.id”): rails/rails@08576b9
image

I believe Globalize's override of _read_attribute is only needed if some ID fields are translated? If that's the case, maybe adding a configuration to opt-out of that feature, which causes a massive slowdown in DB intensive applications, would be relevant. (the configuration would prevent defining _read_attribute altogether)

In the mean time, in our fork, we're probably going to delete your version of read_attribute and _read_attribute and replace it with this single method (which brings us back to the performance we had before Globalize's override of _read_attribute):

def read_attribute(name, options = nil, &block)
  return super(name, &block) unless translated?(name) && options&.send(:[], :translated) != false && (value = globalize.fetch(options&.send(:[], :locale) || Globalize.locale, name))

  value
end
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.