Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > management of many small objects

Reply
Thread Tools

management of many small objects

 
 
Bo Peng
Guest
Posts: n/a
 
      07-03-2004
John Harrison wrote:

> That looks OK, but now the compromise is the global variable (and the
> fact that we've left standard C++ far behind).


Yeap. I am not happy about this either. All these non-standard things
will make understanding of my code a nightmare to others... So,
motivated by Loki's SmallObjectAllocator, the idea is now:

1. objects do use a pointer

struct obj{
int a;
unsigned char * b;
obj();
}

2. do not use the usual new unsigned char [8]. Rather, use
new(address) unsigned char[8] where address point to a big chunk of
memory of size 8 * number_of_objects. For example:

struct Pool{
vector<unsigned char> data;
int cur; // current unused.

vecrot<unsigned char>::iterator begin(){
return data.begin();
}

vecrot<unsigned char>::iterator end(){
return data.end();
}

Pool(size_t size):data(size),cur(0){};

unsigned char * malloc(int blocksize){
int tmp = cur;
cur += blocksize;
return &(data[tmp]);
}
}

Pool pool(8*1000);

When allocating an object:

obj:bj(){
b = pool.malloc(;
}

or something like:

unsigned char * ptr = pool.malloc(;
b = new(ptr) unsigned char[8];


3. Access the elements in two ways:

for(int i=0; i< 1000; i++)
objects[i].b[2] = 2;

or directly through Pool

for( vector<unsigned char>::iteator it=pool.begin()+2; it <
pool.end()+2; it+=
* it = 2;

The latter should be faster. It is also possible to create slicing
vector out of Pool.data.

This looks like a good tradeoff. The interface is now clean and quick;
there is only one pointer to waste and there is only one new operator to
call.....


Potential improvements:
1. use template to generalize the idea to more complex objs.
(boost:ool seems to be complicated for my purpose.)
2. Use Loki's SmallObjectAllocator. His implementation might be better.
3. Integrate SmallObjectAllocator into STL and keep all these behind the
scene.

C++ is hard.
Bo
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-05-2004
Bo Peng wrote:
>
> Hi, Mike,
>
> Thanks for your reply. I certainly know all the grammar details and what
> I showed was just some kind of psudo-code.
>
> The core of the problem is that I will have a million or so small
> objects with some fixed elements and an array.


Get yourself a copy of
Scott Meyers 'Effective C++'

In Item 10 he describes exactly what you seem to be after:
custom versions of operator new and delete.
In short: When new is called, it allocates not a single object but
a whole array of such objects and hands out pointers into the array,
when needed. Only when this pool of objects is used up, the customized
version of new walks through the hassle of system memory management
again by allocating another pool of objects.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-05-2004
Karl Heinz Buchegger wrote:
>
> Bo Peng wrote:
> >
> > Hi, Mike,
> >
> > Thanks for your reply. I certainly know all the grammar details and what
> > I showed was just some kind of psudo-code.
> >
> > The core of the problem is that I will have a million or so small
> > objects with some fixed elements and an array.

>
> Get yourself a copy of
> Scott Meyers 'Effective C++'
>
> In Item 10 he describes exactly what you seem to be after:
> custom versions of operator new and delete.
> In short: When new is called, it allocates not a single object but
> a whole array of such objects and hands out pointers into the array,
> when needed. Only when this pool of objects is used up, the customized
> version of new walks through the hassle of system memory management
> again by allocating another pool of objects.
>


Sorry. After reading the follo ups, it seems you are
heading for a different thing.

--
Karl Heinz Buchegger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Project management / bug management Floris van Haaster ASP .Net 3 09-23-2005 08:36 PM
queue management with "application failure management" pouet Java 2 07-30-2004 09:59 PM
CatOS web management or CiscoView management ? Martin Bilgrav Cisco 1 12-20-2003 01:49 PM



Advertisments