Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created September 7, 2014 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainopia/eac2ba731015fdbc9382 to your computer and use it in GitHub Desktop.
Save brainopia/eac2ba731015fdbc9382 to your computer and use it in GitHub Desktop.
# rails 3.X patch to remove extra allocations on field access
module ActiveRecord::AttributeMethods::Read::ClassMethods
def internal_attribute_access_code(attr_name, cast_code)
access_code = "(v=@attributes[attr_name]) && #{cast_code}"
unless attr_name == primary_key
access_code.insert(0, "missing_attribute(attr_name, caller) unless @attributes.has_key?(attr_name); ")
end
if cache_attribute?(attr_name)
access_code = "@attributes_cache[attr_name] ||= (#{access_code})"
end
# this line has been changed
"attr_name = @@attr_#{attr_name} ||= '#{attr_name}'; #{access_code}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment