Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source.match performance without specifying term string #186

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ class BaseParser
}

module Private
# Terminal requires two or more letters.
INSTRUCTION_TERM = "?>"
COMMENT_TERM = "-->"
CDATA_TERM = "]]>"
DOCTYPE_TERM = "]>"
# Read to the end of DOCTYPE because there is no proper ENTITY termination
ENTITY_TERM = DOCTYPE_TERM

INSTRUCTION_END = /#{NAME}(\s+.*?)?\?>/um
TAG_PATTERN = /((?>#{QNAME_STR}))\s*/um
CLOSE_PATTERN = /(#{QNAME_STR})\s*>/um
Expand Down Expand Up @@ -251,7 +243,7 @@ def pull_event
return process_instruction(start_position)
elsif @source.match("<!", true)
if @source.match("--", true)
md = @source.match(/(.*?)-->/um, true, term: Private::COMMENT_TERM)
md = @source.match(/(.*?)-->/um, true)
if md.nil?
raise REXML::ParseException.new("Unclosed comment", @source)
end
Expand Down Expand Up @@ -318,7 +310,7 @@ def pull_event
raise REXML::ParseException.new( "Bad ELEMENT declaration!", @source ) if md.nil?
return [ :elementdecl, "<!ELEMENT" + md[1] ]
elsif @source.match("ENTITY", true)
match_data = @source.match(Private::ENTITYDECL_PATTERN, true, term: Private::ENTITY_TERM)
match_data = @source.match(Private::ENTITYDECL_PATTERN, true)
unless match_data
raise REXML::ParseException.new("Malformed entity declaration", @source)
end
Expand Down Expand Up @@ -387,14 +379,14 @@ def pull_event
raise REXML::ParseException.new(message, @source)
end
return [:notationdecl, name, *id]
elsif md = @source.match(/--(.*?)-->/um, true, term: Private::COMMENT_TERM)
elsif md = @source.match(/--(.*?)-->/um, true)
case md[1]
when /--/, /-\z/
raise REXML::ParseException.new("Malformed comment", @source)
end
return [ :comment, md[1] ] if md
end
elsif match = @source.match(/(%.*?;)\s*/um, true, term: Private::DOCTYPE_TERM)
elsif match = @source.match(/(%.*?;)\s*/um, true)
return [ :externalentity, match[1] ]
elsif @source.match(/\]\s*>/um, true)
@document_status = :after_doctype
Expand Down Expand Up @@ -434,15 +426,15 @@ def pull_event
#STDERR.puts "SOURCE BUFFER = #{source.buffer}, #{source.buffer.size}"
raise REXML::ParseException.new("Malformed node", @source) unless md
if md[0][0] == ?-
md = @source.match(/--(.*?)-->/um, true, term: Private::COMMENT_TERM)
md = @source.match(/--(.*?)-->/um, true)

if md.nil? || /--|-\z/.match?(md[1])
raise REXML::ParseException.new("Malformed comment", @source)
end

return [ :comment, md[1] ]
else
md = @source.match(/\[CDATA\[(.*?)\]\]>/um, true, term: Private::CDATA_TERM)
md = @source.match(/\[CDATA\[(.*?)\]\]>/um, true)
return [ :cdata, md[1] ] if md
end
raise REXML::ParseException.new( "Declarations can only occur "+
Expand Down Expand Up @@ -656,7 +648,7 @@ def parse_id_invalid_details(accept_external_id:,
end

def process_instruction(start_position)
match_data = @source.match(Private::INSTRUCTION_END, true, term: Private::INSTRUCTION_TERM)
match_data = @source.match(Private::INSTRUCTION_END, true)
unless match_data
message = "Invalid processing instruction node"
@source.position = start_position
Expand Down
26 changes: 18 additions & 8 deletions lib/rexml/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def read_until(term)
def ensure_buffer
end

def match(pattern, cons=false, term: nil)
def match(pattern, cons=false)
if cons
@scanner.scan(pattern).nil? ? nil : @scanner
else
Expand Down Expand Up @@ -204,10 +204,20 @@ def initialize(arg, block_size=500, encoding=nil)
end
end

def read(term = nil)
def read(term = nil, min_bytes = 1)
term = encode(term) if term
begin
@scanner << readline(term)
str = readline(term)
@scanner << str
read_bytes = str.bytesize
begin
while read_bytes < min_bytes
str = readline(term)
@scanner << str
read_bytes += str.bytesize
end
rescue IOError
end
true
rescue Exception, NameError
@source = nil
Expand Down Expand Up @@ -237,10 +247,9 @@ def ensure_buffer
read if @scanner.eos? && @source
end

# Note: When specifying a string for 'pattern', it must not include '>' except in the following formats:
# - ">"
# - "XXX>" (X is any string excluding '>')
def match( pattern, cons=false, term: nil )
def match( pattern, cons=false )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style. Very minor: the spaces around the argument list does not match the other methods in the file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We should use a consistent coding style in the same code base. But it's out-of-scope of this pull request. We should work on it separately.

# To avoid performance issue, we need to increase bytes to read per scan
min_bytes = 1
while true
if cons
md = @scanner.scan(pattern)
Expand All @@ -250,7 +259,8 @@ def match( pattern, cons=false, term: nil )
break if md
return nil if pattern.is_a?(String)
return nil if @source.nil?
return nil unless read(term)
return nil unless read(nil, min_bytes)
min_bytes *= 2
end

md.nil? ? nil : @scanner
Expand Down