Consider implementing ByteArray and Array in Inko with a set of primitive instructions
ByteArray
is currently implemented using a mixture of pure Inko code and built-in functions. Since byte arrays tend to get rather large, operations such as appending, slicing, etc may be too expensive when implemented in Inko (e.g. a series of push()
calls). Adding all sorts of built-in functions also gets really annoying.
A possible solution is to add a set of primitive memory management instructions to the VM. These operate on regular unmanaged memory, and could be used to implement ByteArray
(and possibly Array
in the future) in Inko itself; instead of relying on an ever growing list of built-in functions. VM methods that currently produce a ByteArray
would instead produce something like a *mut u8
, while methods expecting a ByteArray
would expect a &mut [u8]
(created from a *mut u8
and a length).
The instructions would be at least as follows:
alloc(size in bytes)
free(pointer)
copy(from, to, size in bytes)
realloc(pointer, new size in bytes)
write(pointer, byte)
read(pointer)
To handle differently sized types we'd probably also have to pass a type size argument, instead of always operating on bytes.
Moving Array
over to such a setup will probably be more difficult, as there are several places where the VM directly uses it. For example, the built-in function directory_list
returns an Array.
Either way, this will require some thinking. It might not even be worth it if/when we move to a native code compiler, as at that point we'd probably be able to better optimise built-in function calls and just do the ugly work in Rust.