I have the following code sample:
void foo(int size)
{
std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t>(size);
for(int i = 0; i < size; i += 2)
{
data[i] = 1;
data[i + 1] = 2;
}
}
The actual code does actual calculations, but that does not matter for this question and was removed for simplicity's sake.
When compiling and running this code with optimizations turned on, everything works great and runs fast.
However, when running without any optimizations, this code is much slower than:
void foo(int size)
{
std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t>(size);
uint8_t* dataPtr = data.get();
for(int i = 0; i < size; i += 2)
{
dataPtr[i] = 1;
dataPtr[i + 1] = 2;
}
}
To investigate this a bit more, I ran multiple variations of the indexing operator with this dynamic array through compiler explorer (godbolt.org).
Compiling with clang and -O3 optimizations, all variations result in the same assembly.
However, compiling without any optimizations, the unique_ptr only sample has a call to the unique_ptr operator[], which seems to be causing the slowdown.
Why is the operator[] of the unique_ptr much slower without optimizations? From the documentation I see that the operator[] should be equivalent to unique_ptr.get()[].
Is it doing some safety checks without optimizations? If so, which ones?
question from:
https://stackoverflow.com/questions/65643041/stdunique-ptr-operator-vs-raw-ptr-dynamic-array-operator 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…