MutatePlainDataArray.jl: Mutate nested immutables in arrays safely and efficiently

MutatePlainDataArray.jl is a tiny package that specifically aims to make mutating vectors of nested immutable fields easy.

The package exports only one function aref, that wraps a vector allowing subsequent mutation of nested fields using the following syntax:

    aref(v)[i].a.b._i._j[] = val

where _i and _j indicates getting fields in an immutable structs using index i and j, and can be used to access, for example, elements of a Tuple.

The mutation utilizes pointer arithmetics such that the final mutation is simply one memory assignment. The type constraints are enforced at type inference time, and bound checks are enabled unless @inbounds is used.

We also have an experimental feature that allows omission of the final [], but I would also like to know your thoughts!

Background for making this package: In some of our internal projects, we organize some data using nested (immutable) structs and group multiple of them in an array, but it becomes a hassle to change any one nested field in the entire array due to its immutability. We used Setfield.jl but the emitted code is not optimized enough. Therefore, inspired by UnsafePointers.jl, we made this package that makes such mutations much easier and more efficient.

4 Likes