Re: placement new overhead
On Feb 16, 5:38 pm, itaj sherman <itajsher...@gmail.com> wrote:
On Feb 16, 2:03 pm, "Martin B." <0xCDCDC...@gmx.at> wrote:
Maybe if the optimizer could somehow prove that p is null, it would
ommit the comparision.
Ofcourse, I meant prove that it isn't null.
Appart from that, I should say, that as the OP, I too see that it can
cause a perfromance problem. The c++ performance attitude demands that
core operations like placement construction do not perform
unneccessary comparisons - that could be instead the responsibility of
the programmer to assure.
To further explain the problem, I explain how I think it could be
solved with a simple change in the standard:
-- one way --
In 18.6.1.3/3
The section about the pre-defined:
void* operator new( std::size_t, void* ) noexcept;
Add a remark that this function must not be called with a second
parameter null, and that would be undefined behaviour.
That implies that the above expression:
new( p ) T( b );
Has defined behaviour only if p is not null. Making it the
responsibility of the programmer to assure that or compare himself.
This means the compiler can always assume that p != NULL, and ommit
the comparison in this case.
-- another way --
If that first way is objected due to possibly breaking existing code,
it can be tweaked with a dummy parameter for that case:
pre defined enum:
namespace std
{
enum non_null_placement_t { non_null_placement };
}
and pre-defined allocation function:
void* operator new( std::size_t, void* ptr,
std::non_null_placement_t ) noexcept
{
return ptr;
}
with the requirement of ptr != NULL, otherwise undefined-behaviour.
And then:
new( p, std::non_null_placement ) T( b );
Has the required semantics.
itaj