Author Topic: Question on Tokenizer::SkipWhiteSpace  (Read 6323 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5721
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Question on Tokenizer::SkipWhiteSpace
« on: April 05, 2009, 01:36:25 pm »
Here is the code
Code
bool Tokenizer::SkipWhiteSpace()
{
    // skip spaces, tabs, etc.
    while (CurrentChar() <= _T(' ') && MoveToNextChar()) // don't check EOF when MoveToNextChar already does, also replace isspace() which calls msvcrt.dll
        ;                                                // with a dirty hack:  CurrentChar() <= ' ' is "good enough" here
    if (IsEOF())
        return false;
    return true;
}

My question is: Why we should avoid a call to msvcrt.dll?

I use "dependency walker" to check "codeblocks.dll", it do depend on msvcrt.dll. See the screen shot.

Any comments? Thanks.

[attachment deleted by admin]
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Question on Tokenizer::SkipWhiteSpace
« Reply #1 on: April 05, 2009, 03:21:06 pm »
The idea behind avoiding a call to isspace in msvcrt.dll is for the sake of optimization. Comparing directly with "empty-space" (although it is a dirty hack, as the comment points) is a lot faster than pushing the character, saving all registers, calling the function, comparing the character, restoring all registers and returning. I think it was Thomas who changed that, and it improved performance. No other reasons for it.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5721
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Question on Tokenizer::SkipWhiteSpace
« Reply #2 on: April 05, 2009, 03:50:44 pm »
The idea behind avoiding a call to isspace in msvcrt.dll is for the sake of optimization. Comparing directly with "empty-space" (although it is a dirty hack, as the comment points) is a lot faster than pushing the character, saving all registers, calling the function, comparing the character, restoring all registers and returning. I think it was Thomas who changed that, and it improved performance. No other reasons for it.

Thanks for your answer.

By the way, I thought carefully why it didn't use equal comparing instead.
Finally  I realized that

Code
CurrentChar() <= _T(' ')

also skips _T('\n'), that can't be done by calling isspace().
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.