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

Don't lazily retrieve gem specs for completion #433

Merged
merged 1 commit into from
Nov 7, 2022

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Nov 6, 2022

There are a few downsides of the current approach:

  1. Because gem specs are lazily retrieved, this computation happens in every irb completion test case, which is not necessary. (In tests we don't cache the result of retrieve_files_to_require_from_load_path)
  2. Gem::Specification.latest_specs is sensible to the content of LOAD_PATH. And when combined with 1, tests fail "randomly" if they try to mutate LOAD_PATH, even though the test subject it's something else.

So by pre-computing and storing the gem paths in a constant, it guarantees that the computation only happens once and it doesn't get affected by test cases.

One argument could be made against the change is that, it'll store unnecessary data for users that disable autocompletion. But the counter-arguments are:

  1. Since autocompletion is enabled by default, this should not be the case for most users.
  2. For users with autocompletion enabled, IRB already caches the result of retrieve_files_to_require_from_load_path in memory, which should have a similar size of GEM_SPECS. And we currently haven't received any report about problems caused by such memory consumption.

Testing With Ruby

This script:

$ make test-all -j8 TESTS=../test/irb/ TESTOPTS="--no-retry --seed=31291"

can reproduce the related test failures with Ruby:

# Running tests:


  1) Error:
TestIRB::TestCompletion::TestRequireComepletion#test_complete_require_with_string_convertable_in_load_path:
Test::Unit::ProxyError: no implicit conversion of Object into String
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems.rb:364:in `paths'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems.rb:408:in `path'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/specification.rb:965:in `dirs'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/specification.rb:856:in `stubs_for_pattern'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/specification.rb:819:in `stubs'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/specification.rb:1118:in `latest_specs'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/irb/completion.rb:76:in `retrieve_gem_and_system_load_path'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/irb/completion.rb:99:in `retrieve_files_to_require_from_load_path'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/irb/completion.rb:139:in `block in <module:InputCompletor>'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/irb/completion.rb:157:in `block in <module:InputCompletor>'
    /Users/hung-wulo/src/github.com/Shopify/ruby/test/irb/test_completion.rb:114:in `test_complete_require_with_string_convertable_in_load_path'

  2) Error:
TestIRB::TestCompletion::TestRequireComepletion#test_complete_require_with_malformed_object_in_load_path:
Test::Unit::ProxyError: no implicit conversion of Object into String
    <internal:/Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/core_ext/kernel_require.rb>:85:in `require'
    <internal:/Users/hung-wulo/src/github.com/Shopify/ruby/lib/rubygems/core_ext/kernel_require.rb>:85:in `require'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/pp.rb:76:in `width_for'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/pp.rb:91:in `pp'
    /Users/hung-wulo/src/github.com/Shopify/ruby/lib/pp.rb:638:in `pretty_inspect'

But with this patch both of them should pass.

There are a few downsides of the current approach:

1. Because gem specs are lazily retrieved, this computation happens in
   every irb completion test case, which is not necessary. (In tests we
   don't cache the result of `retrieve_files_to_require_from_load_path`)
2. Gem::Specification.latest_specs is sensible to the content of
   LOAD_PATH. And when combined with 1, tests fail "randomly" if they
   try to mutate LOAD_PATH, even though the test subject it's something
   else.

So by pre-computing and storing the gem paths in a constant, it guarantees
that the computation only happens once and it doesn't get affected by test
cases.

One argument could be made against the change is that, it'll store
unnecessary data for users that disable autocompletion. But the
counter-arguments are:

1. Since autocompletion is enabled by default, this should not be the
   case for most users.
2. For users with autocompletion enabled, IRB already caches the
   result of `retrieve_files_to_require_from_load_path` in memory, which
   should have a similar size of GEM_SPECS. And we currently haven't
   received any report about problems caused by such memory consumption.
@st0012 st0012 force-pushed the make-completion-test-more-stable branch from e2abc5d to c671d39 Compare November 6, 2022 15:22
@peterzhu2118 peterzhu2118 merged commit 93a0756 into ruby:master Nov 7, 2022
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