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

[prototype runtime] Optimize performance #1495

Merged
merged 1 commit into from
Sep 5, 2023

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented Sep 4, 2023

Problem

When the number of modules/classes is large and the path is deep, there is a problem that the performance of rbs prototype runtime is extremely degraded.
This is due to the loop of calling const_name for all modules to find their names each time the paths are nested.
This situation can easily occur in Rails applications and is worth addressing.

Solution

To suppress this loop, I modified @modules to be Hash to suppress the search cost.

Comparison Code

$ cat t.rb
module Root
  10.times do |i|
    const_set("A_#{i}", Module.new{
      10.times do |j|
        const_set("B_#{i}_#{j}", Module.new{
          10.times do |k|
            const_set("C_#{i}_#{j}_#{k}", Module.new{
              10.times do |l|
                const_set("D_#{i}_#{j}_#{k}_#{l}", Class.new)
              end
            })
          end
        })
      end
    })
  end
end

$ time bundle exec rbs prototype runtime -R t.rb "Root*" > /dev/null

# before
101.24s user 1.69s system 99% cpu 1:43.11 total

# after
1.05s user 0.14s system 89% cpu 1.330 total

Improve performance in large environments with more than 10,000 modules/classes by `Hash`
@ksss ksss changed the title Optimize performance [prototype runtime] Optimize performance Sep 4, 2023
Copy link
Member

@soutaro soutaro left a comment

Choose a reason for hiding this comment

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

🚄

@soutaro soutaro added this pull request to the merge queue Sep 5, 2023
@soutaro soutaro added this to the RBS 3.3 milestone Sep 5, 2023
Merged via the queue into ruby:master with commit a804a9c Sep 5, 2023
23 checks passed
@ksss ksss deleted the runtime-performance branch September 5, 2023 02:08
@soutaro soutaro added the Released PRs already included in the released version label Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Released PRs already included in the released version
Development

Successfully merging this pull request may close these issues.

2 participants