Skip to content

Base.summarysize incorrect for Memory containing Union #57506

Closed
@PatrickHaecker

Description

@PatrickHaecker

As discussed in the corresponding Discourse topic, Base.summarysize accounts the union optimization byte twice. Copying from the discussion:

julia> sizeandsummary(x) = sizeof(x), Base.summarysize(x)

julia> 1:100 |> Memory{UInt8} |> sizeandsummary
(100, 116)

julia> 1:100 |> Memory{Union{Nothing,UInt8}} |> sizeandsummary
(200, 316)

Where the last number should be 216, not 316.

The relevant code in summarysize.jl is as follows

        dsize = sizeof(obj)
        T = eltype(obj)
        if isbitsunion(T)
            # add 1 union selector byte for each element
            dsize += length(obj)
        end

The line dsize = sizeof(obj) accounts for the union byte array (see above). As Union{Nothing, UInt8} |> Base.isbitsunion (unsurprisingly) returns true, the union byte array is accounted a second time in dsize += length(obj).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions