[as2api-dev] [CVS trunk] After some profilling, it seems like ASLexer#fill might
be the major bottleneck.
David Holroyd
dave at badgers-in-foil.co.uk
Sat, 02 Jul 2005 23:52:09 +0000
<html>
<head>
<style><!--
body {background-color:#ffffff;}
.file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
.pathname {font-family:monospace; float:right;}
.fileheader {margin-bottom:.5em;}
.diff {margin:0;}
.tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
.tasklist ul {margin-top:0;margin-bottom:0;}
tr.alt {background-color:#eeeeee}
#added {background-color:#ddffdd;}
#addedchars {background-color:#99ff99;font-weight:bolder;}
tr.alt #added {background-color:#ccf7cc;}
#removed {background-color:#ffdddd;}
#removedchars {background-color:#ff9999;font-weight:bolder;}
tr.alt #removed {background-color:#f7cccc;}
#info {color:#888888;}
#context {background-color:#eeeeee;}
td {padding-left:.3em;padding-right:.3em;}
tr.head {border-bottom-width:1px;border-bottom-style:solid;}
tr.head td {padding:0;padding-top:.2em;}
.task {background-color:#ffff00;}
.comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
.error {color:red;}
hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>trunk/as2api/parse</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt><a href="#file1">lexer.rb</a></tt></td><td align="right" id="added">+18</td><td align="right" id="removed">-17</td><td nowrap="nowrap" align="center"><a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb?rev=179&content-type=text/vnd.viewcvs-markup">179</a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb.diff?r1=179&r2=180">-></a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb?rev=180&content-type=text/vnd.viewcvs-markup">180</a></td></tr>
</table>
<pre class="comment">
After some profilling, it seems like ASLexer#fill might be the major bottleneck.
This change unrolls the fill() methods inner @@matches.each loop, but keeps
the code fairly tight still, by generating the fill() method using class_eval()
This naive change cuts about 30% off the runtime of the example I've been
testing with (was ~40secs, now ~27secs).
The lexer code could still use a lot of cleaning up.
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname"><a
href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk">trunk</a>/<a
href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api">as2api</a>/<a
href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse">parse</a></span><br />
<div class="fileheader"><big><b>lexer.rb</b></big> <small id="info"><a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb?rev=179&content-type=text/vnd.viewcvs-markup">179</a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb.diff?r1=179&r2=180">-></a> <a href="http://svn.badgers-in-foil.co.uk/viewcvs.cgi/as2api/trunk/as2api/parse/lexer.rb?rev=180&content-type=text/vnd.viewcvs-markup">180</a></small></div>
<pre class="diff"><small id="info">--- trunk/as2api/parse/lexer.rb 2005-07-02 23:18:48 UTC (rev 179)
+++ trunk/as2api/parse/lexer.rb 2005-07-02 23:52:07 UTC (rev 180)
@@ -247,7 +247,7 @@
</small></pre><pre class="diff" id="context"> private
def ASLexer.make_match(match)
</pre><pre class="diff" id="removed">- Regexp.new(match)
</pre><pre class="diff" id="added">+ match.gsub("/", "\\/").gsub("\n", "\\n")
</pre><pre class="diff" id="context"> end
h = "[0-9a-fA-F]"
</pre><pre class="diff"><small id="info">@@ -372,25 +372,26 @@
</small></pre><pre class="diff" id="context"> end
end
</pre><pre class="diff" id="removed">- def fill
- line = StringScanner.new(@io.readline)
- until line.eos?
- matched = false
- @@matches.each do |token_match|
- re, action = token_match
- match = line.scan(re)
- if match
- action.call(self, line, @io)
- matched = true
- break
- end
- end
- unless matched
- parse_error(line)
- end
</pre><pre class="diff" id="added">+ def self.build_lexer
+ text = <<-EOS
+ def fill
+ line = StringScanner.new(@io.readline)
+ until line.eos?
+ EOS
+ @@matches.each_with_index do |token_match, index|
+ re, action = token_match
+ text << "if line.scan(/#{re}/)\n"
+ text << " @@matches[#{index}][1].call(self, line, @io)\n"
+ text << " next\n"
+ text << "end\n"
</pre><pre class="diff" id="context"> end
</pre><pre class="diff" id="added">+ text << " end\n end\n"
+ puts text
+ class_eval(text)
</pre><pre class="diff" id="context"> end
</pre><pre class="diff" id="added">+ self.build_lexer
+
</pre><pre class="diff" id="context"> public
def emit(token)
@tokens << token
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -> email">CVSspam</a> 0.2.11</small></center>
</body></html>