Skip to content

Commit cd1a529

Browse files
authored
Store the resource_klass and id in an array for efficiency (#1428)
Removes the need to allocate a new array for every comparison
1 parent 040f980 commit cd1a529

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lib/jsonapi/resource_identity.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ module JSONAPI
1313
# rid = ResourceIdentity.new(PostResource, 12)
1414
#
1515
class ResourceIdentity
16-
attr_reader :resource_klass, :id
17-
16+
# Store the identity parts as an array to avoid allocating a new array for the hash method to work on
1817
def initialize(resource_klass, id)
19-
@resource_klass = resource_klass
20-
@id = id
18+
@identity_parts = [resource_klass, id]
19+
end
20+
21+
def resource_klass
22+
@identity_parts[0]
23+
end
24+
25+
def id
26+
@identity_parts[1]
2127
end
2228

2329
def ==(other)
@@ -27,11 +33,11 @@ def ==(other)
2733
end
2834

2935
def eql?(other)
30-
other.is_a?(ResourceIdentity) && other.resource_klass == @resource_klass && other.id == @id
36+
hash == other.hash
3137
end
3238

3339
def hash
34-
[@resource_klass, @id].hash
40+
@identity_parts.hash
3541
end
3642

3743
def <=>(other_identity)

0 commit comments

Comments
 (0)