Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource identity comparable #1430

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

lgebhardt
Copy link
Member

Fixes #1429

add tests for ResourceIdentity, including that comparison does not allocate memory

All Submissions:

  • I've checked to ensure there aren't other open Pull Requests for the same update/change.
  • I've submitted a ticket for my issue if one did not already exist.
  • My submission passes all tests. (Please run the full test suite locally to cut down on noise from travis failures.)
  • I've used Github auto-closing keywords in the commit message or the description.
  • I've added/updated tests for this change.

New Feature Submissions:

  • I've submitted an issue that describes this feature, and received the go ahead from the maintainers.
  • My submission includes new tests.
  • My submission maintains compliance with JSON:API.

Bug fixes and Changes to Core Features:

  • I've included an explanation of what the changes do and why I'd like you to include them.
  • I've provided test(s) that fails without the change.

Test Plan:

Reviewer Checklist:

  • Maintains compliance with JSON:API
  • Adequate test coverage exists to prevent regressions

add tests for ResourceIdentity, including that comparison does not allocate memory
@lgebhardt lgebhardt requested a review from bf4 January 13, 2024 17:51
@@ -41,7 +43,16 @@ def hash
end

def <=>(other_identity)
self.id <=> other_identity.id
return nil unless other_identity.is_a?(ResourceIdentity)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparsble includes some of the above methods. Is the override here intended?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea is we have a composite key and need to ensure we sort consistently even when given a polymorphic list of ResourceIdentities. This logic places grouping by type as a higher priority than by id. I chose that logic since it will seem more natural when ids are either UUIDs or integers.

Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method between?.

I could remove the override of == as well, which might be more performant than the hash call. I'm going to test this and see what makes the most sense. I wish I had focused on including Comparable earlier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My gut was wrong and it like I should keep the override of == that uses hash as it's about twice as fast in the most common case where the resource klasses are the same:

>> require 'benchmark'
=> false
>> # Using hash method
>> rid1 = JSONAPI::ResourceIdentity.new(Image, 13); rid2 = JSONAPI::ResourceIdentity.new(Image, 13); puts Benchmark.measure { 1_000_000.times { rid1 == rid2 } }
  0.194055   0.000052   0.194107 (  0.195783)

>> # Using new <=> logic prioritizing resource_klass - Case where they are equal
>> rid1 = JSONAPI::ResourceIdentity.new(Image, 13); rid2 = JSONAPI::ResourceIdentity.new(Image, 13); puts Benchmark.measure { 1_000_000.times { rid1 <=> rid2 } }
  0.398467   0.000156   0.398623 (  0.401471)

>> # Using new <=> logic prioritizing resource_klass - Case where resource klasses differ, which should only do one comparison (best case)
>> rid1 = JSONAPI::ResourceIdentity.new(Image, 13); rid2 = JSONAPI::ResourceIdentity.new(User, 13); puts Benchmark.measure { 1_000_000.times { rid1 <=> rid2 } }
  0.330682   0.000103   0.330785 (  0.332534)

Copy link
Member Author

@lgebhardt lgebhardt Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tested removing the check on other_identity.is_a?(ResourceIdentity) and it only saved a small amount of time:

>> rid1 = JSONAPI::ResourceIdentity.new(Image, 13); rid2 = JSONAPI::ResourceIdentity.new(Image, 13); puts Benchmark.measure { 1_000_000.times { rid1 <=> rid2 } }
  0.379685   0.000151   0.379836 (  0.383279)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job testing!

@lgebhardt lgebhardt requested a review from bf4 January 15, 2024 15:59
Copy link
Collaborator

@bf4 bf4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@lgebhardt lgebhardt merged commit 0bbbc0b into v0-11-dev Jan 16, 2024
39 checks passed
@lgebhardt lgebhardt deleted the v0-11-dev-resource-identity-comparable branch January 16, 2024 12:50
lgebhardt added a commit that referenced this pull request Apr 18, 2024
add tests for ResourceIdentity, including that comparison does not allocate memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants