简体   繁体   中英

Strange behavior using if statement

I'm developing an application using OpenSceneGraph and I'm encountering some odd behavior in an if statement. I'm not sure if it's specific to the API because it simply makes no sense on any level to me.

The code:

if ( !fileAddList_.empty() )
{
    sg::FileStampThread::instance()->addFiles( fileAddList_ );
    fileAddList_.clear();
}

Where:

  • fileAddList_: a static vector of custom objects used to maintain filenames

  • FileStampThread: an instance of an OpenThreads object

  • addFiles(): a method in the thread which saves the list of file objects passed to it

The above code implements hotloading in my application. The FileStampThread instance runs continuously, checking time stamps of filenames passed to it. Once a stamp changes, the filename is saved to another list and passed back for reloading.

What's odd is that the update traversal of the scene graph (when this code gets executed) slows down considerably when I enable this section of code, even if there are no files to add (hat is, even if fileAddList_ is empty). The update traversal time increases by an order of magnitude as a result.

However, if I comment out the call to sg::FileStampThread::addFiles, the slowdown goes away. Yet I've trapped the call in debug mode and it never gets executed.

So, I'm perplexed: why would a line of code inside a conditional affect my program execution speed when the conditional test fails and, by all appearances, it's never executed?

As a side note, I suspected it may have something to do with declaring the variable as a static, so I tried declaring it as a global (using extern) instead, to the same effect.


An edit to address some of the comments below:

  • The thread is an instance of an OpenThreads object. No MS-specific stuff, here. The instance is static.

  • addFiles() is not templated

  • I tested the loop with code in it. I commented out the lines alternately. I'm absolutely positive inclusion of the addFiles() call is the culprit.

  • Debug vs. Release is no different, pushing the code off into a separate function changed nothing, unfortunately.

  • OSG is high-performance and the comment about misprediction could be right on. Research forthcoming...

Code for the FileStampThread class:

void sg::FileStampThread::addFiles( sg::AssetFileList& files )
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock( contentMutex_ );

    for ( sg::AssetFileList::iterator it = files.begin(); it != files.end(); ++it )
    {
        if ( boost::filesystem::exists( (*it).getPath() ))
            fileList_.push_back( (*it) );
    }
};

try to move the code:

sg::FileStampThread::instance()->addFiles( fileAddList_ );
fileAddList_.clear();

in a separate function a see if the problem persist. Hard to sat what is happening, same behavior on release and debug builds?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM