does V8 uses stack and heap like the JVM? if so does it put primitives on the stack and objects on the heap?
-
1Not making this a full answer since I didn't fully read the article, but there were definitely discussions of stack/heap/register usage in wingolog.org/archives/2011/07/05/v8-a-tale-of-two-compilersDomenic– Domenic2011-07-06 21:43:31 +00:00Commented Jul 6, 2011 at 21:43
-
see stackoverflow.com/questions/16949690/…wcy– wcy2013-09-30 07:00:04 +00:00Commented Sep 30, 2013 at 7:00
3 Answers
- In V8
null,undefined,trueandfalseinternally are heap allocated objects. If you are comming from Java you can say thattrueandfalsein V8 are more likeBoolean.TRUEandBoolean.FALSEin Java. - There is an important difference between real local variables and variables that are captured by closures or shadowed by eval/with. Captures variables are stored in a special heap allocated structure called Context and are accessed indirectly. For more details about real vs. context allocates variables see my answer to a different question
V8 has two compilers: non-optimizing (aka full) and optimizing one:
- Non-optimizing compiler can't store floating point numbers and integers beyond 31-bit (32-bit on x64) on the stack it always boxes them into HeapNumbers. It does not try to do register allocation and stores real local variables on the stack.
- Optimizing compiler is much smarter. It does register allocation (linear scan) and can keep full 32-bit integers and floating point numbers on the stack and in the registers (including XMM registers).
Speaking of JVM: it can perform so called stack allocation and allocate a non-escaping object on the stack instead of the heap. A more generic optimization (scalar replacement) can sometimes completely eliminate allocation of non-escaping object and explode it into separate fields.
1 Comment
Yes, V8 uses a heap similar to JVM and most other languages. This, however, means that local variables (as a general rule) are put on the stack and objects in the heap. This may for instance not hold if a function closes over these values. As in the JVM, primitives can only be stored on the stack if they are stored in a local variable.
As a user it is not something you would normally need to worry about.