Skip to content

Commit 3b8cc74

Browse files
committed
Cache computed attribute options
Merging the default and the attribute specific options was resulting in a lot of memory allocations
1 parent ef6fb96 commit 3b8cc74

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

lib/jsonapi/basic_resource.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ def inherited(subclass)
453453

454454
subclass._routed = false
455455
subclass._warned_missing_route = false
456+
457+
subclass._clear_cached_attribute_options
456458
end
457459

458460
def rebuild_relationships(relationships)
@@ -527,6 +529,8 @@ def attributes(*attrs)
527529
end
528530

529531
def attribute(attribute_name, options = {})
532+
_clear_cached_attribute_options
533+
530534
attr = attribute_name.to_sym
531535

532536
check_reserved_attribute_name(attr)
@@ -826,7 +830,7 @@ def verify_relationship_filter(filter, raw, _context = nil)
826830

827831
# quasi private class methods
828832
def _attribute_options(attr)
829-
default_attribute_options.merge(@_attributes[attr])
833+
@_cached_attribute_options[attr] ||= default_attribute_options.merge(@_attributes[attr])
830834
end
831835

832836
def _attribute_delegated_name(attr)
@@ -1107,6 +1111,10 @@ def register_relationship(name, relationship_object)
11071111
@_relationships[name] = relationship_object
11081112
end
11091113

1114+
def _clear_cached_attribute_options
1115+
@_cached_attribute_options = {}
1116+
end
1117+
11101118
private
11111119

11121120
def check_reserved_resource_name(type, name)

0 commit comments

Comments
 (0)