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/refactor a few memoizations #4945

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

coalest
Copy link
Collaborator

@coalest coalest commented Jan 24, 2025

Description

I noticed that the current_organization helper method wasn't actually memoizing (it was never setting a @current_organization instance variable).

Also noticed a few places where we were attempting to memoize, but not actually memoizing when the return value was falsey.

Type of change

  • Internal

How Has This Been Tested?

Automated test suite

Screenshots

@cielf cielf requested a review from dorner January 24, 2025 19:21

return current_role.resource if current_role&.resource&.is_a?(Organization)
return @current_organization = nil unless current_role
return @current_organization = current_role.resource if current_role&.resource&.is_a?(Organization)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we split up the assignment and return statements? Generally not a fan of one line doing two things.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea that's fair, I was trying to keep the same code structure, but it isn't very clean like this.

@@ -15,30 +15,31 @@ class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found!

def current_organization
return @current_organization if @current_organization
return nil unless current_role
return @current_organization if defined? @current_organization
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes me feel woozy... in general there being a difference between "defined but nil" and "not defined" seems very hand-wavy. I'd rather use a sentinel value (e.g. @current_organization = :no_org).

Another option is to use something like https://github.com/panorama-ed/memo_wise/ - or you can reuse the approach it uses, which is to have a top-level instance variable e.g. @memoized_results = {} and only set a result once it's attempted to be used, and make use of key? instead of comparing against nil.

Copy link
Collaborator Author

@coalest coalest Jan 29, 2025

Choose a reason for hiding this comment

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

I don't know, I think using the defined? keyword (Ruby docs) is fairly common practice when memoizing falsey values. It's a pattern I've used before and I see it recommended in most places.

I took a look at a few implementations of memoization gems. Looks like memoist uses Object#instance_variable_defined?, a more specific version of defined? in order to memoize falsey values (source). memo_wise uses Hash#fetch to memoize falsey values (because that method differentiates between a key returning nil/false and a key not existing) (source).

Rails also uses this pattern (return @instance_variable if defined? @instance_variable) in a few places (source).

I think the hash approach of memo_wise makes sense when you are calling the method with arguments, so that you can hash results for different arguments passed in. But for our use cases (only memoizing methods with no parameters), it's just adding extra overhead.

I think adding another dependency to memoize ~5 methods is overkill, but it's up to you. If you say use a sentinel value or adding a gem I can do that. More important for me is that we actually memoize falsey values. (For example, in the partners portal, current_organization can evaluate to nil and can be called multiple times resulting in multiple db queries.)

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