Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
According to valgrind, the tokenizer::next_token function is called several million times during a typical unit test. This commit changes the parser to allocate the tokenizer as a member variable of the class (on the stack) rather than on the heap using new, thus it eliminates millions of calls to new.
- Loading branch information
Showing
1 changed file
with
48 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d2fe8d1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: The commit message is unfortunately misleading --
the reasons it is an improvement is
1.) Eliminates pointer dereference step tok_-> at nearly every line of parser operator()() -- as I understand C++ compilers cannot optimize out instructions like this.
2.) Therefore simplify inlining of tokenizer functions.
3.) Favor stack allocation over heap allocation when possible, for speed.
The tokenizer is not reallocated every time parser operator() is called, so this commit does not in fact save millions of calls to new() as described.
callgrind reports that for a unit test "wesnoth -u characterize_pathfinding_reach_1", parser::operator()() and tokenizer::next_token() take 20% of the (estimated) time respectively, and that after this change they only take 15%.