My use case for packed integer arrays

I am using Elm and TypeScript to create a world edit tool for Survivalcraft, which is very similar to Minecraft. This project, named EditSC, works similarly to MCEdit. Survivalcraft worlds have two files: a binary chunks file and an XML project file. The format of the chunks file is described here. The chunks file stores blocks, while the project file stores entities, world configuration, and some block data.

My original plan was to use Elm for storing data from project file and for displaying the user interface, and to use TypeScript for storing the chunks file and for 3D rendering. But I have recently been considering using Elm for storing and manipulating the chunks data (and maybe even for 3D rendering). For this to be possible, a packed array of integers (like UInt32Array) is needed.

Memory efficiency is extremely important in this scenario because I will be storing lots of arrays (possibly hundreds) with 65536 32-bit integers each.

I would need these features:

  • Support for 3-dimensional arrays

  • Ability to control individual bits of each integer in the array

  • Getting and setting slices of the array

  • Ability to efficiently clone arrays

  • Ability to efficiently read and modify arrays from JavaScript

  • Support for functions similar to things like List.map

  • Ability to convert to and from binary data with complete control over how multi dimensional arrays are stored

1 Like

would this be possible

I’d say the answer is “it’s probably never going to happen”.

There is elm/bytes which covers some of your requirements but Ability to efficiently read and modify arrays from JavaScript is not possible as elm/bytes is designed for encoding and decoding binary to Elm values, not directly modifying binary data.

Adding something that allows for directly modifying a byte array while still having pure functions and immutability would be tricky. It would probably require the Elm language to add support for affine types which I imagine will get implemented right after typeclasses :wink:

1 Like

Actually I probably wouldn’t need direct access from JavaScript

There might be some misunderstanding so to clarify, as I understand it, you want to be able to work with an array of bytes which you can read and write to efficiently (as in, constant time). For that I think something like affine/linear types would be needed which Elm doesn’t support and I doubt will be supported anytime soon if ever.

The array would have to be accessed about every minute

with access that infrequent, I wonder if you could use ports and store the array on the JS side as global.

Because you say the access is about every minute, I’d guess that means the access is based on an event, that is currently handled in update? If so, you could have a type

type ArrayRequest = Set Int Int | Get Int | Slice Int Int | ...

type ArrayResponse = AtIndex Int Int | Slice (Array Int) | ...

you can send the array request over a port, and interpret it on the js side, then send responses back as appropriate.

yeah thats what i originally planned and i will probably have to do that

btw im working on an improvement to your int64 package

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.