[C#] Generating a unique ID for each object instance

Started by
16 comments, last by Spa8nky 14 years, 10 months ago
Quote:Original post by Toolmaker
However, why exactly are you storing your quads with an ID? Wouldn't it be more efficient to just check if an object instance is already in the list? And on dictionaries, why not just calculate the hash code? The hash isn't used to store the object by, just as a way to quickly index it.

Toolmaker


QFE. A HashSet might even be appropriate here, though I'm naively skeptical of the need for a pool in the first place.
Advertisement
(If ID counting is still needed after previous comments)

Personally, I would keep it simple and minimalistic, and implement a small ID-counter that is atomically incremented (read: it shall be thread safe) upon each generateId() call.


So, if you need a global id-counter for each class:

static class ID<T> {        static long id = 0;        static public long generateNewId () {                return System.Threading.Interlocked.Increment (ref id);        }}


Or if you want to have local id-counters:
class ID {        long id = 0;        public long generateNewId () {                return System.Threading.Interlocked.Increment (ref id);        }}



And if you want to load the counter from some file, then call some function that loads a saved id from some file, instead of intializing it to zero.
Thanks very much for all your help with this one guys.

Using a GUID has also solved the query I had in my previous thread.
Quote:Original post by Spa8nky
Thanks very much for all your help with this one guys.

Using a GUID has also solved the query I had in my previous thread.


How do you use the GUIDs? I think if you use them in some acceleration structure (e.g. a complete node in a Kd-tree is only 8 bytes in ray tracing *) or for groups of vertices, they waste way too much memory; I think 16 bytes per single ID, when your ID is only used within your application, is total exaggeration.

Or is it somehow mandatory to identify your data in a solar system wide pool? I seriously mean it; do you know that when you lay down each possible 64bit value in a row, with a sub-millimeter distance (afair 0.002 cm), you'll already range the whole solar system? With 65 bit you double that range, 66 bits is 4x the range, et cetera.

Can you guarantee that the number of unique id's never exceeds 18,446,744,073,709,551,616 (==18,446,744,073,709 million == 18,446 million billion)? Use a uint64 (half of a GUID). Never exceeds 4,294,967,296? Use a uint32 (1/4 of a GUID). Never exceeds 65,536? You are lucky, you can use a uint16 (1/8 of a GUID).

Not specifically addressed at you, just throwing that in: Having way more powerful machines these days, nobody has to wonder about why applications still seem to crawl like back in the day, when so many application developers are wasting so much bandwidth, computing time, and memory.

*: I once crunched a special Kd-tree node into 4 bytes (afair) to store 130 million triangles in a real time ray tracer, in less then 512 MiB of main memory. Using another size optimisation, 2 byte nodes would have been possible.
Quote:
How do you use the GUIDs?


They are used to represent objects in pools that are drawn as Quads.

E.G.

Bullets. I can have a maximum of say 500 bullets. Therefore there will only be a maximum of 500 GUIDs for all bullets.

All bullets are created during load time and then cycled through. If there are 501 bullets then the bullet that was first created will be replaced with the 501st bullet, still leaving 500 bullets with the maximum of 500 GUIDs.

Heck, I could even get away with returning a ushort for my GUID. Am I wasting bandwidth, computing time, and memory using this method?

I was just very glad it worked, but now you got me worried :(
All your arrays are automatically id'd first member to last member.
If the upper limit of your buffer is known, why even use a dictionary and GUIDs? You could just as well use a fixed sized array and use the array index as ID. Instead of removing quads from that array you could just set them to null. That would not only save the need for a GUID but speed up lookups as well.
Quote:
If the upper limit of your buffer is known, why even use a dictionary and GUIDs?


The dictionary is there to order each Quad under the effect type (HLSL shader) for which is to be drawn.

GUIDs are needed because for the time being it seems more logical referencing Bullet Quads that way (E.G. Easier to debug).

This topic is closed to new replies.

Advertisement