3 ms·
It's not bypassing the borrow checker; it's bypassing references. C++ doesn't have a borrow checker, and the same pattern of forgoing references for integer IDs
by kibwen 8y ago
It's not bypassing the borrow checker; it's bypassing references. C++ doesn't have a borrow checker, and the same pattern of forgoing references for integer IDs is seen there. References are a just a tool to express certain pointer patterns, and like every other tool they have contexts for which they work well and contexts for which they don't. Really the most surprising result of all this is the broadening realization that references aren't the end-all abstraction for reasoning about pointer-like behavior (familiarity with smart pointers like C++'s shared_ptr makes this less of a surprising realization, but it's easy to initially dismiss a library-level feature as being less intrinsically fundamental than a language-level feature).
This isn't to say that futzing with integers is the best imaginable solution for these tasks. Language-level support (or at least stdlib support) for generational indices would be an interesting subject to pursue.
- rtpg 8y agoI think the point is that if you're juggling integers open you up to the huge correctness messes from before, even if theoretically you've gotten rid of your memory mess. Granted, you could `Option`-up all your operations, but the dynamic GC languages (or even C++) will let you operate on stuff "correctly", so long as your implementation is right. You can go `unsafe` and implement correct stuff and still be better than C++. I really feel like reference IDs aren't a great way to solve something like a doubly-linked list. Reference IDs make you lose almost all guarantees of correctness, unsafe + a good API will at least give you a fighting chance. There are problems (like entity system stuff) where reference IDs are just the way to go, though
- kibwen 8y ago> I think the point is that if you're juggling integers But I don't think anyone should be juggling integers themselves; they should be using a library (just like a library-defined smart pointer) that handles that (the presentation at https://www.youtube.com/watch?v=P9u8x13W7UE https://www.youtube.com/watch?v=P9u8x13W7UE may be of interest). > There are problems (like entity system stuff) where reference IDs are just the way to go, though Sure, and there isn't anything stopping anyone from both using them where they're practical and not using them where they're not. Some of the comments in this thread seem to be suggesting that you must choose one or the other, but that's simply not the case; using references in one part of one's program and indices in another is totally fine, as is using references and indices together in the same part of the program. They're not exclusive. In particular, nobody is saying that one should be reaching for indices regularly; I've written Rust for years and have never, ever needed them. References work just fine for plenty of applications. But we have other smart pointers for good reasons, and sometimes an application will call for the use one of those smart pointers, and that's totally cool; again, references may be thoroughly useful, but they are not the end-all be-all of pointer-like abstractions.
- wbl 8y agoSome of us have to write those libraries.
- AstralStorm 8y agoAnd the GCs too, so that they actually do not cause performance problems (e.g. unpredictable latencies) and help with debugging them instead of getting in the way.
- pjmlp 8y agoC++ is getting one via static analyzers though.
- kibwen 8y agoIf this is referring to the proposed C++ lifetime profile, it seems as though recently it has scaled back its ambitions from detecting all misuses of references to now detecting only common misuses of references. Still useful for C++ programmers, certainly, but it's no longer really comparable to Rust's borrow checker. And even with it, it's not going to change anyone from using an ECS to using a reference scheme; the point is that references aren't the best pointer abstraction for these sorts of thoroughly-dynamic graphs with elements of dynamic lifetime.
- pjmlp 8y agoYes I am referring to that proposal, as for ECS, indexes are not a must. https://www.gamedev.net/blogs/entry/2265481-oop-is-dead-long-live-oop/ https://www.gamedev.net/blogs/entry/2265481-oop-is-dead-long... In this particular case, currently Rust's borrow checker isn't of much help either, hence the workaround with generations.
- pcwalton 8y agoA broken one. I find this argument deeply frustrating. C++ doesn't even try to make graph memory management safe. Rust has several different strategies for graphs, all of which have some drawbacks, but which are all nonetheless safe. But because they aren't quite as ergonomic as C++, C++ somehow wins. No. C++ graph management is unsafe, regardless of whether you're using the lifetime profile.
- pjmlp 8y agoMy devil's advocate comments are exactly about "But because they aren't quite as ergonomic as C++, C++ somehow wins", better language ergonomics are critical to a language's adoption. One reason why Pascal, Ada and Modula-2 lost against C, was because they weren't ergonomic enough to the eyes of many developers. The typical magazine articles why using them felt like ceremony and straitjackets by those whose security wasn't on their top concerns. So I get to have a bit of experience being on the losing side, arguing how using array indexes with bounds checking (which could be disabled if really needed) was a better option than just incrementing away bare bones pointers. Or why explicit type casts were a better option. Ideally, I would be using C# or Java for everything I do, so it isn't about C++ vs Rust as such, rather making the point that it is not only about grammar and semantics when comparing programming languages. Given Biscuit's paper, I would say Go is on the good path if their Go 2.0 plans actually play as promise. Which remains to be seen, nonetheless. In the end it is a matter of ergonomics and perceptions.
- int_19h 8y ago> References are a just a tool to express certain pointer patterns I'd phrase it the other way - pointers are just a tool to express certain reference patters. The solution you proposed - integers - are another. Conceptually, the way both are used, both are references - a way to indirectly access another object. But if you do it with indices into another data structure, then you do, in fact, just work around the borrow checker, and escape to the land of freedom where you do whatever you want - and pay the price. Now you can pass them with impunity, sure... but now you also have to deal with indices that reference non-existent elements, or (depending on your usage patterns) indices that were not properly updated when an element was deleted from the middle etc.
- kibwen 8y ago> The solution you proposed - integers I explicitly said that I don't think integers are the solution; it's simply that for tasks like e.g. managing the thoroughly interconnected world state of a video game, where you have a web of dynamic entities, references aren't the right solution, and so people start to concoct other solutions using the primitives they have available. I mention that I think generational indices are the solution (or at least a solution). > But if you do it with indices into another data structure, then you do, in fact, just work around the borrow checker Why is it "working around the borrow checker" when it happens in Rust, but "employing an ECS architecture" when it happens in C++? The fundamental insight is that pointers and references as we know them kinda sorta suck at managing arbitrarily-interconnected webs of entities with dynamic lifetime; a garbage collector neatly solves this use case (dynamic lifetimes are their entire jam), but if you're using Rust or C++ then we're assuming that you have stricter performance requirements than a typical garbage collector provides (and if you don't, then you should consider using a GC'd language). An ECS-like approach using indices (which, if you squint and turn your head, resembles a very specialized form of garbage collection) is a way to keep entity management tractable without going all-in on GC. > now you also have to deal with indices that reference non-existent elements, or (depending on your usage patterns) indices that were not properly updated when an element was deleted from the middle etc. Generational indices address these problems. Again, I don't think anyone should use integers directly, you should seek to leverage abstractions that other people have already sat down and thought about.