Skip to content

Commit

Permalink
Generate text file only table
Browse files Browse the repository at this point in the history
I'm using Stackprof for a performance check of CRuby documents
generation.

Before this commit, RDoc::TopLevel#text? uses processing time about
6.8%.

  ~/rdoc$ stackprof stackprof.dump --text --limit 5
  ==================================
    Mode: cpu(1000)
    Samples: 42712 (0.47% miss rate)
    GC: 6066 (14.20%)
  ==================================
  TOTAL   (pct) SAMPLES   (pct) FRAME
   6066 (14.2%)    6066 (14.2%) (garbage collection)
   2892  (6.8%)    2892  (6.8%) RDoc::TopLevel#text?
  14059 (32.9%)    2379  (5.6%) RDoc::Generator::Darkfish#generate_class
   3015  (7.1%)    1389  (3.3%) Ripper::Lexer#lex
   1265  (3.0%)    1265  (3.0%) Ripper::Lexer::State#initialize

After this commit, processing time of RDoc::TopLevel#text? is reduced to
0.1%.

  ~/rdoc$ stackprof stackprof.dump --method 'RDoc::TopLevel#text?'
  RDoc::TopLevel#text? (/home/aycabta/rdoc/lib/rdoc/top_level.rb:274)
    samples:    20 self (0.1%)  /     20 total (0.1%)
    callers:
        17  (   85.0%)  RDoc::Generator::Darkfish#generate_page
         2  (   10.0%)  RDoc::Store#add_file
         1  (    5.0%)  RDoc::Store#complete
  • Loading branch information
aycabta committed Jul 22, 2018
1 parent 0b39cca commit 818ee97
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def parse_file filename
relative_path.relative_path_from @options.page_dir
end

top_level = @store.add_file filename, relative_path.to_s
top_level = @store.add_file filename, relative_name: relative_path.to_s

parser = RDoc::Parser.for top_level, filename, content, @options, @stats

Expand Down
20 changes: 15 additions & 5 deletions lib/rdoc/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def initialize path = nil, type = nil
@classes_hash = {}
@modules_hash = {}
@files_hash = {}
@text_files_hash = {}

@c_enclosure_classes = {}
@c_enclosure_names = {}
Expand Down Expand Up @@ -184,16 +185,24 @@ def add_c_variables c_parser
# Adds the file with +name+ as an RDoc::TopLevel to the store. Returns the
# created RDoc::TopLevel.

def add_file absolute_name, relative_name = absolute_name
def add_file absolute_name, relative_name: absolute_name, parser: nil
unless top_level = @files_hash[relative_name] then
top_level = RDoc::TopLevel.new absolute_name, relative_name
top_level.parser = parser if parser
top_level.store = self
@files_hash[relative_name] = top_level
@text_files_hash[relative_name] = top_level if top_level.text?
end

top_level
end

def update_parser_of_file(absolute_name, parser)
if top_level = @files_hash[absolute_name] then
@text_files_hash[absolute_name] = top_level if top_level.text?
end
end

##
# Returns all classes discovered by RDoc

Expand Down Expand Up @@ -428,8 +437,8 @@ def find_module_named name
# +file_name+

def find_text_page file_name
@files_hash.each_value.find do |file|
file.text? and file.full_name == file_name
@text_files_hash.each_value.find do |file|
file.full_name == file_name
end
end

Expand Down Expand Up @@ -537,6 +546,7 @@ def load_all
@cache[:pages].each do |page_name|
page = load_page page_name
@files_hash[page_name] = page
@text_files_hash[page_name] = page if page.text?
end
end

Expand Down Expand Up @@ -712,8 +722,8 @@ def modules_hash
# Returns the RDoc::TopLevel that is a text file and has the given +name+

def page name
@files_hash.each_value.find do |file|
file.text? and file.page_name == name
@text_files_hash.each_value.find do |file|
file.page_name == name
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/rdoc/top_level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RDoc::TopLevel < RDoc::Context
##
# The parser class that processed this file

attr_accessor :parser
attr_reader :parser

##
# Creates a new TopLevel for the file at +absolute_name+. If documentation
Expand All @@ -52,6 +52,12 @@ def initialize absolute_name, relative_name = absolute_name
@classes_or_modules = []
end

def parser=(val)
@parser = val
@store.update_parser_of_file(absolute_name, val) if @store
@parser
end

##
# An RDoc::TopLevel is equal to another with the same relative_name

Expand Down
3 changes: 1 addition & 2 deletions test/test_rdoc_cross_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ def test_resolve_method_equals3
end

def test_resolve_page
page = @store.add_file 'README.txt'
page.parser = RDoc::Parser::Simple
page = @store.add_file 'README.txt', parser: RDoc::Parser::Simple

assert_ref page, 'README'
end
Expand Down
3 changes: 1 addition & 2 deletions test/test_rdoc_servlet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def test_documentation_page_page

generator = @s.generator_for store

readme = store.add_file 'README.rdoc'
readme.parser = RDoc::Parser::Simple
readme = store.add_file 'README.rdoc', parser: RDoc::Parser::Simple

@s.documentation_page store, generator, 'README_rdoc.html', @req, @res

Expand Down
11 changes: 4 additions & 7 deletions test/test_rdoc_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def setup

@top_level = @s.add_file 'file.rb'

@page = @s.add_file 'README.txt'
@page.parser = RDoc::Parser::Simple
@page = @s.add_file 'README.txt', parser: RDoc::Parser::Simple
@page.comment = RDoc::Comment.new 'This is a page', @page

@klass = @top_level.add_class RDoc::NormalClass, 'Object'
Expand Down Expand Up @@ -146,7 +145,7 @@ def test_add_file
end

def test_add_file_relative
top_level = @store.add_file 'path/file.rb', 'file.rb'
top_level = @store.add_file 'path/file.rb', relative_name: 'file.rb'

assert_kind_of RDoc::TopLevel, top_level
assert_equal @store, top_level.store
Expand Down Expand Up @@ -310,8 +309,7 @@ def test_find_module_named
end

def test_find_text_page
page = @store.add_file 'PAGE.txt'
page.parser = RDoc::Parser::Simple
page = @store.add_file 'PAGE.txt', parser: RDoc::Parser::Simple

assert_nil @store.find_text_page 'no such page'

Expand Down Expand Up @@ -601,8 +599,7 @@ def test_module_names
end

def test_page
page = @store.add_file 'PAGE.txt'
page.parser = RDoc::Parser::Simple
page = @store.add_file 'PAGE.txt', parser: RDoc::Parser::Simple

assert_nil @store.page 'no such page'

Expand Down

0 comments on commit 818ee97

Please sign in to comment.