pointersoptimizationv8javascript-enginenan-boxing

Why does V8 uses pointer tagging and not NaN boxing?


I'm learning V8 internals now. I learned that V8 uses pointer tagging for value storing, but wondered why it is not use NaN boxing.

AFAIK, NaN boxing is better because it can also store doubles and not just SMIs. I've read this, and understand (if that true) why not use NaN boxing on 32-bit platforms. But on 64-bit platforms I don't see why.

I suspect the reason has something to do with SMIs. Maybe they can't be stored using NaN boxing? I think they can. We have 52 superfluous bits for them (we can even use more than 32 bits). Maybe this will require additional masking operations that will render integer math slower? But we already need to do bitwise shift!

I don't know why. Thanks for anyone willing to answer.


Solution

  • (V8 developer here.) NaN boxing and pointer tagging are design choices with different tradeoffs, neither is strictly better than the other. V8's decision to use pointer tagging has been made long before I joined the project, so I can only speculate what the specific reason(s) might have been at the time.

    Advantages of pointer tagging are:

    As you point out, the main benefit of NaN tagging is that it supports the full double range, which is very nice in some situations. You can build a well-performing engine based on either technique.