Skip to content

Commit 039e6ea

Browse files
committed
Backport caching of the constant lookup
1 parent c2565ed commit 039e6ea

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

lib/active_model/serializable/utils.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ module Utils
44
extend self
55

66
def _const_get(const)
7-
begin
8-
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
9-
Object.send method, const
10-
rescue NameError
11-
const.safe_constantize
7+
Serializer.serializers_cache.fetch_or_store(const) do
8+
begin
9+
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
10+
Object.send method, const
11+
rescue NameError
12+
const.safe_constantize
13+
end
1214
end
1315
end
1416
end
1517
end
16-
end
18+
end

lib/active_model/serializer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'active_model/serializer/config'
55

66
require 'thread'
7+
require 'concurrent/map'
78

89
module ActiveModel
910
class Serializer
@@ -100,6 +101,10 @@ def has_many(*attrs)
100101
associate(Association::HasMany, *attrs)
101102
end
102103

104+
def serializers_cache
105+
@serializers_cache ||= Concurrent::Map.new
106+
end
107+
103108
private
104109

105110
def strip_attribute(attr)

lib/active_model_serializers.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
ActionController::Base.send(:include, ::ActionController::Serialization)
1515
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
1616
end
17+
ActionDispatch::Reloader.to_prepare do
18+
ActiveModel::Serializer.serializers_cache.clear
19+
end
1720
end
1821
rescue LoadError
1922
# rails not installed, continuing

0 commit comments

Comments
 (0)