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

V0 11 dev performance #1431

Merged
merged 9 commits into from
Jan 16, 2024
Merged

V0 11 dev performance #1431

merged 9 commits into from
Jan 16, 2024

Conversation

lgebhardt
Copy link
Member

Implements some caching of frequently computed values. Significantly reduces allocations and run times for larger requests.

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

@lgebhardt lgebhardt requested a review from bf4 January 15, 2024 17:47
@@ -39,7 +39,7 @@ def _model
end

def id
_model.public_send(self.class._primary_key)
@id ||= _model.public_send(self.class._primary_key)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Off topic-- we'll want to handle composite primary keys here

Copy link
Member Author

Choose a reason for hiding this comment

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

That's an interesting point. I don't see anything in the spec to provide support for a multi attribute composite key in the JSONAPI section on identification. We could attempt to support this in JR by combining the composite key components, but I suspect it's going to run into the same issues I encountered trying to find a way to support an alternate public key field (one different from the primary key in the database). It was a harder problem than I initially thought it would be.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I figure someone could also just override this method. Since keys need to be consistently ordered, a custom synthetic key would be fine and keep the risk of a spec error off of jsonapi.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that's the way I would handle it too. To work with rails we would need an identity class to convert the composite string keys to an array and back. It would an interesting thing to test.

@@ -1218,6 +1218,14 @@ def _clear_cached_attribute_options
@_cached_attribute_options = {}
end

def _clear_cached_resource_types_for_model
@_cached_resource_types_for_model = {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

No sure how allocations differ re garbage collection to set a new ivar vs calling clear

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure either. I assume, probably a bad thing to do, that a Set or Has would be cleaned up the same as the values it holds when no longer referenced. But I took a look at the code and reworked how the class variables for the caches are initialized and cleared. Probably doesn't matter much since this is code that's only run at startup. Still I hope it makes it a little clearer.

@lgebhardt lgebhardt requested a review from bf4 January 16, 2024 15:29
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 2668b6b into v0-11-dev Jan 16, 2024
39 checks passed
@lgebhardt lgebhardt deleted the v0-11-dev-performance branch January 16, 2024 19:45
lgebhardt added a commit that referenced this pull request Apr 18, 2024
* Reduce number of string allocations in LinkBuilder

* Consistently access `include_related`

* Remove unused class variable

* Cache `id` after retrieving it from the model

* Cache `module_path`

* Cache resource_klass_for and resource_type_for

* Remove no longer used method _setup_relationship

* Delete nil values without creating a new object

* Rework resource naming for method caches
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