V8 如何在内存中存储整数?
How does V8 store integers in memory?
例如整数 5?
我知道它将它存储在堆中,但它究竟是如何存储它的?诸如元数据和实际值本身之类的东西.在存储之前是否有一个常量添加到 int 中?
I know it stores it the heap, but how exactly does it store it? Things like metadata and the actual value itself. Is there a constant added to the int before storing it?
推荐答案V8 使用指针标记方案来区分小整数和堆对象指针.5 将存储为 Smi 类型,这不是 V8 中分配的堆.
V8 uses a pointer tagging scheme to distinguish small integers and heap object pointers. 5 would be stored as a Smi type, which is not heap allocated in V8.
您可以查看 源代码 供 Smi 类了解更多信息.
You can check out the source code for the Smi class to learn more.
在 32 位平台上,Smis 是一个 31 位有符号整数,底位设置为 0.在 64 位平台上,Smis 是一个 32 位有符号整数、31 位填充 0 和一个 0 代表底位.指向堆对象的指针的底部位设置为 1,因此 V8 可以在没有额外元数据的情况下区分指针和 Smis.
On 32-bit platforms, Smis are a 31 bit signed int with a 0 set for the bottom bit. On 64-bit platforms, Smis are a 32 bit signed int, 31 bits of 0 padding and a 0 for the bottom bit. Pointers to heap objects have a 1 set for the bottom bit so that V8 can tell the difference between pointers and Smis without extra metadata.
更多推荐
V8 如何存储像 5 这样的整数?
发布评论