Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better default sizeof(::AbstractArray)? #36714

Open
mbauman opened this issue Jul 17, 2020 · 0 comments
Open

Better default sizeof(::AbstractArray)? #36714

mbauman opened this issue Jul 17, 2020 · 0 comments

Comments

@mbauman
Copy link
Member

mbauman commented Jul 17, 2020

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.

mbauman added a commit that referenced this issue Jul 17, 2020
Not all `AbstractArray`s define elsize.  Cf. #35900, #36714.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant