4 ms·
The JVM doesn’t really have multidirectional arrays, it just has arrays with a type, and that type may itself be an array. Sometimes that can be useful, and so
by aardvark179 3y ago
The JVM doesn’t really have multidirectional arrays, it just has arrays with a type, and that type may itself be an array.
Sometimes that can be useful, and sometimes not.
- bowsamic 3y ago> The JVM doesn’t really have multidirectional arrays, it just has arrays with a type, and that type may itself be an array. That contradicts the article: > When the compiler encounters this code, it emits a unique bytecode, MULTIANEWARRAY, which creates an array with dimensions that are each set to the specified size.
- wffurr 3y agoDoes that bytecode do anything more efficient than allocating arrays inside of arrays inside of arrays etc? Does it ensure they’re contiguous? Does a multidimensional array allocated in this way still require pointer chasing to get to the values? It’s not really a helpful statement. Initializing a single flat array with stride metadata alongside gives you memory locality and arithmetic access. Intuitively, I wouldn’t expect the special multidimensional array bytecode to provide any of that.
- bowsamic 3y agoWell, /u/aardvark179 claimed that the JVM did not treat multidimensional arrays in any special way at all, i.e. that it is completely just a composition of features already available in the JVM, but that was wrong, since multidimensional arrays have their own special bytecode. Anything beyond that is irrelevant, I'm just correcting his mistake there.
- wffurr 3y agoOK, so they're treated in a special way that's completely irrelevant to the concerns one might have when using multidimensional arrays. It's just an implementation detail with no practical considerations.
- bowsamic 3y agoYes
- aardvark179 3y agoSorry, missed this yesterday. From the spec: “A new multidimensional array of the array type is allocated from the garbage-collected heap. If any count value is zero, no subsequent dimensions are allocated. The components of the array in the first dimension are initialized to subarrays of the type of the second dimension, and so on. The components of the last allocated dimension of the array are initialized to the default initial value (§2.3, §2.4) for the element type of the array type. A reference arrayref to the new array is pushed onto the operand stack.” It’s really a little utility method that happens to be pushed all the way to the byte code for historical reasons. The arrays it produces aren’t multidimensional in the ways you might hope, they are just arrays of arrays, which remain mutable and so can become ragged, or be reassigned in sneaky or horrible ways (e.g. with a subtyped array). There has been lots of talk over the years about how we could do better, often motivated by projects like Panama which are concerned with interfacing with other languages which do have firmer concepts of multidimensional arrays, but these are generally at a slightly higher level and try to avoid messing with lower level things the VM has to know about.
- ahoka 3y agohttps://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.html#jvms-6.5.multianewarray https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-6.h... This says otherwise.