Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

placement new

 
 
karthikbalaguru
Guest
Posts: n/a
 
      09-10-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-10-2007
karthikbalaguru wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      09-10-2007
On 2007-09-10 20:29, karthikbalaguru wrote:
> 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.

--
Erik Wikström
 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      09-10-2007
On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      09-10-2007
On Sep 11, 12:09 am, Ondra Holub <ondra.ho...@post.cz> wrote:
> On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.com> wrote:
>
>
>
>
>
> > 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.- 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

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      09-10-2007
karthikbalaguru <> wrote in
news: oups.com:

> 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.).

 
Reply With Quote
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      09-10-2007
On 2007-09-10 21:28, karthikbalaguru wrote:
> On Sep 11, 12:09 am, Ondra Holub <ondra.ho...@post.cz> wrote:
>> On 10 Zá , 20:29, karthikbalaguru <karthikbalagur...@gmail.com> wrote:
>>
>>
>>
>>
>>
>> > 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.- 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-lit...html#faq-16.10
to see how (line 4 in the code).

--
Erik Wikström
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      09-10-2007
karthikbalaguru <> wrote in
news: oups.com:


> 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      09-11-2007
karthikbalaguru wrote:

>> 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


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.


 
Reply With Quote
 
karthikbalaguru
Guest
Posts: n/a
 
      09-11-2007
On Sep 11, 12:56 am, Andre Kostur <nntps...@kostur.net> wrote:
> karthikbalaguru <karthikbalagur...@gmail.com> wrote innews: groups.com:
>
> > 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.).- Hide quoted text -
>
> - Show quoted text -


Interesting !! Thx for the info.

Karthik Balaguru

 
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
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
delete in-place corresponding to placement new? Peter Olcott C++ 11 03-01-2004 05:31 AM
is it placement new? Wenjie C++ 0 06-26-2003 11:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57