469,160 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

overload new and delete for performance? (design)

I just want to hear people's opinions on this subject.

My application has lots and lots of short-lived objects that use
dynamic allocation/deallocation. After implementing its functionality,
I found out that much processing is spent for new/delete calls.
So, to improve its performance, I made a little factory-like class that
pre-'new's those short-lived objects and stores them in a linked list when
the program starts.

Every time I need an instance, I would do something like the following:

// instead of new
ShortLivedObject* slo = SomeFactoryLikeClass::instance()->getOne();
// instead of delete
SomeFactoryLikeClass::instance()->returnOne(slo);

I would retrieve an instance from the pre-allocated objects and set its
members, and when I'm done, I just push it back to the end of the list.
Okay, so that was my intial solution. But an article I read recommended
that I overload new and delete operators, as in the following.
class ShortLivedObject {
...
ShortLivedObject* _next;
static ShortLivedObject* _freelist; // pointer to free list

void* operator new(size_t); // overloaded new()
void operator delete(void*); // overloaded delete()
};
And use the _freelist for storage and recycling.

So, let's say the memory for the object is aligned correctly,
and in new(), memory will either be allocated (if _freelist is empty)
using malloc(), or it will just return pop the front of _freelist
and return its pointer.

In delete(), memeory will not be deallocated. Only its pointer will be
returned to the _freelist.

Now in my personal opinion, the first solution is simpler and safer.
You have less to worry about inheritance. You don't have to come up
with your own storage allocator. You don't have to worry about new[]
and delete[].

Yet, lots of people, including some of my coleagues, seem to be
using the latter technique. Are there any reasons for this? Or is it
just a personal preference?
Jul 19 '05 #1
2 6522


"Chris E. Yoon" wrote:


Yet, lots of people, including some of my coleagues, seem to be
using the latter technique. Are there any reasons for this? Or is it
just a personal preference?


The later is completely transparent to the user of your class.
This means: The user of your class uses new and delete as always.
If this turns out to be to slow you add the new and delete
operators to your class, recompile, and magically the application
runs faster. If on the other hand the systems memory allocator is
optimized by your vendor and gets faster, you simply remove the
new and delete operators, recompile, and the application has equal
or near equal performance as before with less code.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2
cr*********@naver.com (Chris E. Yoon) wrote in message news:<9c**************************@posting.google. com>...
My application has lots and lots of short-lived objects that use
dynamic allocation/deallocation. After implementing its functionality,
I found out that much processing is spent for new/delete calls.
So, to improve its performance, I made a little factory-like class that
pre-'new's those short-lived objects and stores them in a linked list when
the program starts.

Every time I need an instance, I would do something like the following:

// instead of new
ShortLivedObject* slo = SomeFactoryLikeClass::instance()->getOne();
// instead of delete
SomeFactoryLikeClass::instance()->returnOne(slo);

I would retrieve an instance from the pre-allocated objects and set its
members, and when I'm done, I just push it back to the end of the list.
Okay, so that was my intial solution. But an article I read recommended
that I overload new and delete operators, as in the following.

class ShortLivedObject {
...
ShortLivedObject* _next;
static ShortLivedObject* _freelist; // pointer to free list

void* operator new(size_t); // overloaded new()
void operator delete(void*); // overloaded delete()
};
And use the _freelist for storage and recycling.

So, let's say the memory for the object is aligned correctly,
and in new(), memory will either be allocated (if _freelist is empty)
using malloc(), or it will just return pop the front of _freelist
and return its pointer.

In delete(), memory will not be deallocated. Only its pointer will be
returned to the _freelist.

Now in my personal opinion, the first solution is simpler and safer.
You have less to worry about inheritance. You don't have to come up
with your own storage allocator. You don't have to worry about new[]
and delete[].
The solutions are basically the same, despite what you say. Both contain
a number of pre-allocated memory chunks. One difference is in naming
the allocation and deallocation functions. new and delete happen to be
the conventional names. The other is internal; your function will assign
the "initial" value using operator= while the new/delete solution goes
through a proper ctor/dtor sequence. This allows you to share memory
pools for same-sized objects (as an invisible future extension).
Yet, lots of people, including some of my coleagues, seem to be
using the latter technique. Are there any reasons for this? Or is it
just a personal preference?


It's the Standard interface, which means the Standard Library can be
used with your class. std::auto_ptr<ShortLivedObject>::~auto_ptr can
only call ShortLivedObject::operator delete().

Regards,
--
Michiel Salters
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Piotre Ugrumov | last post: by
1 post views Thread by Nimmi Srivastav | last post: by
7 posts views Thread by Sean | last post: by
17 posts views Thread by Chris | last post: by
13 posts views Thread by Vladimir Granitsky | last post: by
2 posts views Thread by Shark | last post: by
6 posts views Thread by Lighter | last post: by
5 posts views Thread by richard.parker | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.