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

Reduce object creation during substitution #2274

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

Conversation

ksss
Copy link
Collaborator

@ksss ksss commented Feb 12, 2025

The #sub method generates many objects, but in most classes, substitution does not occur at all. By modifying it so that no objects are created when there are no variables to substitute, we will optimize performance.

A side effect is that #sub no longer guarantees that a new object will be created.

Benchmark

# validate.rb

require 'rbs'
require 'rbs/cli'
# require 'memory_profiler'

# MemoryProfiler.report do
  loader = RBS::EnvironmentLoader.new
  Dir["stdlib/*"].each do |path|
    lib = File.basename(path).to_s
    loader.add(library: lib, version: nil)
  end
  options = Data.define(:loader).new(loader:)
  RBS::CLI::Validate.new(args: [], options:).run
# end.pretty_print
$ time bundle exec ruby validate.rb

Before

bundle exec ruby validate.rb  1.22s user 0.13s system 86% cpu 1.555 total
bundle exec ruby validate.rb  1.23s user 0.13s system 86% cpu 1.574 total
bundle exec ruby validate.rb  1.23s user 0.13s system 86% cpu 1.576 total

After

bundle exec ruby validate.rb  1.00s user 0.13s system 82% cpu 1.374 total
bundle exec ruby validate.rb  1.00s user 0.13s system 84% cpu 1.334 total
bundle exec ruby validate.rb  1.00s user 0.13s system 84% cpu 1.335 total

With memory_profiler

Before

Total allocated: 309209155 bytes (3277248 objects)
Total retained:  191993 bytes (3441 objects)

After

Total allocated: 190730219 bytes (1904943 objects)
Total retained:  191993 bytes (3441 objects)

The `#sub` method generates many objects, but in most classes, substitution does not occur at all.
By modifying it so that no objects are created when there are no variables to substitute, we will optimize performance.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant