From 72f14d827c861fb764f30bd5bd909fe5c6c66873 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Tue, 15 Mar 2016 22:06:22 +0900 Subject: [PATCH] Remove an extra branch from the bottleneck RDoc::RubyLex#getc is the bottleneck in RDoc. It determined which ungetc buffer to use by branching upon `@here_header`. We can remove the branch by replacing `@here_header` flag with `@current_readed`, which represents the current buffer directly, --- lib/rdoc/ruby_lex.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/rdoc/ruby_lex.rb b/lib/rdoc/ruby_lex.rb index 91b90ab2cf..d56e693a3d 100644 --- a/lib/rdoc/ruby_lex.rb +++ b/lib/rdoc/ruby_lex.rb @@ -101,10 +101,10 @@ def initialize(content, options) @exp_line_no = @line_no = 1 @here_readed = [] @readed = [] + @current_readed = @readed @rests = [] @seek = 0 - @here_header = false @indent = 0 @indent_stack = [] @lex_state = :EXPR_BEG @@ -160,7 +160,7 @@ def get_readed end readed = @readed.join("") - @readed = [] + @readed.clear readed end @@ -170,13 +170,9 @@ def getc @rests.push nil unless buf_input end c = @rests.shift - if @here_header - @here_readed.push c - else - @readed.push c - end + @current_readed.push c @seek += 1 - if c == "\n" + if c == "\n".freeze @line_no += 1 @char_no = 0 else @@ -282,7 +278,7 @@ def initialize_input @indent_stack = [] @lex_state = :EXPR_BEG @space_seen = false - @here_header = false + @current_readed = @readed @continue = false prompt @@ -461,8 +457,8 @@ def lex_init() @indent_stack.pop end end - @here_header = false - @here_readed = [] + @current_readed = @readed + @here_readed.clear Token(TkNL) end @@ -1020,7 +1016,7 @@ def identify_here_document doc = '"' end - @here_header = false + @current_readed = @readed while l = gets l = l.sub(/(:?\r)?\n\z/, "\n") if (indent ? l.strip : l.chomp) == quoted @@ -1037,7 +1033,7 @@ def identify_here_document doc << '"' end - @here_header = true + @current_readed = @here_readed @here_readed.concat reserve while ch = reserve.pop ungetc ch