一个优化的编译器从std :: unique_ptr中删除所有的运行时成本? [英] Could an optimizing compiler remove all runtime costs from std::unique_ptr?
问题描述
阅读 std :: unique_ptr > http://en.cppreference.com/w/cpp/memory/unique_ptr ,我天真的印象是,一个聪明的编译器可以替换 unique_ptr $ c $的正确使用c>使用裸指针,并且当
unique_ptr
被销毁时,只需放在 delete
这是真的吗?如果是这样,任何主流优化编译器实际上是这样做的吗?如果没有,是否可以用 unique_ptr
的编译时安全优点编写一些可以优化为没有运行时成本(空间或时间) ?
Reading about std::unique_ptr
at http://en.cppreference.com/w/cpp/memory/unique_ptr, my naive impression is that a smart enough compiler could replace correct uses of unique_ptr
with bare pointers and just put in a delete
when the unique_ptr
s get destroyed. Is this actually the case? If so, do any of the mainstream optimizing compilers actually do this? If not, would it be possible to write something with some/all of unique_ptr
s compile-time safety benefits that could be optimized to have no runtime cost (in space or time)?
注意那些(正确)担心过早优化:这里的答案不会阻止我使用 std :: unique_ptr
,我只是好奇,如果它是一个真棒的工具或只是一个真棒的工具。
Note to those (properly) worried about premature optimization: The answer here won't stop me from using std::unique_ptr
, I'm just curious if it's a really awesome tool or just an awesome one.
EDIT 2013/07/21 20:07 EST :
好的,所以我测试了下面的程序(如果这里有什么问题,请告诉我):
OK, so I tested with the following program (please let me know if there's something wrong with this):
#include <climits>
#include <chrono>
#include <memory>
#include <iostream>
static const size_t iterations = 100;
int main (int argc, char ** argv) {
std::chrono::steady_clock::rep smart[iterations];
std::chrono::steady_clock::rep dumb[iterations];
volatile int contents;
for (size_t i = 0; i < iterations; i++) {
auto start = std::chrono::steady_clock::now();
{
std::unique_ptr<int> smart_ptr(new int(5));
for (unsigned int j = 0; j < UINT_MAX; j++)
contents = *smart_ptr;
}
auto middle = std::chrono::steady_clock::now();
{
int *dumb_ptr = new int(10);
try {
for (unsigned int j = 0; j < UINT_MAX; j++)
contents = *dumb_ptr;
delete dumb_ptr;
} catch (...) {
delete dumb_ptr;
throw;
}
}
auto end = std::chrono::steady_clock::now();
smart[i] = (middle - start).count();
dumb[i] = (end - middle).count();
}
std::chrono::steady_clock::rep smartAvg;
std::chrono::steady_clock::rep dumbAvg;
for (size_t i = 0; i < iterations; i++) {
smartAvg += smart[i];
dumbAvg += dumb[i];
}
smartAvg /= iterations;
dumbAvg /= iterations;
std::cerr << "Smart: " << smartAvg << " Dumb: " << dumbAvg << std::endl;
return contents;
}
使用 g ++编译g ++ 4.7.3 - std = c ++ 11 -O3 test.cc
给出 Smart:1130859 Dumb:1130005
,这意味着智能指针在0.076%
Compiling with g++ 4.7.3 using g++ --std=c++11 -O3 test.cc
gave Smart: 1130859 Dumb: 1130005
, which means the smart pointer is within 0.076% of the dumb pointer, which is almost surely noise.
推荐答案
这肯定是我期望从任何合理有效的编译器,因为它只是一个简单指针的包装器和一个调用 delete
的析构函数,因此编译器生成的机器代码为:
It would certainly be my expectation from any reasonably competent compiler, since it is just a wrapper around a simple pointer and a destructor that calls delete
, so the machne code generated by the compiler for:
x *p = new X;
... do stuff with p.
delete p;
和
unique_ptr<X> p(new X);
... do stuff with p;
将是完全相同的代码。
will be exactly the same code.
这篇关于一个优化的编译器从std :: unique_ptr中删除所有的运行时成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!