summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2014-03-22 13:34:46 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2014-03-24 23:35:52 +0100
commit85c906c373eb6abba4804baf454fc0456d2f6416 (patch)
tree076d4a8c8da687cc4be0c6194c875ad0092c2d45
parent171f1b7562d46c1cb50e40657b25d705bca0d773 (diff)
downloadragel-85c906c373eb6abba4804baf454fc0456d2f6416.tar.gz
Ruby: reduce the amount of calls to GET_WIDE_KEY()
When using Ruby as the output GET_WIDE_KEY() would result in the following code being generated: data[p].ord In Ruby string indexing/slicing (using String#[]) is not very efficient, especially not when called a lot of times. In the cases where these changes have been made multiple calls to String#[] were made where only 1 would be needed. The result of this is a noticable performance boost on machines that consume large amounts of input. Smaller machines most likely won't notice a big difference. As an example: a XML/HTML lexer I'm working on using Ragel (at least for now) would take 10 seconds or so to lex an input document using Ragel 6.7. Using this patch that would be reduced to around 5-6 seconds [1]. Note that this patch only affects Ragel output generated with the -F1 option, -T1 and -F0 remain unmodified. [1]: using MRI 2.1.1
-rw-r--r--ragel/rubyflat.cpp20
1 files changed, 11 insertions, 9 deletions
diff --git a/ragel/rubyflat.cpp b/ragel/rubyflat.cpp
index 6cf7e457..827084d2 100644
--- a/ragel/rubyflat.cpp
+++ b/ragel/rubyflat.cpp
@@ -279,16 +279,17 @@ void RubyFlatCodeGen::LOCATE_TRANS()
" _keys = " << vCS() << " << 1\n"
" _inds = " << IO() << "[" << vCS() << "]\n"
" _slen = " << SP() << "[" << vCS() << "]\n"
+ " _wide = " << GET_WIDE_KEY() << "\n"
" _trans = if ( _slen > 0 && \n"
- " " << K() << "[_keys] <= " << GET_WIDE_KEY() << " && \n"
- " " << GET_WIDE_KEY() << " <= " << K() << "[_keys + 1] \n"
+ " " << K() << "[_keys] <= _wide && \n"
+ " " << "_wide <= " << K() << "[_keys + 1] \n"
" ) then\n"
- " " << I() << "[ _inds + " << GET_WIDE_KEY() << " - " << K() << "[_keys] ] \n"
+ " " << I() << "[ _inds + _wide - " << K() << "[_keys] ] \n"
" else \n"
" " << I() << "[ _inds + _slen ]\n"
" end\n"
"";
-
+
}
std::ostream &RubyFlatCodeGen::COND_INDEX_OFFSET()
@@ -308,16 +309,17 @@ std::ostream &RubyFlatCodeGen::COND_INDEX_OFFSET()
void RubyFlatCodeGen::COND_TRANSLATE()
{
- out <<
+ out <<
" _widec = " << GET_KEY() << "\n"
" _keys = " << vCS() << " << 1\n"
" _conds = " << CO() << "[" << vCS() << "]\n"
" _slen = " << CSP() << "[" << vCS() << "]\n"
- " _cond = if ( _slen > 0 && \n"
- " " << CK() << "[_keys] <= " << GET_WIDE_KEY() << " &&\n"
- " " << GET_WIDE_KEY() << " <= " << CK() << "[_keys + 1]\n"
+ " _wide = " << GET_WIDE_KEY() << "\n"
+ " _cond = if ( _slen > 0 && \n"
+ " " << CK() << "[_keys] <= _wide &&\n"
+ " " << "_wide <= " << CK() << "[_keys + 1]\n"
" ) then \n"
- " " << C() << "[ _conds + " << GET_WIDE_KEY() << " - " << CK() << "[_keys]" << " ]\n"
+ " " << C() << "[ _conds + _wide - " << CK() << "[_keys]" << " ]\n"
" else\n"
" 0\n"
" end\n";