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

Improve performance for many unresolved includes/extends #1127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions lib/rdoc/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def initialize(name, comment)
@name = name
self.comment = comment
@module = nil # cache for module if found
@module_lookup_failed = false
end

##
Expand Down Expand Up @@ -64,16 +65,10 @@ def inspect # :nodoc:
# - if not found, look into the children of included modules,
# in reverse inclusion order;
# - if still not found, go up the hierarchy of names.
#
# This method has <code>O(n!)</code> behavior when the module calling
# include is referencing nonexistent modules. Avoid calling #module until
# after all the files are parsed. This behavior is due to ruby's constant
# lookup behavior.
#
# As of the beginning of October, 2011, no gem includes nonexistent modules.

def module
return @module if @module
return @name if @module_lookup_failed

# search the current context
return @name unless parent
Expand Down Expand Up @@ -101,6 +96,7 @@ def module
up = up.parent
end

@module_lookup_failed = true
@name
end

Expand Down
19 changes: 16 additions & 3 deletions test/rdoc/test_rdoc_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,26 @@ def test_add_to_done_documenting
refute_includes arr, incl
end

def bench_add_include
cm = RDoc::ClassModule.new 'Klass'
def test_bench_add_include
assert_linear_performance (1..25) do |count|
cm = RDoc::ClassModule.new 'Klass'
count.times do |i|
cm.add_include RDoc::Include.new("N::M#{i}", nil)
end
end
end

def test_bench_include_module_resolution
assert_linear_performance (1..25) do |count|
cm = RDoc::ClassModule.new 'Klass'
cm.store = RDoc::Store.new

assert_performance_linear 0.5 do |count|
count.times do |i|
cm.add_include RDoc::Include.new("N::M#{i}", nil)
end
last_include = RDoc::Include.new("N::M#{count + 1}", nil)
cm.add_include(last_include)
last_include.module
end
end

Expand Down