简体   繁体   English

局部变量与 Class 变量编译器优化; 有效与无效

[英]Local Variables vs. Class Variables Compiler Optimization; Works vs. Doesn't Work

I have an example of code where a straightforward optimization is not working when structured as class variables, yet works as local variables;我有一个代码示例,当结构为 class 变量时,直接优化不起作用,但用作局部变量; I want to know: why is the optimization not happening on the class variables formulation?我想知道:为什么 class 变量公式没有发生优化?

The intent of my example code is to have a class that is either enabled or disabled at construction and possibly changed during it's lifetime.我的示例代码的目的是让 class 在构造时启用或禁用,并且可能在其生命周期内更改。 I expect that, when the object is disabled for it's whole lifetime, the compiler would optimize away all code that conditionally executes when the object is enabled.我希望,当 object 在其整个生命周期内被禁用时,编译器会优化掉所有在启用 object 时有条件地执行的代码。

Specifically, I have a std::ofstream that I only want to write to when "enabled".具体来说,我有一个 std::ofstream ,我只想在“启用”时写入。 When disabled, I want all formatted-output to be skipped.禁用时,我希望跳过所有格式化输出。 ( My real class does it's own, non-trivial message-formatting. ) (我真正的 class 是自己的,非平凡的消息格式。)

I discovered that when I formulate this as a class, I don't get the optimizations I expect.我发现当我将其表述为 class 时,我没有得到我期望的优化。 However, if I replicate the code all as local variables, I do see the expected behavior.但是,如果我将代码全部复制为局部变量,我确实会看到预期的行为。

Additionally, I discovered that if I don't make std::ofstream calls like 'open', 'exceptions', or 'clear' anywhere in the body of the example class's methods, I also get the expected optimizations.此外,我发现如果我不在示例类的方法主体的任何地方进行 std::ofstream 调用,如“open”、“exceptions”或“clear”,我也会得到预期的优化。 ( However, my design requires making such calls on std::ofstream, so for me it's a moot point. ) The below code uses the MACRO DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR to allow one to try this case. (但是,我的设计需要在 std::ofstream 上进行此类调用,所以对我来说这是一个有争议的问题。)下面的代码使用 MACRO DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR 来允许尝试这种情况。

My example code uses 'asm' expressions to insert comments into the generated assembly-code.我的示例代码使用“asm”表达式将注释插入到生成的汇编代码中。 If one inspects the output of the compiler in assembly, I expect there to be no assembly between the 'disabled-test' comments.如果在汇编中检查编译器的 output,我希望在“禁用测试”注释之间没有汇编。 I'm observing assembly between the 'class disabled-test' comments, yet no assembly between the 'locals disabled-test' comments.我正在观察“class disabled-test”注释之间的组装,但“locals disabled-test”注释之间没有组装。

The input C++ code:输入 C++ 代码:

#include <fstream> // ofstream

#define DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR 0

class Test_Ofstream
{
public:
    Test_Ofstream( const char a_filename[],
                   bool a_b_enabled )
    #if DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR
        : m_ofstream( a_filename ),
          m_b_enabled( a_b_enabled )
    {
    }
    #else
        : m_ofstream(),
          m_b_enabled( a_b_enabled )
    {
        m_ofstream.open( a_filename );
    }
    #endif

    void write_test()
    {
        if( m_b_enabled )
        {
            m_ofstream << "Some text.\n";
        }
    }

private:
    std::ofstream m_ofstream;
    bool m_b_enabled;
};

int main( int argc, char* argv[] )
{
    {
        Test_Ofstream test_ofstream( "test.txt", true );
        asm( "# BEGIN class enabled-test" );
        test_ofstream.write_test();
        asm( "# END class enabled-test" );
    }

    {
        Test_Ofstream test_ofstream( "test.txt", false );
        asm( "# BEGIN class disabled-test" );
        test_ofstream.write_test();
        asm( "# END class disabled-test" );
    }

    {
        bool b_enabled = true;
        #if DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR
        std::ofstream test_ofstream( "test.txt" );
        #else
        std::ofstream test_ofstream;
        test_ofstream.open( "test.txt" );
        #endif
        asm( "# BEGIN locals enabled-test" );
        if( b_enabled )
        {
            test_ofstream << "Some text.\n";
        }
        asm( "# END locals enabled-test" );
    }

    {
        bool b_enabled = false;
        #if DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR
        std::ofstream test_ofstream( "test.txt" );
        #else
        std::ofstream test_ofstream;
        test_ofstream.open( "test.txt" );
        #endif
        asm( "# BEGIN locals disabled-test" );
        if( b_enabled )
        {
            test_ofstream << "Some text.\n";
        }
        asm( "# END locals disabled-test" );
    }

    return 0;
}

The output assembly code: output 汇编代码:

##### Cut here. #####
#APP
# 53 "test_ofstream_optimization.cpp" 1
        # BEGIN class disabled-test
# 0 "" 2
#NO_APP
        cmpb        $0, 596(%esp)
        je  .L22
        movl        $.LC1, 4(%esp)
        movl        %ebx, (%esp)
.LEHB9:
        call        _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
.LEHE9:
.L22:
#APP
# 55 "test_ofstream_optimization.cpp" 1
        # END class disabled-test
# 0 "" 2
#NO_APP
##### Cut here. #####
#APP
# 116 "test_ofstream_optimization.cpp" 1
        # BEGIN locals disabled-test
# 0 "" 2
# 121 "test_ofstream_optimization.cpp" 1
        # END locals disabled-test
# 0 "" 2
#NO_APP
##### Cut here. #####

I realize that this is possibly tied to the compiler I'm using, which is: g++-4.6 (Debian 4.6.1-4) 4.6.1;我意识到这可能与我正在使用的编译器有关,即:g++-4.6 (Debian 4.6.1-4) 4.6.1; compiler flags: -Wall -S -O2.编译器标志:-Wall -S -O2。 However, this seems like such a simple optimization I find it hard to believe it could be the compiler messing up.然而,这似乎是一个如此简单的优化,我很难相信它可能是编译器搞砸了。

Any help, insight or guidance is greatly appreciated.非常感谢任何帮助、见解或指导。

Pretty simple.很简单。 When you write the code directly as a local variable, then the code is inlined and the compiler performs the constant folding.当您直接将代码编写为局部变量时,代码会被内联,编译器会执行常量折叠。 When you're in the class scope, then the code is not inlined and the value of m_b_enabled is unknown, so the compiler has to perform the call.当您在 class scope 中时,代码未内联且m_b_enabled的值未知,因此编译器必须执行调用。 To prove that the code was semantically equal and perform this optimization, not just that call would have to be inlined, but every access to the class.为了证明代码在语义上是相等的并执行此优化,不仅必须内联该调用,而且每次访问 class。 The compiler may well decide that inlining the class would not yield sufficient benefit.编译器很可能认为内联 class 不会产生足够的好处。 Compilers can also choose not to inline code because they don't know how, and inline asm expressions is exactly the kind of thing that could cause them to do it, as the compiler does not know how to handle your assembly code.编译器也可以选择不内联代码,因为他们不知道如何内联,而内联asm表达式正是可能导致他们这样做的事情,因为编译器不知道如何处理您的汇编代码。

Usually, you would place a breakpoint and inspect the disassembly.通常,您会放置一个断点并检查反汇编。 That's what I'd do in Visual Studio, anyway.无论如何,这就是我在 Visual Studio 中所做的。 Inline assembler of any kind can be so damaging to the optimizer.任何类型的内联汇编器都可能对优化器造成如此大的破坏。

When I removed the assembler expressions, then Visual Studio inlined the code- and promptly didn't perform the optimization anyway.当我删除汇编器表达式时,Visual Studio 内联了代码,并且立即没有执行优化。 The problem with stacking optimization passes is that you can never get the right order to find all potential optimizations.堆叠优化通道的问题是您永远无法获得正确的顺序来找到所有潜在的优化。

As you say, this will depend on compiler.正如你所说,这将取决于编译器。 But my guess:但我的猜测是:

The optimizer can prove that no other code can ever modify object bool b_enabled , since it's local and you never take its address or bind a reference to it.优化器可以证明没有其他代码可以修改 object bool b_enabled ,因为它是本地的,您永远不会获取它的地址或绑定对它的引用。 The local version is easily optimized.本地版本很容易优化。

When DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR is true, the Test_Ofstream constructor:DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR为真时, Test_Ofstream构造函数:

  • Calls the constructor ofstream(const char*)调用ofstream(const char*)的构造函数
  • Initializes member m_b_enabled初始化成员m_b_enabled

Since there are no operations between initializing test_ofstream.m_b_enabled and testing it, this optimization is only a bit trickier, but it sounds like g++ still manages it.由于初始化test_ofstream.m_b_enabled和测试之间没有任何操作,所以这个优化只是有点棘手,但听起来 g++ 仍然管理它。

When DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR is false, the Test_Ofstream constructor:DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR为 false 时, Test_Ofstream构造函数:

  • Calls the ofstream default constructor调用ofstream默认构造函数
  • Initializes member m_b_enabled初始化成员m_b_enabled
  • Calls m_ofstream.open(const char*)调用m_ofstream.open(const char*)

The optimizer is not allowed to assume that ofstream::open will not change test_ofstream.m_b_enabled .不允许优化器假设ofstream::open不会改变test_ofstream.m_b_enabled We know it shouldn't, but in theory that non-inline library function could figure out the complete object test_ofstream which contains its 'this' argument, and modify it that way.我们知道它不应该,但理论上非内联库 function 可以找出包含其“this”参数的完整 object test_ofstream ,并以这种方式修改它。

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

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