From 818ee979e02151885b4ae769b9f87b8f6b5f1802 Mon Sep 17 00:00:00 2001 From: aycabta Date: Wed, 18 Jul 2018 21:03:03 +0900 Subject: [PATCH] Generate text file only table 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 --- lib/rdoc/rdoc.rb | 2 +- lib/rdoc/store.rb | 20 +++++++++++++++----- lib/rdoc/top_level.rb | 8 +++++++- test/test_rdoc_cross_reference.rb | 3 +-- test/test_rdoc_servlet.rb | 3 +-- test/test_rdoc_store.rb | 11 ++++------- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/rdoc/rdoc.rb b/lib/rdoc/rdoc.rb index 31a92b3bec..ca2c1abefd 100644 --- a/lib/rdoc/rdoc.rb +++ b/lib/rdoc/rdoc.rb @@ -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 diff --git a/lib/rdoc/store.rb b/lib/rdoc/store.rb index b7e677abf9..f420fc2bd2 100644 --- a/lib/rdoc/store.rb +++ b/lib/rdoc/store.rb @@ -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 = {} @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/rdoc/top_level.rb b/lib/rdoc/top_level.rb index b8d4506ec8..b8b6110bb2 100644 --- a/lib/rdoc/top_level.rb +++ b/lib/rdoc/top_level.rb @@ -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 @@ -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 diff --git a/test/test_rdoc_cross_reference.rb b/test/test_rdoc_cross_reference.rb index 8233fe3f61..183de0930d 100644 --- a/test/test_rdoc_cross_reference.rb +++ b/test/test_rdoc_cross_reference.rb @@ -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 diff --git a/test/test_rdoc_servlet.rb b/test/test_rdoc_servlet.rb index c0c0869293..0a197a31b6 100644 --- a/test/test_rdoc_servlet.rb +++ b/test/test_rdoc_servlet.rb @@ -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 diff --git a/test/test_rdoc_store.rb b/test/test_rdoc_store.rb index e5cf75203f..5a884c0b5f 100644 --- a/test/test_rdoc_store.rb +++ b/test/test_rdoc_store.rb @@ -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' @@ -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 @@ -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' @@ -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'