From 2d2c982d8ac714a52fd382283e51fe1ee67e44e8 Mon Sep 17 00:00:00 2001 From: lgebhardt Date: Fri, 12 Jan 2024 16:47:50 -0500 Subject: [PATCH] Store the resource_klass and id in an array for efficiency Removes the need to allocate a new array for every comparison --- lib/jsonapi/resource_identity.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/jsonapi/resource_identity.rb b/lib/jsonapi/resource_identity.rb index 74fae1aa..9466ba0c 100644 --- a/lib/jsonapi/resource_identity.rb +++ b/lib/jsonapi/resource_identity.rb @@ -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) @@ -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)