Skip to content

Unified, efficient code for mutable and immutable AbstractArrays #32

@c42f

Description

@c42f

As mentioned in #31, nobody wants to write two versions of code, one for mutable arrays, vs one for immutable arrays like StaticArray. I'd like a central place where we identify the main issues, and discuss minimal changes to the AbstractArray interface or julia compiler which would allow packages like StaticArrays to exist without rewriting a lot of algorithms from Base. Here's a very incomplete list, I hope people will jump in with more detail:

API

  1. similar() assumes a mutable container can be created efficiently filled in an iterative way. Julep: setfield! for mutable references to immutables JuliaLang/julia#17115 goes some way to solving this, though we'd be working with a statically sized buffer, something like Ref{T<:StaticArray} and would need to unwrap this to return the value.
  2. The hvcat() API doesn't reflect the structure of the input expression AST in a way which can give a type inferred output. (Potentially solvable with constant prop, without API changes?)

Optimization

  1. similar() with a size argument is not type inferrable for StaticArray. Constant prop in inference could potentially fix this in many cases, Ref. Do some constant propagation before type inference runs JuliaLang/julia#5560.
  2. getindex() with fancy indexing is not type inferrable for StaticArray. Constant prop in inference could fix a bunch of important instances of this, eg literal ranges v[3:5] could be known to be a SVector{3}. But what about non-inferrable cases, even after constant prop? We'd like to return an Array in those cases, can we achieve this in principle?
  3. Loop unrolling - small static arrays can benefit a lot from this, but in StaticArrays we're forced to do it at the AST level via @generated functions which no doubt pushes the compiler pretty hard and generates a heap of garbage. More interesting would be an @unroll meta to instruct codegen to unroll a given loop if the range is statically known.

Obviously one thing we can't fix is differences in the most appropriate algorithm for small vs large objects. For example, eigen decomposition of 2x2 matrices should probably just be done using a specialized analytical expression for that size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    designspeculative design related issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions