简体   繁体   English

使用if语句的奇怪行为

[英]Strange behavior using if statement

I'm developing an application using OpenSceneGraph and I'm encountering some odd behavior in an if statement. 我正在使用OpenSceneGraph开发应用程序,并且在if语句中遇到一些奇怪的行为。 I'm not sure if it's specific to the API because it simply makes no sense on any level to me. 我不确定它是否特定于API,因为它对我而言在任何层面上都毫无意义。

The code: 编码:

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

Where: 哪里:

  • fileAddList_: a static vector of custom objects used to maintain filenames fileAddList_:用于维护文件名的自定义对象的静态向量

  • FileStampThread: an instance of an OpenThreads object FileStampThread:OpenThreads对象的实例

  • addFiles(): a method in the thread which saves the list of file objects passed to it addFiles():线程中的一种方法,用于保存传递给它的文件对象列表

The above code implements hotloading in my application. 上面的代码在我的应用程序中实现了热加载。 The FileStampThread instance runs continuously, checking time stamps of filenames passed to it. FileStampThread实例连续运行,检查传递给它的文件名的时间戳。 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). 奇怪的是,即使没有文件要添加(即使fileAddList_为空),当我启用此部分代码时,场景图的更新遍历(当执行此代码时)也会大大降低。 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. 但是,如果我注释掉对sg :: FileStampThread :: addFiles的调用,则速度变慢了。 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. 附带说明一下,我怀疑这可能与将变量声明为静态变量有关,因此我尝试将其声明为全局变量(使用extern),以达到相同的效果。


An edit to address some of the comments below: 编辑以解决以下一些评论:

  • The thread is an instance of an OpenThreads object. 该线程是OpenThreads对象的实例。 No MS-specific stuff, here. 这里没有MS专用的内容。 The instance is static. 该实例是静态的。

  • addFiles() is not templated addFiles()未模板化

  • 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. 我绝对肯定addFiles()调用是罪魁祸首。

  • 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. OSG是高性能的,有关错误预测的评论可能正确无误。 Research forthcoming... 正在研究...

Code for the FileStampThread class: FileStampThread类的代码:

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? 难以置信发生了什么,发布和调试版本上的行为相同?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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