Skip to content

Commit 31d3127

Browse files
author
Zachary Scott
authored
Merge pull request #404 from rdoc/retry-mame-pr
Retry mame's pull request for Performance Improvement
2 parents bb02ced + 7dbd025 commit 31d3127

File tree

4 files changed

+90
-46
lines changed

4 files changed

+90
-46
lines changed

History.rdoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
* Minor enhancements
1212
* Improve class name expansion/resolution in ri. PR #400 by NARUSE, Yui
13+
* Improve performance of document generation. PR #397 by Yusuke Endoh.
1314

1415
=== 4.2.2 / 2016-02-09
1516

lib/rdoc/parser/c.rb

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -606,18 +606,46 @@ def find_attr_comment var_name, attr_name, read = nil, write = nil
606606
RDoc::Comment.new comment, @top_level
607607
end
608608

609+
##
610+
# Generate a Ruby-method table
611+
612+
def gen_body_table file_content
613+
table = {}
614+
file_content.scan(%r{
615+
((?>/\*.*?\*/\s*)?)
616+
((?:(?:\w+)\s+)?
617+
(?:intern\s+)?VALUE\s+(\w+)
618+
\s*(?:\([^)]*\))(?:[^;]|$))
619+
| ((?>/\*.*?\*/\s*))^\s*(\#\s*define\s+(\w+)\s+(\w+))
620+
| ^\s*\#\s*define\s+(\w+)\s+(\w+)
621+
}xm) do
622+
case
623+
when $1
624+
table[$3] = [:func_def, $1, $2, $~.offset(2)] if !table[$3] || table[$3][0] != :func_def
625+
when $4
626+
table[$6] = [:macro_def, $4, $5, $~.offset(5), $7] if !table[$6] || table[$6][0] == :macro_alias
627+
when $8
628+
table[$8] ||= [:macro_alias, $9]
629+
end
630+
end
631+
table
632+
end
633+
609634
##
610635
# Find the C code corresponding to a Ruby method
611636

612637
def find_body class_name, meth_name, meth_obj, file_content, quiet = false
613-
case file_content
614-
when %r%((?>/\*.*?\*/\s*)?)
615-
((?:(?:\w+)\s+)?
616-
(?:intern\s+)?VALUE\s+#{meth_name}
617-
\s*(\([^)]*\))([^;]|$))%xm then
618-
comment = RDoc::Comment.new $1, @top_level
619-
body = $2
620-
offset, = $~.offset(2)
638+
if file_content
639+
@body_table ||= {}
640+
@body_table[file_content] ||= gen_body_table file_content
641+
type, *args = @body_table[file_content][meth_name]
642+
end
643+
644+
case type
645+
when :func_def
646+
comment = RDoc::Comment.new args[0], @top_level
647+
body = args[1]
648+
offset, = args[2]
621649

622650
comment.remove_private if comment
623651

@@ -646,12 +674,12 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
646674
meth_obj.line = file_content[0, offset].count("\n") + 1
647675

648676
body
649-
when %r%((?>/\*.*?\*/\s*))^\s*(\#\s*define\s+#{meth_name}\s+(\w+))%m then
650-
comment = RDoc::Comment.new $1, @top_level
651-
body = $2
652-
offset = $~.offset(2).first
677+
when :macro_def
678+
comment = RDoc::Comment.new args[0], @top_level
679+
body = args[1]
680+
offset, = args[2]
653681

654-
find_body class_name, $3, meth_obj, file_content, true
682+
find_body class_name, args[3], meth_obj, file_content, true
655683

656684
comment.normalize
657685
find_modifiers comment, meth_obj
@@ -665,11 +693,11 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
665693
meth_obj.line = file_content[0, offset].count("\n") + 1
666694

667695
body
668-
when %r%^\s*\#\s*define\s+#{meth_name}\s+(\w+)%m then
696+
when :macro_alias
669697
# with no comment we hope the aliased definition has it and use it's
670698
# definition
671699

672-
body = find_body(class_name, $1, meth_obj, file_content, true)
700+
body = find_body(class_name, args[0], meth_obj, file_content, true)
673701

674702
return body if body
675703

@@ -764,28 +792,42 @@ def find_class_comment class_name, class_mod
764792
class_mod.add_comment comment, @top_level
765793
end
766794

795+
##
796+
# Generate a const table
797+
798+
def gen_const_table file_content
799+
table = {}
800+
@content.scan(%r{
801+
((?>^\s*/\*.*?\*/\s+))
802+
rb_define_(\w+)\((?:\s*(?:\w+),)?\s*
803+
"(\w+)"\s*,
804+
.*?\)\s*;
805+
| Document-(?:const|global|variable):\s
806+
((?:\w+::)*\w+)
807+
\s*?\n((?>.*?\*/))
808+
}mxi) do
809+
case
810+
when $1 then table[[$2, $3]] = $1
811+
when $4 then table[$4] = "/*\n" + $5
812+
end
813+
end
814+
table
815+
end
816+
767817
##
768818
# Finds a comment matching +type+ and +const_name+ either above the
769819
# comment or in the matching Document- section.
770820

771821
def find_const_comment(type, const_name, class_name = nil)
772-
comment = if @content =~ %r%((?>^\s*/\*.*?\*/\s+))
773-
rb_define_#{type}\((?:\s*(\w+),)?\s*
774-
"#{const_name}"\s*,
775-
.*?\)\s*;%xmi then
776-
$1
777-
elsif class_name and
778-
@content =~ %r%Document-(?:const|global|variable):\s
779-
#{class_name}::#{const_name}
780-
\s*?\n((?>.*?\*/))%xm then
781-
"/*\n#{$1}"
782-
elsif @content =~ %r%Document-(?:const|global|variable):
783-
\s#{const_name}
784-
\s*?\n((?>.*?\*/))%xm then
785-
"/*\n#{$1}"
786-
else
787-
''
788-
end
822+
@const_table ||= {}
823+
@const_table[@content] ||= gen_const_table @content
824+
table = @const_table[@content]
825+
826+
comment =
827+
table[[type, const_name]] ||
828+
(class_name && table[class_name + "::" + const_name]) ||
829+
table[const_name] ||
830+
''
789831

