Hacker News new | ask | show | jobs
by canyp 4 days ago
I still don't get what is the advantage over an unsigned integer. Yes, fp64 has unused bits. But why are you going to involve the FPU at all when a uint64 does the trick as well? Plus with a uint64 you get all the flexibility of what bits to dedicate to the address vs metadata.

Edit: I guess one advantage is that, if we later treat the handle like a pointer, NaN math gets you NaN again, whereas the uint64 math might get you an invalid address, or you'd need extra logic to check that the uint64 is not a valid handle?

2 comments

The benefit is that floats are allowed to be unboxed values - without NaN-boxing, you must heap-allocate them. The tradeoff is that immediate/unboxed integer values end up being smaller than the full machine word range (i.e. you have either a 24-bit or 48-bit mantissa you can use to hold data), but that's usually worthwhile because most integers are small anyway, so you box larger ones. Similarly, pointer values can't use the full address space, but that's also usually worth it since rarely do you actually need to do so in VMs where this technique is used.

Using unsigned integers is only a better choice if your VM doesn't need efficient floating-point operations.

Makes sense, thank you.
Honestly I’m with you there I use uint64, but I guess if you’re already storing f64 as a base number type then you just keep everything in a float and it’s more convenient.