4 ms·
Using an index/ID rather than a direct object reference basically excludes the object from any kind of automatic memory management. Aren’t you just arguing that
by seanmcdirmid 8y ago
Using an index/ID rather than a direct object reference basically excludes the object from any kind of automatic memory management. Aren’t you just arguing that they should all be managed manually?
- Retra 8y agoThe object will still be freed when its owner is dropped... It's just the accesses to the object that are manually managed, not memory.
- seanmcdirmid 8y agoThere is no way to tell if there is an index to the object or not, so obviously it’s lifetime has to be managed manually.
- Retra 8y agoIndexes aren't references to objects, they're references to slots. The lifetimes of the objects are determined by whoever puts them into and out of those slots. That doesn't have to be determined manually. Sure, objects aren't deallocated when the index is dropped, but that doesn't mean it isn't being automatically deallocated somewhere else for some other reason.
- seanmcdirmid 8y agoWherever the object is deallocted, it can’t be done safely via the language or languages runtime because those indices are completely opaque. I’m not saying this is a bad thing, it definitely has a use case, but just that it isn’t really showing off a viable alternative to GC, it is very weird to be states as such. All manual memory management schemes work as you describe, they aren’t classes as automatic because the language runtime (or standard libs) can’t provide it for free.
- pcwalton 8y agoI don't see any reason why you couldn't have a Rust ECS that keeps track of the number of outstanding references to each entity and doesn't free them while there are any references left. You'd have an opaque "reference" object instead of an index. (Care would have to be taken to make sure the references can't go dangling when the vector is resized, but it should be possible.) But usually programmers intend to destroy entities in one place; for example, in UI code, a programmer typically expects a widget to be destroyed once it's removed from the window. It can be beneficial to have the runtime system diagnose if a reference to an object thought to be destroyed is actually used. So I haven't seen anyone actually go to the trouble…
- int_19h 8y agoAt this point you're basically writing a garbage collector. Which you might as well do with raw pointers, if it's a part of the language, and it will be more efficient.