1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Speed up func1.func2 completion by using Set for ignored modules

And thus avoiding Module#name calls. Those are slow, especially in
larger projects, with lots of anonymous modules.

[Fix GH-1798]

From: Dmitry Gutov <dgutov@yandex.ru>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62732 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-03-13 01:28:27 +00:00
parent 0faf018624
commit 583d903c2b

View file

@ -169,18 +169,9 @@ module IRB
else
# func1.func2
candidates = []
to_ignore = ignored_modules
ObjectSpace.each_object(Module){|m|
begin
name = m.name
rescue Exception
name = ""
end
begin
next if name != "IRB::Context" and
/^(IRB|SLex|RubyLex|RubyToken)/ =~ name
rescue Exception
next
end
next if (to_ignore.include?(m) rescue true)
candidates.concat m.instance_methods(false).collect{|x| x.to_s}
}
candidates.sort!
@ -218,6 +209,32 @@ module IRB
end
end
end
def self.ignored_modules
# We could cache the result, but this is very fast already.
# By using this approach, we avoid Module#name calls, which are
# relatively slow when there are a lot of anonymous modules defined.
require 'set'
s = Set.new
scanner = lambda do |m|
next if s.include?(m) # IRB::ExtendCommandBundle::EXCB recurses.
s << m
m.constants(false).each do |c|
value = m.const_get(c)
scanner.call(value) if value.is_a?(Module)
end
end
%i(IRB SLex RubyLex RubyToken).each do |sym|
next unless Object.const_defined?(sym)
scanner.call(Object.const_get(sym))
end
s.delete(IRB::Context) if defined?(IRB::Context)
s
end
end
end