473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

better new delete

Hi Folks,

I just had the following idea for opertor new and delete
(I am sure I am not the first one, haven't seen it
so far though):

class A
{
std::vector< intsomeData;

static std::stack used;

public:
static void * operator new (size_t size);
static void operator delete (void *p, size_t size);
};

void* A::operator new(size_t sz)
{
void* res;
if( used.size() 0)
{
res = used.back();
used.pop();
return res;
}

return ::new( sz);
}

void A::operator delete (void *p, size_t sz)
{
someData.clear();
used.push( p);
}

If there are a lot of instances of A to be allocated and freed all the
time,
could (would) this result in a performance gain compared to
the standard new/delete operator as system calls are avoided in most
cases?
Of course it is obvious that there is a certain memory overhead (for
the stack and as all As are never freed), but it looks to me that this
is a very easy way to gain a lot of performance potentially. Am I
missing something?

Aug 27 '07 #1
8 1385
<cp********@googlemail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
Hi Folks,

I just had the following idea for opertor new and delete
(I am sure I am not the first one, haven't seen it
so far though):
[code snipped]
void* A::operator new(size_t sz)
{
void* res;
if( used.size() 0)
{
res = used.back();
Don't you mean used.top()?
used.pop();
return res;
}
[code snipped]
If there are a lot of instances of A to be allocated and freed all the
time,
could (would) this result in a performance gain compared to
the standard new/delete operator as system calls are avoided in most
cases?
One obvious thing that you should gain is the protection from memory
fragmentation (which is really a big performance gain), the same method can
be (and is) implemented using placement new

Abdo Haji-Ali
Programmer
In|Framez
Aug 27 '07 #2
cp********@googlemail.com wrote:
I just had the following idea for opertor new and delete
(I am sure I am not the first one, haven't seen it
so far though):

class A
{
std::vector< intsomeData;

static std::stack used;

public:
static void * operator new (size_t size);
static void operator delete (void *p, size_t size);
};

void* A::operator new(size_t sz)
{
void* res;
if( used.size() 0)
{
res = used.back();
used.pop();
return res;
}

return ::new( sz);
}

void A::operator delete (void *p, size_t sz)
{
someData.clear();
used.push( p);
}

If there are a lot of instances of A to be allocated and freed all the
time,
could (would) this result in a performance gain compared to
the standard new/delete operator as system calls are avoided in most
cases?
Of course it is obvious that there is a certain memory overhead (for
the stack and as all As are never freed), but it looks to me that this
is a very easy way to gain a lot of performance potentially. Am I
missing something?
The idea, as I understand it, is not to deallocate the last "freed"
instance[s] but to reuse it. Seems nice to have the freeing delayed,
and that's essentially what Boost's pooled memory manager does. Their
idea is not to deallocate and not to reuse, but instead just keep
giving you new pieces and deallocate them all at once at the end, when
you don't need any of them any more.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 27 '07 #3
I just had the following idea for opertor new and delete
(I am sure I am not the first one, haven't seen it
so far though):
[code snipped]
void* A::operator new(size_t sz)
{
void* res;
if( used.size() 0)
{
res = used.back();

Don't you mean used.top()?
yes.
used.pop();
return res;
}

[code snipped]
If there are a lot of instances of A to be allocated and freed all the
time,
could (would) this result in a performance gain compared to
the standard new/delete operator as system calls are avoided in most
cases?

One obvious thing that you should gain is the protection from memory
fragmentation (which is really a big performance gain), the same method can
be (and is) implemented using placement new
So my suggestions will lead to fragmentation? But still better than
standard new, isn't it?
Do you have a link to a aolution with placement new?
Aug 27 '07 #4
<cp********@googlemail.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
>One obvious thing that you should gain is the protection from memory
fragmentation (which is really a big performance gain), the same method
can
be (and is) implemented using placement new

So my suggestions will lead to fragmentation? But still better than
standard new, isn't it?
Do you have a link to a aolution with placement new?
On the contrary, what I meant is that the main advantage of a memory pool
(something like what you're doing) IS the *protection* from memory
fragmentation.
For samples, google for "C++ memory pool". This should return a few hundred
of thousands results :)

Abdo Haji-Ali
In|Framez
Programmer
Aug 27 '07 #5
Thanks for your reply (same to all other repliers as well!)
The general idea is called a free-list or, with more sophisticated
management, a cache. And what you're missing is, first, that std::stack
itself uses dynamic allocation,
so what?
For me only performance gain is what counts.
(plus I might use a std::vetor with reserve() in the end)

and second, that in operator delete you
don't have access to instance member data (the instance has already been
destroyed),
Ok.
and third, that a free-list is based on a certain usage
pattern (otherwise it just adds overhead),
As I said, frequent allocation/deallocation (no persistent objects).

and fourth, that defining
operator new and operator delete is very costly in that it prohibits
other possibly more important usages of that mechanism,
Don't get this one: Better no improvement than a reasonable one just
because there is a possible even better one (which then might never be
done in the end because it is way more complicated)?

and fifth, that
it's generally not a good idea to do micro-optimization. In short don't
do it, or at least, don't do it yet (for optimization, first measure!).
I did some. Much of the time is indeed spend on operator new/delete.
(But before actually implementing my idea I thought it is wise to get
some
advice to maybe even make it better - or let it even be if I will be
conviced that the idea wasn't that good in the end).

Aug 27 '07 #6
On Aug 27, 3:33 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
cppques...@googlemail.com wrote:
I just had the following idea for opertor new and delete
(I am sure I am not the first one, haven't seen it
so far though):
class A
{
std::vector< intsomeData;
static std::stack used;
public:
static void * operator new (size_t size);
static void operator delete (void *p, size_t size);
};
void* A::operator new(size_t sz)
{
void* res;
if( used.size() 0)
{
res = used.back();
used.pop();
return res;
}
return ::new( sz);
}
void A::operator delete (void *p, size_t sz)
{
someData.clear();
used.push( p);
}
If there are a lot of instances of A to be allocated and freed all the
time,
could (would) this result in a performance gain compared to
the standard new/delete operator as system calls are avoided in most
cases?
Of course it is obvious that there is a certain memory overhead (for
the stack and as all As are never freed), but it looks to me that this
is a very easy way to gain a lot of performance potentially. Am I
missing something?

The idea, as I understand it, is not to deallocate the last "freed"
instance[s] but to reuse it. Seems nice to have the freeing delayed,
and that's essentially what Boost's pooled memory manager does. Their
idea is not to deallocate and not to reuse, but instead just keep
giving you new pieces and deallocate them all at once at the end, when
you don't need any of them any more.
I think I cannot use a pool here because I do not know the peak memory
amount used. Some objects might have a very short life span, others
might
exist almost for the whole runtime.


Aug 27 '07 #7
On 2007-08-27 16:06, cp********@googlemail.com wrote:
Thanks for your reply (same to all other repliers as well!)
>The general idea is called a free-list or, with more sophisticated
management, a cache. And what you're missing is, first, that std::stack
itself uses dynamic allocation,

so what?
For me only performance gain is what counts.
(plus I might use a std::vetor with reserve() in the end)
Even vector with reserve() is not safe, should you ever over-allocate it
will re-allocate and you're in deep shit, so to speak. To do it
correctly you must allocate raw memory and do the book-keeping yourself.

One way to do it would be to allocate chunks of data sized to be a
multiple of the size of the objects, say 10 to 50. Then you keep track
of how many objects you have in each chunk. When all are full you
allocate a new one and start filling it. The idea is that when you start
deallocating the objects you'll end up with empty chunks which can then
be deallocated.

For some usage patterns this will end up with lots of chunks with one
object in each, but never more chunks than needed to hold the maximum
number of objects used.

--
Erik Wikström
Aug 27 '07 #8
"Alf P. Steinbach" <al***@start.nowrote in news:13d5m5fsfp1laf5
@corp.supernews.com:
>
Considering that original code would not compile, it isn't necessarily
bad to introduce additional undefined behavior.

However, the OP and other readers may get the impression that the above
would be a good idea.

So, to correct that impression: it's just Undefined Behavior, the object
has already been destroyed.
Good point, I was focusing too much on the OP's code and not enough on what
was actually happening.

joe
Aug 27 '07 #9

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

Similar topics

0
by: Casey Hawthorne | last post by:
Is there a better way to delete the image from a Tkinter button other than the following: - reconstructing the button - image=blank.gif -- Regards,
0
by: 2trax | last post by:
Hi all, I am trying to mimic the unix security model (users and groups) in a web app. I have thought of two ways of implementing this, but am unsure which would be better. The first design...
3
by: NeilAnderson | last post by:
I'm a fairly new user of access & I've never had any training, so I'm wondering if I'm doing the right thing here, or if it matter at all. I'm building a database for room booking purposes and I'm...
1
by: Rod | last post by:
I am writing my third ASP.NET application. Unfortunately for me, I get to work on these only once every few months (which doesn't help in trying to remember how to do everything). Anyway, I've...
2
by: msnews.microsoft.com | last post by:
Hello, I have the scenario. I m building an application either in asp.net or window application. This application is base on n-tier application model. Let us take example of Northwind Database in...
1
by: Frank [GOD] | last post by:
Let me set this up for y'all... I have 8 mySQL databases with over 100K records, which include a phone number field. I call these the storage tanks. They're labeled db1 - db8. Then I have 1...
4
by: James | last post by:
Basically I have a DataGrid that I'm binding to the results of a stored procedure call. The recordset is fairly small. Initially I'm creating a DataSet from the results and binding it. There's a...
6
by: AlGorr | last post by:
Hello,,, I don't know much about PHP+SQL but I have to find the better way to make my query work and to be shorter and better. All help will be appreciated. //Check if IP exists in banIP...
204
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.