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 memory usage of RBS::Buffer #907

Merged

Conversation

pocke
Copy link
Member

@pocke pocke commented Feb 20, 2022

This PR reduces memory usage of RBS::Buffer.

RBS::Buffer setups @lines and @ranges on initialize, but they aren't used in many cases and they consume large memory.
This patch makes the initialization lazy to avoid allocating memory.

It doesn't affect rbs command speed, but it reduces memory usage.

Benchmarking

It reduces 8,275kb (17%) memory only with the core libraries, and reduces 33,718kb (24%) memory with rails gems.

With core libraries

$LOAD_PATH << File.join(__dir__, "./lib")
require 'rbs'
require 'rbs/cli'

def rss
  `ps u --no-headers --pid #{Process.pid}`.split(' ')[5].to_i
end

l = RBS::EnvironmentLoader.new
x = x = RBS::Environment.from_loader(l).resolve_type_names
GC.start
p rss

before: 48690 (kb)
after: 40415 (kb)

With rails gems

$LOAD_PATH << File.join(__dir__, "./lib")
require 'rbs'
require 'rbs/cli'

def rss
  `ps u --no-headers --pid #{Process.pid}`.split(' ')[5].to_i
end

l = RBS::EnvironmentLoader.new
path = Pathname('/path/to/rbs_collection.yaml')
lock = RBS::Collection::Config.lockfile_of(path)
l.add_collection lock
x = x = RBS::Environment.from_loader(l).resolve_type_names
GC.start
p rss

before: 139662 (kb)
after: 105944 (kb)

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.

👍

(The methods are used by Location if the instance is not created during parsing.)

@pocke pocke merged commit 026d73d into ruby:master Feb 21, 2022
@pocke pocke deleted the Improve_performance_of_creation_RBS__Buffer branch February 21, 2022 02:59
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.

2 participants