Skip to content

Commit

Permalink
Store the resource_klass and id in an array for efficiency (#1428)
Browse files Browse the repository at this point in the history
Removes the need to allocate a new array for every comparison
  • Loading branch information
lgebhardt committed Apr 18, 2024
1 parent 18e150c commit c61110f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/jsonapi/resource_identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ module JSONAPI
# rid = ResourceIdentity.new(PostResource, 12)
#
class ResourceIdentity
attr_reader :resource_klass, :id

# Store the identity parts as an array to avoid allocating a new array for the hash method to work on
def initialize(resource_klass, id)
@resource_klass = resource_klass
@id = id
@identity_parts = [resource_klass, id]
end

def resource_klass
@identity_parts[0]
end

def id
@identity_parts[1]
end

def ==(other)
Expand All @@ -27,11 +33,11 @@ def ==(other)
end

def eql?(other)
other.is_a?(ResourceIdentity) && other.resource_klass == @resource_klass && other.id == @id
hash == other.hash
end

def hash
[@resource_klass, @id].hash
@identity_parts.hash
end

def <=>(other_identity)
Expand Down

0 comments on commit c61110f

Please sign in to comment.