为什么 V8 使用指针标记而不是 NaN 装箱? [英] Why does V8 uses pointer tagging and not NaN boxing?

查看:63
本文介绍了为什么 V8 使用指针标记而不是 NaN 装箱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 V8 内部结构.我了解到 V8 使用 指针标记 进行值存储,但想知道为什么不使用 NaN 装箱.

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 拳击更好,因为它也可以存储双打而不仅仅是 SMI.我已经阅读了this,并理解(如果是这样)为什么不使用 NaN 拳击在 32 位平台上.但在 64 位平台上我不明白为什么.

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.

我怀疑原因与 SMI 有关.也许它们不能使用 NaN 装箱存储?我认为他们可以.我们有 52 个多余的位(我们甚至可以使用超过 32 位).也许这将需要额外的屏蔽操作,这会使整数数学运算变慢?但是我们已经需要做按位移位了!

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.

推荐答案

(此处为 V8 开发人员.) NaN 装箱和指针标记是具有不同权衡的设计选择,严格来说两者都没有比另一个更好.V8 使用指针标记的决定早在我加入项目之前就已经做出,所以我只能推测当时可能是什么具体原因.

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

指针标记的优点是:

  • 显着减少内存消耗(当然在 32 位平台上;使用指针压缩"); 在 64 位平台上也是)
  • 稍微更高效(小)整数运算,因为大多数 CPU 的整数运算比它们的双运算快.一旦优化编译器出现,这可能就完全无关紧要了.
  • 更高效的指针操作,因为您可以在访问对象字段时简单地添加调整后的偏移量(这与根本不玩任何指针技巧的性能相同),而不是必须屏蔽 NaN 的不相关部分.一旦优化编译器出现,这可能就完全无关紧要了.
  • 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.

正如您所指出的,NaN 标记的主要好处是它支持完整的双倍范围,这在某些情况下非常好.您可以基于任一技术构建性能良好的引擎.

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.

这篇关于为什么 V8 使用指针标记而不是 NaN 装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