• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
SuperVGA

Fastest map-like collection for <GUID, BaseClass*> lookup

4 posts in this topic

I currently have 

std::map<const guid, CommWrapper >* comm_table;

 

where guid

 struct guid
 {
  unsigned char d[16];
  
  // Less-comparer for std::map with GUID keys
  bool operator<(const guid& rhs) const
  {
   for(unsigned c = 0; 16 != c; ++c)
   {
    if(d[c] == rhs.d[c]) {
  continue;
    }
    return d[c] < rhs.d[c];
   }
   return false;
  } 
 };

 

But it seems to be way slower than the approach i took before, where i just based the (gu)id on the index the pointer had in a std::vector.

for instance truck would be in comm_table[5], and thus its id would be 5.

 

But I kinda like the idea of using some big, fancy guids. It's just really slow for autonomous inter-instance communication,
and I'm wondering if I'm doing it right. Perhaps there's no reason to go this way at all.

 

Or am i just using a wrong collection?

0

Share this post


Link to post
Share on other sites

The comparision method isn't the fastest one. For lookup heavy maps I would sugguest the use of a hashmap instead.

 


But I kinda like the idea of using some big, fancy guids.

Do you really need a global, unique id  here ? What's wrong with a 32bit or 64bit application unique id ? It would be a lot faster and would make it even more useful in combination with a hashmap. Looks a little bit like over-engineering to me smile.png

Edited by Ashaman73
1

Share this post


Link to post
Share on other sites

The comparision method isn't the fastest one. For lookup heavy maps I would sugguest the use of a hashmap instead.

 

 

 


But I kinda like the idea of using some big, fancy guids.

Do you really need a global, unique id  here ? What's wrong with a 32bit or 64bit application unique id ? It would be a lot faster and would make it even more useful in combination with a hashmap. Looks a little bit like over-engineering to me smile.png

 

Thanks for your input. Yeah, I have been over-engineering. It's probably because I've become so accustomed to these 128bit guids at work that I feel like I should be using those too. But you're absolutely right - less will definitely suffice. I'll moderate this, and overhaul this communicator thing with hashmaps and shorter uids.

I only want them to be unique application-wide, after all. 

0

Share this post


Link to post
Share on other sites

I've found 64bit unsigned ints are more than enough for any GUID I've had.  I tend to use them as bit fields as well to help identify type and which machine they come from, so I could imagine I could get away with 32 bits in a simpler game.  The nice thing is that the comparison is simply an integer comparison, which is really fast.

 

As for quick look-up, you need to think about how many objects you have total.  If you have less than 100 objects, a simple array of objects may actually be faster than using std::map (which if I recall, is a red-black tree data structure).  However, if you have many objects (10000-1000000) you may need to test which is better, a std::map or some kind of hash table.

 

Cheers,

 

Bob 

1

Share this post


Link to post
Share on other sites
As Scourge mentioned, the number of items stored is important.


std::map is a tree sorted by the key value. It takes O(log N) time to find the value. It makes comparisons as it walks down the tree in a binary search.
std::unordered_map (c++11) is a hash table ordered by key value. It computes a hash of they key, then looks up the slot directly in a hash table.

And finally, storing them sorted in a std::vector or a non-dynamic array allows you to do a binary search if it is sorted, or a linear search if it is not.


So to figure out fastest, you need to figure out how lookups are going to happen. How many items (N) are you storing? A linear search takes N/2 comparisons on average, but benefits from cache effects as you run through the data. The cost for doing 1 may be the same cost as doing 16 or 32 compares thanks to the cache helping out a linear accessing pattern. A binary search takes log N comparisons but they don't get a cache bonus as they are not linear. Finally, a hash table has a near-constant time cost, but it must go through the effort of computing a hash, then looking in the bucket that has the hash; if there are multiple items in the bucket it must search through those few items.

Which of those is fastest depends on the cost to compare the keys (a single machine word is much faster than a string based comparison) and on the cache friendliness of the data (a machine word is smaller than a string, and a string can possibly require more memory lookups) and finally on the cost to compute a hash.

I will add my vote to using a machine word size for the key as well. It is the preferred efficient size for processing on the machine.



Incidentally, many games use a layered approach to resource lookup so that it happens as infrequently as possible.. They have a long name, such as a file name or a resource key or some other value that is used outside the system. When a request for the named object is made the object gets looked up (hash table) to see if it is in the resource list already. If no, the item is added to the resource list and the key and list index are added to the hash table; the program now has the resource index and can use that for the rest of the running of the application. The resource list is not actually the data, but a proxy to the data that can be loaded and unloaded as needed based on game-specific factors. The net result is that the game gets either a fixed index (or even a fixed pointer) in the resource list so hopefully lookup needs to happen only once, ever.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0