#1 Members - Reputation: 615
Posted 15 November 2012 - 04:12 PM
Is this a good idea?
#2 Moderators - Reputation: 7737
Posted 15 November 2012 - 04:26 PM
What happens if you save and reload the game? Every object will be in a different address. What happens when you free an object, and allocate another object later? It could potentially re-use the memory address even though the object is different.
Unique IDs per object can be great for many game designs. A simple number assigned to each object is generally sufficient.
#4 Members - Reputation: 919
Posted 15 November 2012 - 04:27 PM
If you ever need networking, then it's useless, because memory pointers will be different on different machines, if you save games, then you can't save/load your guids, because you can't guarantee the addresses will match when you load. You can't even guarantee that your guids won't be recycled (since the same address might be reallocated after an object is deleted.
So, given those problems, I can't see any advantage calling those things a Guid, rather than being explicit about it and calling it a pointer.
Basically, don't do it.
#5 Members - Reputation: 1225
Posted 15 November 2012 - 06:19 PM
#6 Crossbones+ - Reputation: 398
Posted 17 November 2012 - 08:12 PM
#7 Members - Reputation: 1432
Posted 17 November 2012 - 08:29 PM
Edited by alnite, 17 November 2012 - 08:29 PM.
#8 Moderators - Reputation: 7663
Posted 18 November 2012 - 05:01 PM
UUIDs are massive overkill for this kind of thing.
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#9 Members - Reputation: 640
Posted 18 November 2012 - 07:16 PM
Say you're sorting a handful of pointers, you can simply use the address.
One problem is if these are allocated non-continuous, each ptr is heap allocated, then the sort result will not be deterministic.
Your tools may sort your ai nodes in one order and the build process in another, so when you go to debug node 25 in your tool, it's not the same node 25 that the build process created.
A problem with incrementing IDs is that they wrap. For small tests it may appear that everything works fine. But after soaking your game for a couple days you will very likely wrap your IDs, then you need to deal with "well where do i get the next id from?" you'd need to returned freed ids to a list.
So if you need short lived, automatically recycling ids that simply guarantee uniqueness (locally) and are not deterministic, the memory address is perfectly applicable.
In most other cases, with multiple machines, or multiple runs, where the ids much be the same.. the memory address will fail miserably.
#10 Moderators - Reputation: 7663
Posted 18 November 2012 - 08:46 PM
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#11 GDNet+ - Reputation: 1739
Posted 19 November 2012 - 12:38 AM
Except I've often found it useful to create those indices independantly, for each pool of objects so the id is
(pool, objectIndex) where an object can be accessed simply by pool[objectIndex].
This still has the easiness of the incremental pointer and provides some useful insight on the type of the object (if the various pools are type-coherent) or their lifetime (if the pool is temporally coherent).
#12 Members - Reputation: 640
Posted 19 November 2012 - 07:01 AM
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?
Maybe there are duplicate entries in the container, and you want to correlate them. Programming is a mysterious world, with many mysterious tasks.
#14 Senior Moderators - Reputation: 4742
Posted 19 November 2012 - 09:19 AM
Unless, of course, your allocations are guaranteed to be in-order and contiguous placement allocations...Not to mention using memory ptrs makes your application undeterministic. Each time you run the program, if your data is sorted by pointers, the order will always be different.
The world of memory-friendly optimisations is a strange and wonderful place ;)
Tristam MacDonald - SDE @ Amazon - swiftcoding [Need to sync your files via the cloud? | Need affordable web hosting?]
#15 Moderators - Reputation: 7663
Posted 19 November 2012 - 08:31 PM
Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?
Maybe there are duplicate entries in the container, and you want to correlate them. Programming is a mysterious world, with many mysterious tasks.
I'm hard pressed to think of a situation that can't be solved by better means than comparing pointer addresses. That's just begging for undefined behavior.
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
#16 Moderators - Reputation: 6658
Posted 19 November 2012 - 09:09 PM
Occasionally, sorting a container of pointers by pointer value can be a performance boost, provided that you need to do batch processes on all the elements in the container, the container contains a sufficient number of pointers, the objects pointed to are sufficiently small enough that multiple objects can fit on a cache line (preferably L1), but the data set is large enough that an appreciable number of cache misses occur, the objects are allocated in a way with low fragmentation and inline overhead and the container is not modified (and hence resorted) frequently. Usually a complete reorganization of data storage is more effective in this kind of situation, but it makes for a low programmer effort speed boost if it works.Why on earth would you ever sort a container of pointers by their address values? What is that even going to accomplish?







