Skip to content

lazy parent eltype conversion during getindex #238

Open
@johnnychen94

Description

@johnnychen94

Because the struct definition hard-coded the eltype of the parent array:

struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
    parent::AA
    offsets::NTuple{N,Int}
end

whenever the wrapper OffsetArray is created, an implicity convert(AbstractArray{T, N}, parent) will be applied:

x = Int[1, 2, 3, 4]
xo = OffsetVector{Float32}(x)
parent(xo) === x # false

@btime OffsetVector{Float32}($x); # 34.555 ns (1 allocation: 96 bytes)
@btime OffsetVector($x); # 1.660 ns (0 allocations: 0 bytes)

This is an unnecessary memory allocation to me, and I believe we could relax the type to

- struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
+ struct OffsetArray{T,N,AA<:AbstractArray} <: AbstractArray{T,N}
    parent::AA
    offsets::NTuple{N,Int}
end

and do the eltype conversion during each getindex call.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions