局部变量与类变量编译器优化;有效与无效
我有一个代码示例,其中当结构为类变量时,直接优化不起作用,但作为局部变量起作用;我想知道:为什么类变量公式没有发生优化?
我的示例代码的目的是拥有一个在构造时启用或禁用并可能在其生命周期内更改的类。我希望,当对象在其整个生命周期内被禁用时,编译器将优化在启用对象时有条件执行的所有代码。
具体来说,我有一个 std::ofstream 我只想在“启用”时写入。禁用时,我希望跳过所有格式化输出。 (我真正的类有它自己的、不平凡的消息格式。)
我发现当我将其表述为一个类时,我没有得到我期望的优化。但是,如果我将代码全部复制为局部变量,我确实会看到预期的行为。
此外,我发现,如果我不在示例类方法主体中的任何位置进行 std::ofstream 调用,例如“open”、“exceptions”或“clear”,我也会获得预期的优化。 (但是,我的设计需要对 std::ofstream 进行此类调用,所以对我来说这是一个没有实际意义的问题。)下面的代码使用 MACRO DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR 来允许尝试这种情况。
我的示例代码使用“asm”表达式将注释插入到生成的汇编代码中。如果有人在汇编中检查编译器的输出,我希望“disabled-test”注释之间不会有汇编。我正在观察“类禁用测试”注释之间的汇编,但“本地禁用测试”注释之间没有汇编。
输入 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;
}
输出汇编代码:
##### 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. #####
我意识到这可能与我正在使用的编译器有关,即: g++-4.6 (Debian 4.6.1-4) 4.6.1;编译器标志:-Wall -S -O2。然而,这似乎是一个如此简单的优化,我发现很难相信它可能是编译器搞砸了。
非常感谢任何帮助、见解或指导。
I have an example of code where a straightforward optimization is not working when structured as class variables, yet works as local variables; I want to know: why is the optimization not happening on the class variables formulation?
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. 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.
Specifically, I have a std::ofstream that I only want to write to when "enabled". When disabled, I want all formatted-output to be skipped. ( My real class does it's own, non-trivial message-formatting. )
I discovered that when I formulate this as a class, I don't get the optimizations I expect. 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. ( 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.
My example code uses 'asm' expressions to insert comments into the generated assembly-code. If one inspects the output of the compiler in assembly, I expect there to be no assembly between the 'disabled-test' comments. I'm observing assembly between the 'class disabled-test' comments, yet no assembly between the 'locals disabled-test' comments.
The input C++ code:
#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:
##### 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; compiler flags: -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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很简单。当您直接将代码编写为局部变量时,代码将被内联,编译器将执行常量折叠。当您位于类作用域中时,代码不会内联,并且
m_b_enabled
的值未知,因此编译器必须执行调用。为了证明代码在语义上是相等的并执行此优化,不仅必须内联该调用,还必须内联对类的每次访问。编译器很可能认为内联类不会产生足够的好处。编译器还可以选择不内联代码,因为它们不知道如何内联,而内联asm
表达式正是可能导致它们这样做的类型,因为编译器不知道如何处理你的汇编代码。通常,您会放置一个断点并检查反汇编。无论如何,这就是我在 Visual Studio 中要做的事情。任何类型的内联汇编器都可能对优化器造成很大的损害。
当我删除汇编器表达式时,Visual Studio 内联了代码,并且立即没有执行优化。堆叠优化过程的问题在于,您永远无法获得正确的顺序来找到所有潜在的优化。
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. 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. The compiler may well decide that inlining the class would not yield sufficient benefit. Compilers can also choose not to inline code because they don't know how, and inlineasm
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.Usually, you would place a breakpoint and inspect the disassembly. That's what I'd do in Visual Studio, anyway. 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. The problem with stacking optimization passes is that you can never get the right order to find all potential optimizations.
正如你所说,这取决于编译器。但我的猜测:
优化器可以证明没有其他代码可以修改对象
bool b_enabled
,因为它是本地的,并且您永远不会获取它的地址或绑定对它的引用。本地版本很容易优化。当 DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR 为 true 时,
Test_Ofstream
构造函数:ofstream(const char*)
m_b_enabled
初始化 初始化
test_ofstream.m_b_enabled
和测试之间没有任何操作,这种优化只是有点棘手,但听起来像g++ 仍然可以管理它。当 DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR 为 false 时,
Test_Ofstream
构造函数:ofstream
默认构造函数m_b_enabled
m_ofstream .open(const char*)
优化器不允许假设
ofstream::open
不会改变test_ofstream.m_b_enabled
。我们知道不应该,但理论上非内联库函数可以找出包含“this”参数的完整对象test_ofstream
,并以这种方式修改它。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. The local version is easily optimized.When
DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR
is true, theTest_Ofstream
constructor:ofstream(const char*)
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.When
DISABLE_OPEN_OFSTREAM_AFTER_CONSTRUCTOR
is false, theTest_Ofstream
constructor:ofstream
default constructorm_b_enabled
m_ofstream.open(const char*)
The optimizer is not allowed to assume that
ofstream::open
will not changetest_ofstream.m_b_enabled
. We know it shouldn't, but in theory that non-inline library function could figure out the complete objecttest_ofstream
which contains its 'this' argument, and modify it that way.