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

Fix single rel caching issue. #1712

Closed

Conversation

huseynov206
Copy link
Contributor

Fixes #

This pull introduces/changes:

  • Resolved an issue where single relationships were not being cached accurately, leading to performance inefficiencies.

@@ -111,13 +111,17 @@ def init_cache

def add_to_cache(object, rel = nil)
(@cached_rels ||= []) << rel if rel
(@cached_result ||= []).tap { |results| results << object if object && !results.include?(object) }
(@cached_result ||= []).tap { |results| results << object if !results.include?(object) } if object
Copy link
Member

Choose a reason for hiding this comment

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

do you realize that this change is not equivalent? If object is nil then @cached_result will not be initialized if nil, while it was before. However this will make a difference only in case of a call with only nil parametesr add_to_cache(nil, nil).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a reason why I changed this code. After adding the rel function to AssociationProxy, I noticed that the following case is working incorrectly.

role = Role.find(xxx)
role.manager.source_rel
role.manager.source

Even though the manager has a source, if we call source after calling source_rel, it returns nil. Because we used the add_to_cache method in line has_n.rb:118, @cached_result was initialized as an empty array then in line has_n.rb:81 we check @cached_result, since it is initialized we use it, i.e. the empty array. To avoid this, I made @cached_result initialize only when the object exists. All specs and manual tests passed successfully.

  • Object parameter is nil only when we use the add_to_cache method for rels.

@codecov-commenter
Copy link

codecov-commenter commented Dec 28, 2023

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (3c89aef) 93.43% compared to head (a77a77e) 43.26%.

❗ Current head a77a77e differs from pull request most recent head 1443ca8. Consider uploading reports for the commit 1443ca8 to get more accurate results

Files Patch % Lines
lib/active_graph/node/has_n.rb 33.33% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #1712       +/-   ##
===========================================
- Coverage   93.43%   43.26%   -50.17%     
===========================================
  Files         128      106       -22     
  Lines        5909     4946      -963     
===========================================
- Hits         5521     2140     -3381     
- Misses        388     2806     +2418     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

3 participants