in computers, there's no real multi-dimension arrays, all data are placed in memory in a one-dimension address space. [1]
so when you slice a "muti-dimension" array, you only get contiguous range in certain special cases, i.e. all "inner" dimensions are fully covered.
a more general solution would be some scattered data structure, typically represented with a base address and some "stride" information for the dimensions, and the address for individual elements can be easily calculated in constant time.
yes.
if this kind of slicing operation is the common one for your use case, changing the data layout in this way is a good optimization, it will give you contiguous memory regions for the slices.
small suggestion for the code though, this line
let slice = world[z];
can be changed to
let slice = &world[z];
you code create a copy of the subarray, which might not be desirable when the data is large and you don't need mutability anyway. but I don't know your exact use case, so it's just a suggestion.
physically, the DRAM hardware actually does decode the address and activate the columns in an hierarchical manner, sort of multi-dimensional, but software are hidden from the physical layout of memory cells, for good reasons like compatibility and security etc, and that's how most computer architectures are designed, but IMO it is a waste of hardware potentials in some way ↩︎