Skip to content

Commit

Permalink
Improve performance for many unresolved includes/extends
Browse files Browse the repository at this point in the history
Potential fix for ruby#1126
  • Loading branch information
Earlopain committed Jun 27, 2024
1 parent 4b84660 commit 1db4502
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
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

0 comments on commit 1db4502

Please sign in to comment.