Skip to content

Commit

Permalink
Fix performance regression in 62119e5.
Browse files Browse the repository at this point in the history
That commit caused unnecessary creation of string objects in
REGEX commands. Work around that by providing direct access
to the data buffer. Also use peek() instead of next() to
not construct unnecessary single-char string objects.

Addresses #8.
  • Loading branch information
eldering committed Nov 18, 2018
1 parent 42edb06 commit a6ba1ab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 4 additions & 0 deletions databuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class databuffer {
size_t line() const { return _line; }
size_t lpos() const { return _lpos; }

std::string::const_iterator curr() const { return data.cbegin()+_pos; }
std::string::const_iterator begin() const { return data.cbegin(); }
std::string::const_iterator end() const { return data.cend(); }

std::string next(size_t length=1) const
{
size_t end = std::min(size(),_pos+length);
Expand Down
11 changes: 6 additions & 5 deletions libchecktestdata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ bool dotest(const test& t)
if ( gendata ) {
return (get_random(2) == 0);
} else {
return !data.eof() && t.args[0].val.find(data.next())!=string::npos;
return !data.eof() && t.args[0].val.find(data.peek())!=string::npos;
}
case 'U': return unique(t.args);
case 'A': return inarray(t.args[0],t.args[1]);
Expand Down Expand Up @@ -1010,8 +1010,9 @@ void checktoken(const command& cmd)
if ( toupper(data.readchar())!='E' ) error("exponent 'E' expected");
has_exp = true;
if ( data.peek()=='-' || data.peek()=='+' ) data.readchar();
while ( isdigit(data.peek()) ) data.readchar();
if ( !isdigit(data.peek(-1)) ) error("digit expected");
char c = '!';
while ( isdigit(data.peek()) ) c = data.readchar();
if ( !isdigit(c) ) error("digit expected");
}

if ( cmd.name()=="FLOATP" ) {
Expand Down Expand Up @@ -1055,8 +1056,8 @@ void checktoken(const command& cmd)
smatch res;
string matchstr;

string searchstr = data.next(data.size());
if ( !regex_search(searchstr,res,regexstr,regex_constants::match_continuous) ) {
if ( !regex_search(data.curr(),data.end(),res,regexstr,
regex_constants::match_continuous) ) {
error();
} else {
size_t match_len = res[0].second - res[0].first;
Expand Down

0 comments on commit a6ba1ab

Please sign in to comment.