-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Optimised performance for attribute extraction #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
*.swp | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "rubygems" | ||
require "bundler/setup" | ||
require "active_model_serializers" | ||
require "active_support/json" | ||
require "benchmark" | ||
|
||
class User < Struct.new(:id,:name,:age,:about) | ||
include ActiveModel::SerializerSupport | ||
|
||
def fast_hash | ||
h = { | ||
id: read_attribute_for_serialization(:id), | ||
name: read_attribute_for_serialization(:name), | ||
about: read_attribute_for_serialization(:about) | ||
} | ||
h[:age] = read_attribute_for_serialization(:age) if age > 18 | ||
h | ||
end | ||
end | ||
|
||
class UserSerializer < ActiveModel::Serializer | ||
attributes :id, :name, :age, :about | ||
|
||
def include_age? | ||
object.age > 18 | ||
end | ||
end | ||
|
||
|
||
|
||
u = User.new(1, "sam", 10, "about") | ||
s = UserSerializer.new(u) | ||
|
||
n = 100000 | ||
|
||
Benchmark.bmbm {|x| | ||
x.report("init") { n.times { UserSerializer.new(u) } } | ||
x.report("fast_hash") { n.times { u.fast_hash } } | ||
x.report("attributes") { n.times { UserSerializer.new(u).attributes } } | ||
x.report("serializable_hash") { n.times { UserSerializer.new(u).serializable_hash } } | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ def attribute(attr, options={}) | |
end | ||
|
||
define_include_method attr | ||
|
||
end | ||
|
||
def associate(klass, attrs) #:nodoc: | ||
|
@@ -281,13 +282,9 @@ def as_json(options={}) | |
# object without the root. | ||
def serializable_hash | ||
return nil if @object.nil? | ||
instrument(:serialize, :serializer => self.class.name) do | ||
@node = attributes | ||
instrument :associations do | ||
include_associations! if _embed | ||
end | ||
@node | ||
end | ||
@node = attributes | ||
include_associations! if _embed | ||
@node | ||
end | ||
|
||
def include_associations! | ||
|
@@ -378,13 +375,19 @@ def merge_association(hash, key, serializables, unique_values) | |
# Returns a hash representation of the serializable | ||
# object attributes. | ||
def attributes | ||
hash = {} | ||
_fast_attributes | ||
rescue NameError | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why rescue a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. faster, cause the general case it will not get the NameError, NameError only happens on first call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, just pushed the fix |
||
method = "def _fast_attributes\n" | ||
|
||
_attributes.each do |name,key| | ||
hash[key] = read_attribute_for_serialization(name) if include?(name) | ||
end | ||
method << " h = {}\n" | ||
|
||
_attributes.each do |name,key| | ||
method << " h[:#{key}] = read_attribute_for_serialization(:#{name}) if send #{INCLUDE_METHODS[name].inspect}\n" | ||
end | ||
method << " h\nend" | ||
|
||
hash | ||
self.class.class_eval method | ||
_fast_attributes | ||
end | ||
|
||
# Returns options[:scope] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha, this is great, I have it in my global .gitignore as well as have vim configured to save these files elsewhere. :)