Something faster than linked lists

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Something faster than linked lists

Post by earlz »

I am trying to make my event system not use a huge array of function pointers and rather use something like a linked list. The only problem is that linked lists are so slow! Is there any other method of dynamic allocation that is any faster?
My best idea so far is to have a linked list of arrays, like rather than just having a linked list object hold only one element, have a linked list object have an array of 256 or so elements..which in my case would be allocating in 2k blocks..
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

Yeah that works. It's called unrolled linked lists There's more info on the wikipedia page. You might find some better methods there as well.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

<edit>
I just realized you were talking about allocation being slow and not the linked list. Oops.
</edit>

A similar method is to periodically convert portions or as much as you like of the linked list into an array. You might use a unrolled linked list along side of something like this.

If you are finding you're self having too large of a linked list then you might try increasing the overall structure depth or/and grouping events so that when you need to perform a search or modification of certain entries you have decreased the number of entries.

So if you are keeping track of events for something, then you might only attach those events to that something instead of:

Shallow Overall Structure Depth

Code: Select all

struct tEvent{
     struct tSomething *something;
     /// event information
     struct tEvent *next, *prev;
};


Increased Overall Structure Depth.

Code: Select all

struct tEvent{
     /// event information
     struct tEvent *next, *prev;
};
struct tSomething{
     struct tEvent *events;
};

Grouping
Sometimes you can make one item become part of multiple lists.

Code: Select all

struct tItem{
     u32 type;
     struct{
           struct tItem *next, *prev;
     } core, type1, type2, type3;
};


Where the core next and prev could repersent if you needed the entire linked list. The type1, type2, and type3 could represent linked list of entries corresponding to certain types. So if you needed to only search for a type1 then you would have eliminated the type2 and type3 needless searches.

Decrease Evaluation Time Of Items

You can sometimes decrease the evaluation time of the items in the linked list which will decrease the total time to search the linked list. If you are searching for strings try adding a hash value beside them. Then check the hash, then the string if the hash is correct.

Code: Select all

unsigned int hash(unsigned char *string){
  u32 x, hash;
  for(x = 0, hash = 0; string[x] != 0; ++x){
    hash += string[x];
  }
  return hash;
}


You might even find a performance increase if you check multiple integer values. Where you check a, b, and c for va, vb, vc. Then a+b+c==va+vb+vc. Of course you take a penalty when you find the correct item; or for each correct item.
Post Reply