Skip to content Skip to sidebar Skip to footer

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

Solution 1:

(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:

  • significantly less memory consumption (certainly on 32-bit platforms; with "pointer compression" on 64-bit platforms too)
  • slightly more efficient (small) integer operations, because most CPUs' integer operations are faster than their double operations. This may not matter at all once an optimizing compiler enters the picture.
  • slightly more efficient pointer operations, because you can simply add an adjusted offset when accessing object fields (which has the same performance as not playing any pointer tricks at all), as opposed to having to mask off irrelevant parts of a NaN. This may not matter at all once an optimizing compiler enters the picture.

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.

Post a Comment for "Why Does V8 Uses Pointer Tagging And Not Nan Boxing?"