473,671 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
ShortLivedObjec t* slo = SomeFactoryLike Class::instance ()->getOne();
// instead of delete
SomeFactoryLike Class::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 ShortLivedObjec t {
...
ShortLivedObjec t* _next;
static ShortLivedObjec t* _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 6693


"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*********@nav er.com (Chris E. Yoon) wrote in message news:<9c******* *************** ****@posting.go ogle.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
ShortLivedObjec t* slo = SomeFactoryLike Class::instance ()->getOne();
// instead of delete
SomeFactoryLike Class::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 ShortLivedObjec t {
...
ShortLivedObjec t* _next;
static ShortLivedObjec t* _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<S hortLivedObject >::~auto_ptr can
only call ShortLivedObjec t::operator delete().

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2055
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this least operator for the class Person, but I don't know how write the overload for a class that derived from the class Person. The overload of << in Person is this: ostream & operator<<(ostream &out, const Persona &p){ out<<p.getNome()<<"...
1
3844
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
7
4668
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
17
2506
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
13
2723
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong overload resolution ? I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void...
2
3093
by: R.Welz | last post by:
Hello. I want to discuss a problem I have with my database design becourse I feel I cannot decide wheather I am on the right way of doing things. First of all, I am writing a literature and magazine database with web (PHP) and C++ Interface, serving over the web and in a very fast LAN. So my concern is about performance (and aestaetic by doing the things as optimal as possible.) This is my first database at all, but I have read a lot of...
2
11519
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use operator new?
6
12458
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
5
3479
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be overloaded, but I need to link with the system libraries (frameworks) and I DO NOT want my overloads to be mapped to them. I'm working with some code that is currently working on Win32 but also needs to work on the Mac. Under windows this is very...
0
8485
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5704
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.