mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Merge pull request #233 from SamSaffron/fix
Optimised performance for attribute extraction
This commit is contained in:
commit
6408b73e3c
1
.gitignore
vendored
1
.gitignore
vendored
@ -15,3 +15,4 @@ spec/reports
|
|||||||
test/tmp
|
test/tmp
|
||||||
test/version_tmp
|
test/version_tmp
|
||||||
tmp
|
tmp
|
||||||
|
*.swp
|
||||||
|
|||||||
5
Rakefile
5
Rakefile
@ -10,4 +10,9 @@ Rake::TestTask.new(:test) do |t|
|
|||||||
t.verbose = true
|
t.verbose = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc 'Benchmark'
|
||||||
|
task :bench do
|
||||||
|
load 'bench/perf.rb'
|
||||||
|
end
|
||||||
|
|
||||||
task :default => :test
|
task :default => :test
|
||||||
|
|||||||
43
bench/perf.rb
Normal file
43
bench/perf.rb
Normal file
@ -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 } }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -96,6 +96,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
define_include_method attr
|
define_include_method attr
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def associate(klass, attrs) #:nodoc:
|
def associate(klass, attrs) #:nodoc:
|
||||||
@ -284,13 +285,9 @@ module ActiveModel
|
|||||||
# object without the root.
|
# object without the root.
|
||||||
def serializable_hash
|
def serializable_hash
|
||||||
return nil if @object.nil?
|
return nil if @object.nil?
|
||||||
instrument(:serialize, :serializer => self.class.name) do
|
@node = attributes
|
||||||
@node = attributes
|
include_associations! if _embed
|
||||||
instrument :associations do
|
@node
|
||||||
include_associations! if _embed
|
|
||||||
end
|
|
||||||
@node
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def include_associations!
|
def include_associations!
|
||||||
@ -381,13 +378,19 @@ module ActiveModel
|
|||||||
# Returns a hash representation of the serializable
|
# Returns a hash representation of the serializable
|
||||||
# object attributes.
|
# object attributes.
|
||||||
def attributes
|
def attributes
|
||||||
hash = {}
|
_fast_attributes
|
||||||
|
rescue NameError
|
||||||
|
method = "def _fast_attributes\n"
|
||||||
|
|
||||||
_attributes.each do |name,key|
|
method << " h = {}\n"
|
||||||
hash[key] = read_attribute_for_serialization(name) if include?(name)
|
|
||||||
end
|
|
||||||
|
|
||||||
hash
|
_attributes.each do |name,key|
|
||||||
|
method << " h[:#{key}] = read_attribute_for_serialization(:#{name}) if send #{INCLUDE_METHODS[name].inspect}\n"
|
||||||
|
end
|
||||||
|
method << " h\nend"
|
||||||
|
|
||||||
|
self.class.class_eval method
|
||||||
|
_fast_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns options[:scope]
|
# Returns options[:scope]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user