790832
RDoc::Comment.new comment, @top_level
791833
end

lib/rdoc/parser/changelog.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ def create_items items
102102
# Groups +entries+ by date.
103103

104104
def group_entries entries
105+
@time_cache ||= {}
105106
entries.group_by do |title, _|
106107
begin
107-
Time.parse(title).strftime '%Y-%m-%d'
108+
time = @time_cache[title]
109+
(time || Time.parse(title)).strftime '%Y-%m-%d'
108110
rescue NoMethodError, ArgumentError
109111
time, = title.split ' ', 2
110112
Time.parse(time).strftime '%Y-%m-%d'
@@ -128,6 +130,7 @@ def group_entries entries
128130
# 'README.EXT.ja: ditto']]
129131

130132
def parse_entries
133+
@time_cache ||= {}
131134
entries = []
132135
entry_name = nil
133136
entry_body = []
@@ -143,6 +146,7 @@ def parse_entries
143146

144147
begin
145148
time = Time.parse entry_name
149+
@time_cache[entry_name] = time
146150
# HACK Ruby 1.8 does not raise ArgumentError for Time.parse "Other"
147151
entry_name = nil unless entry_name =~ /#{time.year}/
148152
rescue NoMethodError
@@ -185,6 +189,7 @@ def parse_entries
185189
# Converts the ChangeLog into an RDoc::Markup::Document
186190

187191
def scan
192+
@time_cache = {}
188193
entries = parse_entries
189194
grouped_entries = group_entries entries
190195

lib/rdoc/ruby_lex.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ def initialize(content, options)
102102
@exp_line_no = @line_no = 1
103103
@here_readed = []
104104
@readed = []
105+
@current_readed = @readed
105106
@rests = []
106107
@seek = 0
107108

108-
@here_header = false
109109
@indent = 0
110110
@indent_stack = []
111111
@lex_state = :EXPR_BEG
@@ -161,7 +161,7 @@ def get_readed
161161
end
162162

163163
readed = @readed.join("")
164-
@readed = []
164+
@readed.clear
165165
readed
166166
end
167167

@@ -171,13 +171,9 @@ def getc
171171
@rests.push nil unless buf_input
172172
end
173173
c = @rests.shift
174-
if @here_header
175-
@here_readed.push c
176-
else
177-
@readed.push c
178-
end
174+
@current_readed.push c
179175
@seek += 1
180-
if c == "\n"
176+
if c == "\n".freeze
181177
@line_no += 1
182178
@char_no = 0
183179
else
@@ -283,7 +279,7 @@ def initialize_input
283279
@indent_stack = []
284280
@lex_state = :EXPR_BEG
285281
@space_seen = false
286-
@here_header = false
282+
@current_readed = @readed
287283

288284
@continue = false
289285
prompt
@@ -462,8 +458,8 @@ def lex_init()
462458
@indent_stack.pop
463459
end
464460
end
465-
@here_header = false
466-
@here_readed = []
461+
@current_readed = @readed
462+
@here_readed.clear
467463
Token(TkNL)
468464
end
469465

@@ -1021,7 +1017,7 @@ def identify_here_document
10211017
doc = '"'
10221018
end
10231019

1024-
@here_header = false
1020+
@current_readed = @readed
10251021
while l = gets
10261022
l = l.sub(/(:?\r)?\n\z/, "\n")
10271023
if (indent ? l.strip : l.chomp) == quoted
@@ -1038,7 +1034,7 @@ def identify_here_document
10381034
doc << '"'
10391035
end
10401036

1041-
@here_header = true
1037+
@current_readed = @here_readed
10421038
@here_readed.concat reserve
10431039
while ch = reserve.pop
10441040
ungetc ch

0 commit comments

Comments
 (0)