Skip to content

Better default sizeof(::AbstractArray)? #36714

Open
@mbauman

Description

@mbauman

By default, new AbstractArray subtypes fall back to the fully generic sizeof(::Any) implementation that simply gives the size of their fields. Given that we know more about what an AbstractArray is — and given that we have a default write implementation, it seems like we should be able to do a better job of getting closer to the docstring of sizeof:

Size, in bytes, of the canonical binary representation of the given DataType T, if any. Size, in bytes, of object obj if it is not DataType.

For example:

julia> write(IOBuffer(), rand(5,5))
200

julia> sizeof(rand(5,5))
200

julia> write(IOBuffer(), rand(5,5)')
200

julia> sizeof(rand(5,5)')
8

Even the "compressed" representations by default write their whole contents:

julia> using LinearAlgebra

julia> write(IOBuffer(), Diagonal(rand(5)))
200

julia> sizeof(Diagonal(rand(5)))
8

So it'd seem like a sizeof(A::AbstractArray) = length(A) * sizeof(eltype(A)) definition would be better all around. If your array has a specialized canonical representation, then you define sizeof to match when you implement write.

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