Merge pull request #2079 from dylanahsmith/backport-serializers-cache

Backport caching of the constant lookup
This commit is contained in:
Rafael França 2017-03-15 17:45:47 -04:00 committed by GitHub
commit 1e04d1128b
4 changed files with 18 additions and 2 deletions

View File

@ -21,5 +21,6 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = ">= 1.9.3"
gem.add_dependency "activemodel", ">= 3.2"
gem.add_dependency "concurrent-ruby", "~> 1.0"
gem.add_development_dependency "rails", ">= 3.2"
end

View File

@ -33,7 +33,11 @@ module ActiveModel
end
def namespace
get_namespace && Utils._const_get(get_namespace)
if module_name = get_namespace
Serializer.serializers_cache.fetch_or_store(module_name) do
Utils._const_get(module_name)
end
end
end
def embedded_in_root_associations

View File

@ -4,6 +4,7 @@ require 'active_model/serializer/association'
require 'active_model/serializer/config'
require 'thread'
require 'concurrent/map'
module ActiveModel
class Serializer
@ -65,7 +66,10 @@ end
ArraySerializer
end
else
_const_get build_serializer_class(resource, options)
klass_name = build_serializer_class(resource, options)
Serializer.serializers_cache.fetch_or_store(klass_name) do
_const_get(klass_name)
end
end
end
@ -100,6 +104,10 @@ end
associate(Association::HasMany, *attrs)
end
def serializers_cache
@serializers_cache ||= Concurrent::Map.new
end
private
def strip_attribute(attr)

View File

@ -14,6 +14,9 @@ begin
ActionController::Base.send(:include, ::ActionController::Serialization)
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
end
ActionDispatch::Reloader.to_prepare do
ActiveModel::Serializer.serializers_cache.clear
end
end
rescue LoadError
# rails not installed, continuing