Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@sergioisidoro

Description

@sergioisidoro

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions