Live data from Hacker News

Handles Are the Better Pointers (2018)

floooh.github.com

21–30 of 113 posts

Re: Handles Are the Better Pointers (2018)

#21

Moving away from pointers and using a shared pool of handles with integer indexes will introduce a whole new array of issues. A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. I remember debugging a regression with UNIX network sockets where valid connections were being killed, and the bug was only triggered under heavy lo…

> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references.

A pointer is just an integer index to a byte in memory in most computing architectures.

Re: Handles Are the Better Pointers (2018)

#23

Moving away from pointers and using a shared pool of handles with integer indexes will introduce a whole new array of issues. A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. I remember debugging a regression with UNIX network sockets where valid connections were being killed, and the bug was only triggered under heavy lo…

> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. All of this applies to pointers as well.

In C world yes, in C++ world no, because of weak_ptr.

Re: Handles Are the Better Pointers (2018)

#24

(2018) This is often a really useful strategy to use in Rust code as well.

You can also use it in GCed languages if you want to avoid creating cycles in the object graph.

Yes, Go supports this idiom very well.

Re: Handles Are the Better Pointers (2018)

#25

Earlier quoted context omitted.

> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. The article suggests a handle containing a "plain int" plus a "unique bit pattern", the latter acting like a watermark which is compared to the one in the private array of the corresponding module, preventing dangling accesses. Apart from rare occasional collisions in the…

You are essentially passing an database index key to the getter-function. The only issue I see is that your system has a database integrated into it.

The translation of that key to a memory location should be very straightforward. A few bitwise operations to extract the necessary pieces from the handle, a multiplication of the index, an addition to the private base pointer and there you go. Nothing fancy or CPU-expensive.

Re: Handles Are the Better Pointers (2018)

#26

Earlier quoted context omitted.

> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. All of this applies to pointers as well.

In C world yes, in C++ world no, because of weak_ptr.

Weak_ptr in C++ only applies to shared_ptr, which is not used for most C++ pointers. Because of the atomic ops done on each copy, in performance sensitive contexts people aren't going to be using it.

Re: Handles Are the Better Pointers (2018)

#27
post #5

I am surprised that not every large project uses this technique. If you are running on a 64bit system, the use of 32bit handles instead of pointer results in huge memory savings. You can also put the start addressed of the arrays (the handles are indexing into) anywhere in virtual memory and grow them as needed. Finally you can stripe the data for one object into multiple arrays and use the same handle to index each…

> you can stripe the data for one object into multiple arrays and use the same handle to index each of them which can improve locality. I wish programming languages made it easy to structure arrays of objects like this. I think Johnathan Blows’s JAI has some kind of transparent support for SoA.

This basically is how tables work in k. Tables are defined as a list of dictionaries, but in fact stored as an array for each element (ie. each column of the table is an array, but each row can be read as a dictionary).

Re: Handles Are the Better Pointers (2018)

#28

I am surprised that not every large project uses this technique. If you are running on a 64bit system, the use of 32bit handles instead of pointer results in huge memory savings. You can also put the start addressed of the arrays (the handles are indexing into) anywhere in virtual memory and grow them as needed. Finally you can stripe the data for one object into multiple arrays and use the same handle to index each…

[deleted]

Re: Handles Are the Better Pointers (2018)

#29

Earlier quoted context omitted.

> A plain int carries no information about the validity of the object behind that handle because they are carried around as copies, not references. The article suggests a handle containing a "plain int" plus a "unique bit pattern", the latter acting like a watermark which is compared to the one in the private array of the corresponding module, preventing dangling accesses. Apart from rare occasional collisions in the…

You are essentially passing an database index key to the getter-function. The only issue I see is that your system has a database integrated into it.

I mean, calling it a database might be technically accurate, but is misleading in the sense the it gives the impression that it's a big heavyweight thing. It's not like there's a SQL implementation in there.

Re: Handles Are the Better Pointers (2018)

#30
Thinking out loud:

Since handles refer to offer rather than absolute addresses, this sort of scheme should allow for "serializing" data by memmapping chunks of memory to disk.

Obviously, this scheme wouldn't be useful for cross-platform save files, and might not even translate across builds.

Still, as long as you have a robust scheme for invalidating data when a new version is deployed, I could see this being useful for caching, and for saving state for short periods of time on mobile platforms that like to restart processes with little warning. (Rather than potentially losing your place within the game level any time you switch between apps, you only lose your place within the game level when an app update gets installed.)

Post reply on HN