Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > placement new overhead

Reply
Thread Tools

placement new overhead

 
 
itaj sherman
Guest
Posts: n/a
 
      02-17-2011
On Feb 17, 2:16 pm, James Kanze <(E-Mail Removed)> wrote:
> On Feb 16, 12:03 pm, "Martin B." <(E-Mail Removed)> wrote:
>


>
> 3.7.3.1 (which specifies the requirements for allocator
> functions), particularly 3.7.3.1/2: "If [the allocation
> function] is successful, it shall return the address of the
> start of a block of storage whose length in bytes shall be at
> least as large as the requested size" and 3.7.3.1/3: "[...] If
> an allocation function declared with an empty
> exception-specification (15.4), throw(), fails to allocate
> storage, it shall return a null pointer." Together, this
> specifies clearly that the only legal return values of an
> allocator function (and all "operator new" are allocator
> functions) are a pointer to valid memory and a null pointer, and
> the latter only if the allocator function has an empty exception
> specifier.
>
> Since "void* operator new(size_t size, void* ptr) throw()"
> returns ptr directly, it stands to reason that ptr must meet the
> requirements for a legal return value of this function. Without
> the empty exception specification, that would mean a valid
> pointer to the requested size; with the empty exception
> specification, a null pointer is also valid.
>
> --
> James Kanze


By 5.4.3/13, I thought such allocator was allowed to either throw
bad_except or return null. It doesn't say that, but it is confusing.
I did not notice 3.7.3.1/3:
If an allocation function declared with a non-throwing exception-
specification (15.4) fails to allocate storage, it shall return a null
pointer. Any other allocation function that fails to allocate storage
shall indicate failure only by throwing an exception of a type that
would match a handler (15.3) of type std::bad_alloc (18.6.2.1).

In that case, maybe it can currently be solved with the optimizer?

How about doing ourselves what I suggested in my other post:

enum non_null_placement_t { non_null_placement };

// non-noexcept allocator
void* operator new( std::size_t, void* ptr,
std::non_null_placement_t )
{
return ptr;
}

And then:
new( p, std::non_null_placement ) T( b );
Has the required semantics.

Maybe in this case, the optimizer can assume that the allocator
doesn't return null (because without noexcept), and ommit the
condition.
And it can check its code and see that it neither throws an exception
(if it matters at all).

itaj
 
Reply With Quote
 
 
 
 
itaj sherman
Guest
Posts: n/a
 
      02-17-2011
On Feb 17, 3:12*pm, itaj sherman <(E-Mail Removed)> wrote:
> On Feb 17, 2:16 pm, James Kanze <(E-Mail Removed)> wrote:
>
>


>
> How about doing ourselves what I suggested in my other post:
>
> enum non_null_placement_t { non_null_placement };
>
> // non-noexcept allocator
> void* operator new( std::size_t, void* ptr,
> std::non_null_placement_t )
> {
> * * * * return ptr;
>
> }


::non_null_placement_t

>
> And then:
> new( p, std::non_null_placement ) T( b );
> Has the required semantics.


new( p, non_null_placement ) T( b );

itaj
 
Reply With Quote
 
 
 
 
itaj sherman
Guest
Posts: n/a
 
      02-17-2011
On Feb 16, 6:00 pm, itaj sherman <(E-Mail Removed)> wrote:
> On Feb 16, 5:38 pm, itaj sherman <(E-Mail Removed)> wrote:
>
> > On Feb 16, 2:03 pm, "Martin B." <(E-Mail Removed)> 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:
>


As Kanze pointed out in his reply: An allocator function without
noexcept is not allowed to return null (instead it could throw
std::bad_alloc).

If at all, using that would produce the same results as my hipothetic
change (i.e. this change would improve nothing).

itaj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How much overhead for a new templated class? jacob navia C++ 9 05-22-2012 08:09 AM
overriding operator new and accessing placement new Mark P C++ 6 04-27-2005 04:17 AM
New Window size and placement gcc HTML 11 06-10-2004 05:14 PM
placement new , is this acceptable? lallous C++ 10 03-06-2004 03:53 AM
is it placement new? Wenjie C++ 0 06-26-2003 11:16 AM



Advertisments