OSDev.org

The Place to Start for Operating System Developers
It is currently Sat May 25, 2024 1:11 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Something faster than linked lists
PostPosted: Sat Apr 21, 2007 6:05 pm 
Offline
Member
Member

Joined: Thu Jul 07, 2005 11:00 pm
Posts: 1546
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..

_________________
My new NEW blag


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 21, 2007 6:28 pm 
Offline
Member
Member
User avatar

Joined: Thu Jan 04, 2007 3:29 pm
Posts: 1466
Location: Noricum and Pannonia
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.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 21, 2007 7:07 pm 
Offline
Member
Member
User avatar

Joined: Tue Nov 09, 2004 12:00 am
Posts: 843
Location: United States
<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:
struct tEvent{
     struct tSomething *something;
     /// event information
     struct tEvent *next, *prev;
};


Increased Overall Structure Depth.
Code:
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:
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:
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.

_________________
Projects


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group