Profiling has shown that during runtime of my program, 100 million
objects of class X are constructed and destructed.
What is the best way to optimize in such a case, both for memory and
speed (speed maybe more important)?
One idea is to make the class very small, to contain only the most
important member data, and put everything else, including member
functions, into another class:
// So, the following:
class X {
private: int x;
public: void manipulate();
};
// would turn into this:
class X {
private: int x;
};
class XContext {
public: void manipulate(const X&);
};
Does that help to speed up the constructor? BTW, these objects are
stored in hashes a lot (not pointers, but the whole objects).
The created objects are BTW very similar (there are maybe 100 non-equal
X objects), so I guess a Factory could help that would return (pointers
to) already-created objects, instead of constructing the same object a
million times (Flyweight?)
What else could be done? Unfortunately, this class gets mentioned a lot
in a huge codebase, so it would be best if code changes would be minimal.
Would a memory pool help? Which solution is best?
Thanks! 5 1896
Markus Dehmann wrote: Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed.
That should not be a concern unless you know that it took significant
amount of time.
What is the best way to optimize in such a case, both for memory and speed (speed maybe more important)?
It depends.
One idea is to make the class very small, to contain only the most important member data, and put everything else, including member functions, into another class:
Member functions don't matter for the size of an object.
// So, the following: class X { private: int x; public: void manipulate(); };
// would turn into this: class X { private: int x; }; class XContext { public: void manipulate(const X&); };
You will still need to create 100000 of 'X', no? This 'X' has the
same size as the old one.
Does that help to speed up the constructor? BTW, these objects are stored in hashes a lot (not pointers, but the whole objects).
Most likely not.
The created objects are BTW very similar (there are maybe 100 non-equal X objects), so I guess a Factory could help that would return (pointers to) already-created objects, instead of constructing the same object a million times (Flyweight?)
That is possibly a solution. You definitely don't want to use
'new' operator (especially the default one) 100000 times.
What else could be done? Unfortunately, this class gets mentioned a lot in a huge codebase, so it would be best if code changes would be minimal. Would a memory pool help? Which solution is best?
Memory pool would help. Or a good memory manager (if you can find
one better than the one that came with your compiler).
V
Markus Dehmann wrote: Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed.
What is the best way to optimize in such a case, both for memory and speed (speed maybe more important)?
[snip] The created objects are BTW very similar (there are maybe 100 non-equal X objects), so I guess a Factory could help that would return (pointers to) already-created objects, instead of constructing the same object a million times (Flyweight?)
What else could be done? Unfortunately, this class gets mentioned a lot in a huge codebase, so it would be best if code changes would be minimal.
Hi Markus,
From what I can gather, there seem to be a lot of assignments of your
objects taking place. Maybe a Copy On Write approach might be useful
(If the profiler indiactes that the copies take up a large chunk of CPU
and/or the independent cpoies use up a lot of memory. If your app is
multi-threaded you will have to be careful with COW though, else you
might turn up with negligible performance gains.
Check out chapter 4 of Andrei Alexandrescu's book Modern C++ Design.
Markus Dehmann <ma************@gmail.com> writes: Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed.
Does this means temporaries?
If yes, then try to give your code some mojo ;-) http://www.cuj.com/documents/s=8246/...r/alexandr.htm
ImRe
Markus Dehmann wrote: Profiling has shown that during runtime of my program, 100 million objects of class X are constructed and destructed.
OK, but what percentage of the program execution time does that take?
How many of those 100M needed to actually exist?
What is the best way to optimize in such a case, both for memory and speed (speed maybe more important)?
That depends on the class and how you use it.
One idea is to make the class very small, to contain only the most important member data, and put everything else, including member functions, into another class:
// So, the following: class X { private: int x; public: void manipulate(); };
// would turn into this: class X { private: int x; }; class XContext { public: void manipulate(const X&); };
That makes no sense. You don't construct functions when you construct the object.
Does that help to speed up the constructor? BTW, these objects are stored in hashes a lot (not pointers, but the whole objects).
Probably not, are you using the default constructor or your own?
The created objects are BTW very similar (there are maybe 100 non-equal X objects), so I guess a Factory could help that would return (pointers to) already-created objects, instead of constructing the same object a million times (Flyweight?)
What else could be done? Unfortunately, this class gets mentioned a lot in a huge codebase, so it would be best if code changes would be minimal.
Well if the construction actually takes a significant time, and you don't require so many copies, then perhaps use a pointer, or some smart pointer. Perhaps a reference counted
pointer if you need to have lots of "copies" of the same object.
Would a memory pool help? Which solution is best?
I don't know. It depends on your usage patterns.
If you construct 100M objects and then destroy them all, in order, then probably not.
Ben
--
I'm not just a number. To many, I'm known as a String... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel |
last post by:
Hello =)
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -
self.setInterval('ObjectName.methodName()',...
|
by: Dave |
last post by:
In Exceptional C++, the following code snippet appears at the bottom of page
197:
template<class T>
class Array : private ArrayBase, public Container
{
typedef Array AIType;
public:
Array(...
|
by: Vulcan Fire |
last post by:
Hi
Had this doubt really irking me for some time.
For some obj of type say Obj.
void fn(Obj o){
// do somethng
}
|
by: Jimmy Cerra |
last post by:
I am a little confused how the memory for objects is allocated in
JavaScript. David Flanagan, in "JavaScript: The Definitive Guide,"
states that each property of a class takes up memory space when...
|
by: M. Posseth |
last post by:
The below code is to split a mysql file into a file that MSSQL can
understand ( only the DDL should be manually modified )
It does work however it is verry verry slow , i tried it on a AMD 64 ...
|
by: Stephan Keil |
last post by:
Hi all,
I am a novice with .NET and I am wondering if there is something like an
"identity value" of an object. I mean something like the object's address
in C++ or C, i.e. a fixed unique value...
|
by: Dave |
last post by:
I'm never quite sure whether to use "this." or not when referring to fields
or properties in the same class. It obviously works just fine without it
but sometimes I wonder if using this....
|
by: Jeroen |
last post by:
Hi all,
I've got a question about writing a library. Let me characterize that
library by the following:
* there is a class A which is available to the user
* there is a class B that is used...
|
by: pedalpete |
last post by:
Hey MySql Gurus,
I've been having some issues with my database starting a few days ago, though I haven't changed any of the db code in at least 3 weeks.
I do some daily maintenance on my...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |