What is the size of an integer variable in Dart

青春壹個敷衍的年華 提交于 2021-02-10 20:25:33

问题


In Dart site they said that the integer size is 64 bit , is this the max size of an integer, or this is the size of any integer even if the integer is a small number such as 12?

If every integer size is 64 bit , does this affect the performance of the application?


回答1:


The answer is: "It depends".

First of all, if you compile Dart to JavaScript, all numbers are JavaScript numbers, which means Dart doubles. Even the integers, they just happen to be non-fractional doubles. That means that you have at most 53 bits of precision.

The native Dart numbers are 64-bit integers. The VM may represent them as smaller numbers internally if it can see an advantage in that.

On a 64-bit VM, all pointers are 64 bits, so you can't store anything smaller than that in the heap (at least outside of typed-data lists). Still, if your value is a 63-bit signed integer, the VM can store it directly in a pointer (a so-called "small integer" or "smi"), otherwise it has to allocate a heap object to store the 64 bits, so that it can still tell the difference between an integer and a heap pointer. That's important for garbage collection.

On a 32-bit VM, all pointers are 32 bits, and if your integer is a 31-bit signed integer, it can be stored in the pointer, otherwise it becomes a heap object.

That's for storing numbers. For a local variable inside a function, the VM can choose to unbox the value, storing plain integers on the stack instead of heap-numbers or tagged small integers. If the compiler knows that you only use 16-bit integers, it can (theoretically) use only 16 bits for it. In practice, anything smaller than 32 bits is probably less efficient, and perhaps even that on 64-bit platforms.

These optimizations happen for performance reasons, so yes, it affects performance, and the compiler tries to avoid unnecessary overheads.

That's the current implementation. It an change at any time as long as you can't tell the difference when running your program.



来源:https://stackoverflow.com/questions/58161519/what-is-the-size-of-an-integer-variable-in-dart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!