placement new

K

karthikbalaguru

Hi,

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in

pre-allocated buffer using

'placement new'.

Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf
}

So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the

allocation in

pre-allocated buffer

using 'placement

new' for 'hi'.

So, How could 'placement new' be stated to be faster ?
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison
with 'new' as it indirectly depends on heap ?

Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?

Thx in advans,
Karthik Balaguru.
 
V

Victor Bazarov

karthikbalaguru said:
I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

The benefit comes when the allocation does not happen for several
constructions, like when 'std::vector' shrinks and then resizes,
it doesn't deallocate its storage but instead keeps destructing and
construcing elements in place (using placement new).

V
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

Placement new is faster because it does not have to allocate the memory
before it constructs an object, normal new first allocates memory and
then constructs the object. One example of this is std::vector which
allocates more memory than it uses and then constructs new elements into
this memory.

If we assume that all allocations take the same amount of time then
allocating one large chunk of memory and then constructing N objects
into that chunk takes A+N*C time (A is allocation time, and C is
construction time). Making one allocation for each construction takes
N*(A+C) time, which is (N-1)*A longer.
I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in

pre-allocated buffer using

'placement new'.

Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf
}

I'm not an expert on the area but I'm quite sure that it's the other way
around; new first allocates memory and then uses placement new to
construct the object into this memory.
 
O

Ondra Holub

Hi,

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in

pre-allocated buffer using

'placement new'.

Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf

}

So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the

allocation in

pre-allocated buffer

using 'placement

new' for 'hi'.

So, How could 'placement new' be stated to be faster ?
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison
with 'new' as it indirectly depends on heap ?

Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?

Thx in advans,
Karthik Balaguru.

Hi.

You can preallocate big buffer once and then use many placement new
operators. You can this way get better performance and less fragmented
memory (especially when allocating small amounts of memory). However
you have to manage this part of memory on your own, so what you gain
depends on quality how is your preallocated buffer managed.
 
K

karthikbalaguru

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.
Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?
I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in
pre-allocated buffer using
'placement new'.
Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf

So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the
allocation in
pre-allocated buffer
using 'placement
new' for 'hi'.
So, How could 'placement new' be stated to be faster ?
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison
with 'new' as it indirectly depends on heap ?
Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?
Thx in advans,
Karthik Balaguru.

Hi.

You can preallocate big buffer once and then use many placement new
operators. You can this way get better performance and less fragmented
memory (especially when allocating small amounts of memory). However
you have to manage this part of memory on your own, so what you gain
depends on quality how is your preallocated buffer managed.- Hide quoted text -

Managing Buffers on our own - This sounds something like array
management :(:(
There is every possibility that the Buffer can be manipulated as it is
accessible under a variable name for a programmer. So, 'placement new'
is inturn increasing the burden on the programmer by making the
programmer to take special care of that buffer . What do you think ?
But, normal 'new' does not make the programmer to take care of these.
So, 'new' appears to be
efficient than 'placement new' here . What do you think ?

Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better. But, that better timing
over the normal 'new' does not matter as lot of time will be consumed
to design in such a way to avoid the corruption of buffer(buffer
management) and to fix the errors due to improper buffer management .
What do you think ?

So, practically, what is the need of 'placement new' ? Any ideas ?

Thx in advans,
Karthik Balaguru
 
A

Andre Kostur

Hi,

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Depends on what you're trying to measure. Yes, 'placement new' is
faster than a simple 'new'. But that's because 'new' has to do more
work.
Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

That's a different question. And it might be faster and it might be
slower. Depends on what you're doing to allocate the buffer. Your own
allocator may or may not be faster than the "default" allocator.
I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in

pre-allocated buffer using

'placement new'.

Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf
}

So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the

allocation in

pre-allocated buffer

using 'placement

new' for 'hi'.

So, How could 'placement new' be stated to be faster ?

Because you're not looking at the whole question. If you already have
the memory block through some other mechanism, then you don't need to
spend time talking to the global allocator to get more memory.
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison

No.. the pre-allocated buffer may not be in heap.... neither does it
necessarily come from new.
with 'new' as it indirectly depends on heap ?

Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?

How about: I want to construct an object in a shared memory segment (Ok,
this bit is somewhat offtopic). I can't use the normal new since it
isn't allocated off of the heap. So I use whatever the allocation
method is for the shared memory segment and then use placement new to
construct the object in that memory block.

Or look at many implementations of vector. It will have allocated some
chunk of memory for all of the current objects in the vector, plus some
more for some future objects (Probably. This is why capacity() doesn't
necessarily equal size().). When you go and push_back() another object
into the vector, all vector needs to do is use a placement new on the
next "empty" space in the vector to copy-construct the object in place.
vector doesn't need to get more memory, it already has it. (Not
counting the case where capacity() == size() before the push_back. Then
the vector has to go get a new bigger space to hold the vector, copy
over all of the existing objects, and then placement new the next
object. And delete the previous memory.).
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.
Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?
I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in
pre-allocated buffer using
'placement new'.
Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf

