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

Performance of aasm_ensure_initial_state (end Rails 3 support?) #838

Open
sergioisidoro opened this issue Sep 26, 2023 · 1 comment
Open

Comments

@sergioisidoro
Copy link

sergioisidoro commented Sep 26, 2023

I was debugging the performance of a large query serialisation (read query) , and I stumbled upon something curious. There is significant amount of time being spent in AASM::Persistence::ActiveRecordPersistence.

Screenshot 2023-09-26 at 10 32 47

Upon investigation, it seems that most of the time is being spent on attribute models. It seems to be coming from aasm_ensure_initial_state and aasm_column_is_blank, listing all attribute names.

I found a comment that this method is not using responds_to because of Rails 3 support. Given that it has been 10 years, I wonder if it we could consider migrating to responds_to, and if it would make it a bit more performant? After all in our case, we only have one AASM field in a very large model.

        def aasm_ensure_initial_state
          AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
            # checking via respond_to? does not work in Rails <= 3
            # if respond_to?(self.class.aasm(state_machine_name).attribute_name) && send(self.class.aasm(state_machine_name).attribute_name).blank? # Rails 4
            if aasm_column_is_blank?(state_machine_name)
              aasm(state_machine_name).enter_initial_state
            end
          end
        end

        def aasm_column_is_blank?(state_machine_name)
          attribute_name = self.class.aasm(state_machine_name).attribute_name
          attribute_names.include?(attribute_name.to_s) &&
            (send(attribute_name).respond_to?(:empty?) ? !!send(attribute_name).empty? : !send(attribute_name))
        end
@sergioisidoro
Copy link
Author

Here's a Zoomed in section of the flamegraph:
Screenshot 2023-09-26 at 10 42 55

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

No branches or pull requests

1 participant