Linearindexing, LinearFast and LinearSlow


#1

I have been trying to figure out when exactly is it clearly stupid not to define linearindexing(::Type{T}) = Base.LinearFast() for some (1D) indexable type T? So could someone please explain when to use Base.LinearFast and why I get the following errors even though all of Array, String and Tuple are indexable?

julia> Base.linearindexing(Array)
Base.LinearFast()

julia> Base.linearindexing(String)
ERROR: MethodError: no method matching linearindexing(::Type{String})
Closest candidates are:
  linearindexing(::Base.LinearFast, ::Base.LinearFast) at abstractarray.jl:239
  linearindexing(::Base.SparseArrays.CHOLMOD.Dense{T<:Union{Complex{Float64},Flo
at64}}) at sparse\cholmod.jl:1124
  linearindexing(::Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},F
loat64}}) at sparse\cholmod.jl:1145
  ...

julia> Base.linearindexing(Tuple)
ERROR: MethodError: no method matching linearindexing(::Type{Tuple})
Closest candidates are:
  linearindexing(::Base.LinearFast, ::Base.LinearFast) at abstractarray.jl:239
  linearindexing(::Base.SparseArrays.CHOLMOD.Dense{T<:Union{Complex{Float64},Flo
at64}}) at sparse\cholmod.jl:1124
  linearindexing(::Base.SparseArrays.CHOLMOD.Sparse{Tv<:Union{Complex{Float64},F
loat64}}) at sparse\cholmod.jl:1145
  ...

Also, when should I use Base.LinearSlow and how is it different from LinearFast in terms of speed difference and functionality?

Thank you!


#2

linearindexing is only used for AbstractArray, tuples and strings implement directly their own getindex methods.

See the manual for an explanation. Basically it depends on what’s the most natural way to index into your custom array type.


#3

I see. So for 1D indexable types, linearindexing is never going to be used, which I suppose is why it was not defined for String or Tuple and only defined for AbstractArray. And when it said LinearSlow() is the default value for linearindexing(::Type), it was referring to types that are subtypes of AbstractArray, not in general. Thanks alot.

julia> Base.linearindexing(AbstractArray)
Base.LinearSlow()