C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Performance. Memory allocation. Datastructure design

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
venki
Guest





PostPosted: Tue Dec 13, 2005 10:33 pm    Post subject: Performance. Memory allocation. Datastructure design Reply with quote



Hi,
Given 2 possible ways of creating a class:
class NestedClass
{
char chunk_[1024];
};
class PtrMember
{
NestedClass* pmember_;
}

and
class ContainedMember
{
NestedClass member_;
}

is creating (and using very frequently) ContainedMember on the heap
__significantly__ more efficient that creating PtrMember (and
PtrMember::pmember_ separately) on the heap?

I could think of only 2 reasons why it could be more efficient: cost of
2 news and possible locality of reference? But are these significant
enuff to warrant changin legacy code to use the contained approach?

thanks!
-venki


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze
Guest





PostPosted: Wed Dec 14, 2005 12:07 pm    Post subject: Re: Performance. Memory allocation. Datastructure design Reply with quote



venki wrote:

Quote:
Given 2 possible ways of creating a class:

class NestedClass
{
char chunk_[1024];
};
class PtrMember
{
NestedClass* pmember_;
}

and
class ContainedMember
{
NestedClass member_;
}

is creating (and using very frequently) ContainedMember on the
heap __significantly__ more efficient that creating PtrMember
(and PtrMember::pmember_ separately) on the heap?

I could think of only 2 reasons why it could be more
efficient: cost of 2 news and possible locality of reference?
But are these significant enuff to warrant changin legacy code
to use the contained approach?

It depends. What does the profiler say? Is the application
spending a significant amount of time allocating and freeing
memory, or not?

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
PPD
Guest





PostPosted: Wed Dec 14, 2005 12:51 pm    Post subject: Re: Performance. Memory allocation. Datastructure design Reply with quote



Wherever possible I would prefer the contained approach. For the
following reasons:

1. Yes, the second new is unwarranted. If the system has a costly
memory allocation algorithm then it may get a bad performace hit
(doubling the allocation times).

2. When member_ is contained the locality of reference is guaranteed.
When it is separately dynamically allocated - one part of
ContainedMember object would be on stack and the other would be on
heap. These are very different memory areas.

3. If NestedClass object is separately allocated, the second new might
throw. If it is contained, such a possiblity does not exist. The
solution is more robust.

4. Finally it takes less space too (pmember_ pointer is not needed).

In general, I would take the dynamically allocated nested member route
only if I potentially:

1. May not need to do use the member in a good number of cases.

2. The nested member may have some creation parameter (for example
array size) that I may not know when I create the ContainedMember.

Your context does not suggest to qualify for these factors. Hence, I
would prefer the second class design.


Also, note that the first design is in line with Sutter's PIMPL idiom
(Exceptional C++). However that idiom would not come in the rescue as
the interface / implementation isolation is context here.

PPD


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Earl Purple
Guest





PostPosted: Wed Dec 14, 2005 12:56 pm    Post subject: Re: Performance. Memory allocation. Datastructure design Reply with quote


kanze wrote:
Quote:
venki wrote:

I could think of only 2 reasons why it could be more
efficient: cost of 2 news and possible locality of reference?
But are these significant enuff to warrant changin legacy code
to use the contained approach?

It depends. What does the profiler say? Is the application
spending a significant amount of time allocating and freeing
memory, or not?

and even then the problem may be that you are copying buffers where you
could be referencing them.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.