Some perfomance thoughts required

A

Alexander Adam

Hi!

I've got a pretty complex data structure that keeps instances of
structs allocated with the new operator.
Now my issue is that those structures are required to be allocated /
deallocated pretty often resulting
in a huge perfomance issue. Usually what I am doing is to create
arrays and initialize their sizes a few
times bigger than originally required to avoid another allocation on
the next run. Then I do simply reset them
and do a memcpy at the existing location. This works great with POD
structus but my structs are complex
in the way that they not only use inheritance but they do also use
internal objects that do require an
constructor / destructor. So what is the general way to handle such
things? Am I missing something?

thanks!
Alex
 
E

Erik Wikström

Hi!

I've got a pretty complex data structure that keeps instances of
structs allocated with the new operator.
Now my issue is that those structures are required to be allocated /
deallocated pretty often resulting
in a huge perfomance issue. Usually what I am doing is to create
arrays and initialize their sizes a few
times bigger than originally required to avoid another allocation on
the next run. Then I do simply reset them
and do a memcpy at the existing location. This works great with POD
structus but my structs are complex
in the way that they not only use inheritance but they do also use
internal objects that do require an
constructor / destructor. So what is the general way to handle such
things? Am I missing something?

A pool allocator might help.
 
J

Jerry Coffin

Hi!

I've got a pretty complex data structure that keeps instances of
structs allocated with the new operator.
Now my issue is that those structures are required to be allocated /
deallocated pretty often resulting
in a huge perfomance issue. Usually what I am doing is to create
arrays and initialize their sizes a few
times bigger than originally required to avoid another allocation on
the next run. Then I do simply reset them
and do a memcpy at the existing location. This works great with POD
structus but my structs are complex
in the way that they not only use inheritance but they do also use
internal objects that do require an
constructor / destructor. So what is the general way to handle such
things? Am I missing something?

You probably want to overload operator new for the class(es) of the
objects you're allocating/freeing so frequently. At startup, your
operator new will grab a big chunk of memory for the data, and break it
up into chunks, each the size of a single object. As objects are
allocated, it'll give out the addresses of object-sized chunks, and mark
each chunk as being in use when it does so. When an object is freed,
it'll mark each chunk as being available again.

You can probably find existing source code for a few different versions
of this -- Andrei Alexandrescu's Loki library has one, and Boost has
another, and so on.
 
F

Fei Liu

Alexander said:
Hi!

I've got a pretty complex data structure that keeps instances of
structs allocated with the new operator.
Now my issue is that those structures are required to be allocated /
deallocated pretty often resulting
in a huge perfomance issue. Usually what I am doing is to create
arrays and initialize their sizes a few
times bigger than originally required to avoid another allocation on
the next run. Then I do simply reset them
and do a memcpy at the existing location. This works great with POD
structus but my structs are complex
in the way that they not only use inheritance but they do also use
internal objects that do require an
constructor / destructor. So what is the general way to handle such
things? Am I missing something?

thanks!
Alex
Apart from the other suggestions, you should think about the use of
array as your container data structure, perhaps a node based container
will provide better overall performance, especially when it's memory
intensive.

Fei
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top