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: reduce UnboundMethod objects by memoizing initialize_parameters #1868

Merged
merged 3 commits into from
Oct 19, 2023

Conversation

rainerborene
Copy link
Collaborator

@rainerborene rainerborene commented Oct 10, 2023

I have just discovered a simple optimization on ViewComponent::Base that can reduce UnboundMethod objects allocation. I have removed other objects from this memory allocation report comparison to keep things simple and clear. These are the results in one of our main Rails endpoints.

Before:

allocated memory by class
-------------------------
4080  UnboundMethod

After:

allocated memory by class
-------------------------
1280  UnboundMethod

If you want to test this patch yourself, just do this on your ApplicationComponent:

class ApplicationComponent < ViewComponent::Base
  private
    def self.initialize_parameters
      @initialize_parameters ||= super
    end
end

@reeganviljoen
Copy link
Collaborator

@rainerborene can you please add your change to the changelog while waiting on a review

@rainerborene rainerborene force-pushed the reduce_unbound_allocations branch from 64c1929 to f5d3a40 Compare October 11, 2023 12:44
@Spone
Copy link
Collaborator

Spone commented Oct 11, 2023

I'm a bit unsure if adding memorization here can have unwanted effects. Mind chiming in @camertron?

@camertron
Copy link
Contributor

Cool, thanks for looking into this @rainerborene :)

The only issue I can think of would be if the #initialize method gets redefined via class_eval or something. It's possible in such a scenario that the memoized list of parameters would be out-of-date by the time Base tries to find the collection parameter. However I can't imagine that sort of thing is very common, so I think this change is ok 👍

@camertron
Copy link
Contributor

I'm on leave right now @Spone, would you or another maintainer be able to look into the Vale CI failure and merge this?

@Spone
Copy link
Collaborator

Spone commented Oct 12, 2023

Yes I will, thanks for your input!

@camertron
Copy link
Contributor

camertron commented Oct 18, 2023

I thought a bit more about this, and I'm now convinced the memoization probably won't affect detecting collection parameters. Any component that renders a collection should already have defined the collection parameter in its initializer. Virtually any monkeypatch or redefinition of the #initialize method will need to specify that argument as well, so there should be no danger in memoizing the argument list. There could still be a problem if .with_collection_parameter is called after memoization, which changes the name of the initialization parameter Base looks for. I think we can mitigate issues by setting @initialize_parameters to nil in .with_collection_parameter.

@rainerborene rainerborene force-pushed the reduce_unbound_allocations branch from 755df13 to 1d48d29 Compare October 19, 2023 08:33
@camertron camertron merged commit c39f969 into ViewComponent:main Oct 19, 2023
21 checks passed
claudiob pushed a commit to claudiob/view_component that referenced this pull request Dec 22, 2023
claudiob pushed a commit to claudiob/view_component that referenced this pull request Jan 3, 2024
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.

4 participants