0

So it turns out v8 arrays are stored contiguously, and if the array outgrows its current slot in memory, it will be reallocated in a different position in memory.

This got me wondering how about pointers. If this was in C and you reallocated the array, you have to get a reference to a new pointer!. This means that all of your code that used the old pointer will now have to use the new pointer.

This got me wondering how a JIT compiler such as v8 deals with dynamically changing variables. If you have var a = [] and then trigger v8 to grow the array size however, and it reallocates it in a different part of memory, then any code that references that a needs to be updated to point to the new address if the JIT stuff works similarly to how I imagine assembly does. (I have very little knowledge about assembly, C, and JIT).

So I'm wondering, how v8 manages these variable references internally. How does it work.

My confusion comes at the part where it connects to the actual memory addresses. Something has to be storing the actual memory address, something is hardcoded to that. But then if it is reallocated, those references need to be updated it seems.

Lance
  • 75,200
  • 93
  • 289
  • 503
  • 1
    I don't know about v8, but language runtimes invariably manage arrays as "dope vectors." Each of these contains a pointer to the memory block currently representing the array plus size data: for example number of dimensions and length of each. All other language constructs refer to the dope vector, not the memory block directly. When the array is resized, the pointer in the dope vector is adjusted. This is just one more example of the old computer science maxim that you can solve many, many problems with just another layer of indirection :-) See https://en.wikipedia.org/wiki/Dope_vector. – Gene Apr 24 '18 at 02:51
  • @Gene Awesome thank you! I have never heard of the "dope vector" (reading the wiki now). Wondering if there is any more information related to this topic. Not really sure how to search it, something about memory layout/addresses and pointers for variables in JIT. Would like to know about how they are defined and other related concepts. Such as, maybe there is a tree of dope vectors, or a linked list of them or something. – Lance Apr 24 '18 at 02:56
  • Get any good book on compilers and interpreters. The "Dragon Book" Aho, Sethi, and Ullman is a decent start, but there are others. – Gene Apr 24 '18 at 02:59
  • 1
    See also: [Dynamic Arrays](https://en.wikipedia.org/wiki/Dynamic_array), which consists of storage pointer, capacity, and used size. Other code references this array object, not the storage directly. Therefore, the storage can be reallocated. Dynamic arrays are the basis e.g. of the Java `ArrayList`, C++ `vector`, or Python `list`, and probably also how V8 implements JS arrays. They are a fun first data structure to implement. Dope vectors are an extension of dynamic arrays with extra metadata. – amon Apr 24 '18 at 09:05

0 Answers0