C
cppquester
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< int> someData;
static std::stack used;
public:
static void * operator new (size_t size);
static void operator delete (void *p, size_t size);
};
void* A:perator new(size_t sz)
{
void* res;
if( used.size() > 0)
{
res = used.back();
used.pop();
return res;
}
return ::new( sz);
}
void A:perator 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?
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< int> someData;
static std::stack used;
public:
static void * operator new (size_t size);
static void operator delete (void *p, size_t size);
};
void* A:perator new(size_t sz)
{
void* res;
if( used.size() > 0)
{
res = used.back();
used.pop();
return res;
}
return ::new( sz);
}
void A:perator 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?