So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the
allocation in
pre-allocated buffer
using 'placement
new' for 'hi'.
So, How could 'placement new' be stated to be faster ?
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison
with 'new' as it indirectly depends on heap ?
Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?
Thx in advans,
Karthik Balaguru.

Hi.

You can preallocate big buffer once and then use many placement new
operators. You can this way get better performance and less fragmented
memory (especially when allocating small amounts of memory). However
you have to manage this part of memory on your own, so what you gain
depends on quality how is your preallocated buffer managed.- Hide quoted text -

Managing Buffers on our own - This sounds something like array
management :(:(
There is every possibility that the Buffer can be manipulated as it is
accessible under a variable name for a programmer. So, 'placement new'
is inturn increasing the burden on the programmer by making the
programmer to take special care of that buffer . What do you think ?
But, normal 'new' does not make the programmer to take care of these.
So, 'new' appears to be
efficient than 'placement new' here . What do you think ?

Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better. But, that better timing
over the normal 'new' does not matter as lot of time will be consumed
to design in such a way to avoid the corruption of buffer(buffer
management) and to fix the errors due to improper buffer management .
What do you think ?

So, practically, what is the need of 'placement new' ? Any ideas ?

It is used every time you write something like

Foo* f = new Foo();

Read http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.10
to see how (line 4 in the code).
 
A

Andre Kostur

Managing Buffers on our own - This sounds something like array
management :(:(
There is every possibility that the Buffer can be manipulated as it is
accessible under a variable name for a programmer. So, 'placement new'
is inturn increasing the burden on the programmer by making the
programmer to take special care of that buffer . What do you think ?
But, normal 'new' does not make the programmer to take care of these.
So, 'new' appears to be
efficient than 'placement new' here . What do you think ?

You're using a rather vague term. What is "efficient"? Efficient in terms
of what? And recall all of the standard admonitions about "Premature
optimization is the root of all evil.". Measure first, then worry about
optimizing.
Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better. But, that better timing
over the normal 'new' does not matter as lot of time will be consumed
to design in such a way to avoid the corruption of buffer(buffer
management) and to fix the errors due to improper buffer management .
What do you think ?

I think you already have drawn a conclusion and are looking for arguments
to support it instead of examining the entire issue impartially.
So, practically, what is the need of 'placement new' ? Any ideas ?

You already have at least one answer to this question. std::vector.
 
R

Rolf Magnus

karthikbalaguru said:
Managing Buffers on our own - This sounds something like array
management :(:(

Placement new does _not_ manage memory for you. That's what makes it
different from the regular operator new. Placement new just constructs the
object into the memory you give it, while the non-placement new allocates
that memory. Managing memory on your own might be faster, but probably only
in special cases. The default memory managers are typically optimized very
well for general use.
There is every possibility that the Buffer can be manipulated as it is
accessible under a variable name for a programmer. So, 'placement new'
is inturn increasing the burden on the programmer by making the
programmer to take special care of that buffer . What do you think ?

Well, what do you expect to gain from it?
But, normal 'new' does not make the programmer to take care of these.
So, 'new' appears to be efficient than 'placement new' here . What do you
think ?

There are different kinds of "efficient".
Maybe time-wise, when construcing large number of objects into a
chunk, 'placement new' appears to be better.

I don't think so, unless you want to allocate the chunk first, and then some
time later, construct the objects.
But, that better timing over the normal 'new' does not matter as lot of
time will be consumed to design in such a way to avoid the corruption of
buffer(buffer management) and to fix the errors due to improper buffer
management . What do you think ?

You are right about that observation. Also, after all that additional work,
you might find out that your own memory management isn't much faster, or
even that it's slower than the built-in memory manager.
This also smells like the "premature optimization" someone already
mentioned. Do you currently have any performance issues, and did you
determine those to be due to operator new being slow? If not, why bother?
So, practically, what is the need of 'placement new' ? Any ideas ?

Placement new is about seperating memory allocation from object
construction. As an example, std::vector does that. It can allocate a chunk
of memory, and then, some time later, construct objects into that memory.
For example:

std::vector<MyCoolType> v;
v.reserve(1000);
v.push_back(MyCoolType("I'm very cool"));

This code reserves memory for 1000 objects, which means that the vector will
allocate enough space for at least those 1000 objects, but it won't
construct any actual objects. The push_back call copy-constructs its
argument into the reserved space, internally using placement new to do
that. push_back can automatically increase the size if needed, so the
reseve wouldn't strictly be needed for it to work. But even if you don't
reseve(), the vector will typically resize its reserved memory in larger
steps than single objects. Without placement new, it would have to
re-allocate the buffer (i.e. allocate a new one, copy everything over,
destroy the old one) for every single added object, since there would be no
way to separate allocation from construction.
 
K

karthikbalaguru

karthikbalaguru said:
I find that articles stating that 'placement new' constructs an object
on a pre-allocated buffer and so takes less time.

Depends on what you're trying to measure. Yes, 'placement new' is
faster than a simple 'new'. But that's because 'new' has to do more
work.
Actually, we have to consider the allocation of the buffer and then
the construction of object using 'placement new'. So, Effectively it
should be taking more time . What do you think ?

That's a different question. And it might be faster and it might be
slower. Depends on what you're doing to allocate the buffer. Your own
allocator may or may not be faster than the "default" allocator.




I think,
'placement new' time = Time for creation of pre-allocated buffer +
Time for doing the allocation in
pre-allocated buffer using
'placement new'.
Ex :
void placementnew{
char *buf = new char[1000]; // pre-allocated buffer
string *p = new(buf)string("hi"); // placement new in turn depends
on buf
}
So, i think, it should be as below :
'placement new' time(p) = Time for creation of pre-allocated buffer
called 'buf' + Time for doing the
allocation in
pre-allocated buffer
using 'placement
new' for 'hi'.
So, How could 'placement new' be stated to be faster ?

Because you're not looking at the whole question. If you already have
the memory block through some other mechanism, then you don't need to
spend time talking to the global allocator to get more memory.
Further, what is the advantage in using 'placement new'. It in turn is
depending on the pre-allocated buffer in heap that is assigned using
'new'. So, how does 'placement new' prove to be good in comparison

No.. the pre-allocated buffer may not be in heap.... neither does it
necessarily come from new.
with 'new' as it indirectly depends on heap ?
Why was 'placement new' introduced in c++ ? What are the practical
areas of application of 'placement new' ?

How about: I want to construct an object in a shared memory segment (Ok,
this bit is somewhat offtopic). I can't use the normal new since it
isn't allocated off of the heap. So I use whatever the allocation
method is for the shared memory segment and then use placement new to
construct the object in that memory block.

Or look at many implementations of vector. It will have allocated some
chunk of memory for all of the current objects in the vector, plus some
more for some future objects (Probably. This is why capacity() doesn't
necessarily equal size().). When you go and push_back() another object
into the vector, all vector needs to do is use a placement new on the
next "empty" space in the vector to copy-construct the object in place.
vector doesn't need to get more memory, it already has it. (Not
counting the case where capacity() == size() before the push_back. Then
the vector has to go get a new bigger space to hold the vector, copy
over all of the existing objects, and then placement new the next
object. And delete the previous memory.).- Hide quoted text -

- Show quoted text -

Interesting !! Thx for the info.

Karthik Balaguru
 
J

James Kanze

Don't waste your time with such articles. The authors obviously
don't know what they are talking about.
Depends on what you're trying to measure. Yes, 'placement new' is
faster than a simple 'new'. But that's because 'new' has to do more
work.

Yes and no. Regretfully, the term "placement new" doesn't
really mean what one would expect. It refers to any new
expression which has additional arguments, and which thus calls
a placement new allocation function. So:

T* p = new T ;
// calls: operator new( sizeof( T ) )
T* p = new ( pool ) T ;
// calls operator new( sizeof( T ), pool )

(In both cases, after calling the allocation function, it then
calls the constructor of T.) Obviously, the total runtime will
depend somewhat on the execution time of the allocator function
(which has the name "operator new", just to add to the
confusion) called. The standard defines several placement new:

T* p = new (std::nothrow) T ;
// calls operator new( sizeof(T), std::nothrow)...
// which returns a null pointer instead of raising
// std::bad_alloc in case of failure.
char* buffer ;
T* p = new ( buffer ) T ;
// calls operator new( sizeof(T), buffer )...
// if <new> has been included, and no other
// operator new functions have been declared,
// overload resolution will choose the standard
// operator new( size_t, void* ) function. Which
// is defined to return the second argument, no
// questions asked.

It is the second which one usually thinks of when placement new
is talked about, because it ends up doing very much what the
name "placement new" suggests. The mechanism is far more
general, however, and this last form ends up calling the
constructor at a pre-determined address only because the
semantics of the allocator function are so defined.

Of course, it's hard to imagine a system where just returning
the address wouldn't be faster than actually allocating memory,
but this is really irrelevant. The two operations have
completely different semantics, and you can't simply replace one
with the other, in order to speed up your program. The choice
of which one to use must be based entirely one the needed
semantics.

In the case of this last "placement new", it is normally used
whenever you want allocation and initialization separated, i.e.
to occur at a different time. I can think of at least two cases
where this is important: in containers, where you want a
contiguous block of elements, but you don't have the information
necessary to initialize them all immediately (e.g. as in
std::vector), and in implementations of discriminate unions, if
you wanted to support objects with non-trivial constructors or
destructors (boost::variant, perhaps).
That's a different question. And it might be faster and it
might be slower. Depends on what you're doing to allocate the
buffer. Your own allocator may or may not be faster than the
"default" allocator.

It would be surprizing if it were faster than the default
operator new( size_t, void* ):).

[...]
Anything can be stated when the author doesn't know what he is
talking about.
Because you're not looking at the whole question. If you already have
the memory block through some other mechanism, then you don't need to
spend time talking to the global allocator to get more memory.

And you're addressing the problem backwards. If you have the
memory block through some other mechanism, you can't use any
other form of new, because no other form will have the correct
semantics.
No.. the pre-allocated buffer may not be in heap.... neither does it
necessarily come from new.

But watch out for alignment problems if it doesn't.
How about: I want to construct an object in a shared memory segment (Ok,
this bit is somewhat offtopic). I can't use the normal new since it
isn't allocated off of the heap. So I use whatever the allocation
method is for the shared memory segment and then use placement new to
construct the object in that memory block.

Rather, what I use is placement new directly:

T* p = new (sharedMemoryPool) T ;
Or look at many implementations of vector. It will have allocated some
chunk of memory for all of the current objects in the vector, plus some
more for some future objects (Probably. This is why capacity() doesn't
necessarily equal size().).

There's no probably about it: the standard requires that the
number of copies be an amortised constant for insertions at the
end. Reallocation means copying all existing objects, so some
exponential strategy of growth must be used.

But the issue of separating allocation from initialization goes
further, and affects more than just performance. As a user, I
can, for example, do v.reserve( 100 ), and be assured that my
iterators into the vector are valid as long as I don't insert
more than a hundred elements. While speed was probably also a
consideration in this particular case (you don't want to recopy
a million elements each time you append one), there are also
significant semantic issues. Independantly of speed
considerations, the semantics of std::vector require separation
of allocation and initialization. (In fact, because the user
can instantiation any standard container over his own, custom
allocator, all standard containers require separation of
allocation and initialization.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top