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

Store the resource_klass and id in an array for efficiency #1428

Merged
merged 1 commit into from
Jan 13, 2024

Conversation

lgebhardt
Copy link
Member

Removes the need to allocate a new array for every comparison.

Found a large number of allocations in the hash method when profiling the code. This removes about 35K allocations from a result set that contained about 3500 resources (1000 primary with 2500 includes). Not surprisingly that doesn't actually make a statistically significant performance issue for small requests like the one tested, but every little bit helps.

Also changes the eql? to use the hash method

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

Removes the need to allocate a new array for every comparison
@lgebhardt lgebhardt requested a review from bf4 January 12, 2024 22:11
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

end

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

def <=>(other_identity)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting that eql? and <=> diverge in what they compare but is out of scope to this change

Copy link
Member Author

Choose a reason for hiding this comment

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

For <=> it really should be comparing the resource_klasses first and then the ids second to determine the response. I think that would only affect the ordering of identities in the related identities when we're using a SortedSet and storing identities for a polymorphic relationship. It looks like that comparison can be done without any allocations so it's probably a good idea to make the change, but I'll handle that in a separate PR.

@lgebhardt lgebhardt merged commit cd1a529 into v0-11-dev Jan 13, 2024
39 checks passed
@lgebhardt lgebhardt deleted the v0-11-dev-reduce-resource_identity_allocations branch January 13, 2024 16:06
lgebhardt added a commit that referenced this pull request Apr 18, 2024
Removes the need to allocate a new array for every comparison
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