4 ms·
Three thing: a) pointers are handles, to parts of virtual memory pages. When your process maps memory, those addresses are really just ... numbers... referring
by cmrdporcupine 3y ago
Three thing:
a) pointers are handles, to parts of virtual memory pages. When your process maps memory, those addresses are really just ... numbers... referring to pages relative to your process only. Things like userfaultfd or sigsegv signal handlers are even capable of blurring the line significantly between self-managed handles and pointers even by allowing user resolution of page fault handling. Worth thinking about.
b) If performance is a concern, working through a centralized handle/object/page table is actually far worse than you'd think at first -- even with an O(1) datastructure for the mapping. Especially when you consider concurrent access. Toss a lock in there, get serious contention. Throw in an atomic? Cause a heap of L1 cache evictions. Handles can mess up branch prediction, they can mess up cache performance generally, and...
3) they can also confuse analysis tools like valgrind, debuggers, etc. Now static analysis tools, your graphical debugger, runtime checks etc don't have the same insight. Consider carefully.
All this to say, it's a useful pattern, but a blanket statement like "Handles are the better pointers" is a bit crude.
I prefer that we just make pointers better; either through "smart pointers" or some sort of pointer swizzling, or by improving language semantics & compiler intelligence.
- jjnoakes 3y ago> pointers are handles Pointers are quite limited handles - they would all to be handles to the same array with base pointer 0, but that removes many of the useful benefits you get from having different arrays with different base pointers.
- taeric 3y agoMy hunch is that b is often mitigated by the fact that you were having to touch many items? Such that you don't necessarily even care that it is O(1) in lookup, you are iterating over the items. (And if you aren't having to touch many of the items on the regular, than you probably won't see a benefit to this approach?)