Skip to content

Extra allocations with hvcat of mixed arrays and scalars #39713

Closed
@BioTurboNick

Description

@BioTurboNick

On 1.6.0-rc1:

const a, b, c, d = zeros(Int, 2, 2), [3 4], [2 ; 4], 5
using BenchmarkTools
# mixed arrays and scalars
@btime [a c ; b d]   # 31 allocations and 1.25 kb -- uses generic fallback method
@btime [a c ; [b d]] # 21 allocations and 880 bytes
@btime [[a c] ; [b d]] # 16 allocations and 816 bytes -- explicit hcat nested within vcat
# scalars wrapped in arrays
@btime [a c ; b [d]] # 10 allocations and 512 bytes -- uses as::AbstractArray{T}... method
@btime [a c ; [b [d]]] # 9 allocations and 560 bytes
@btime [[a c] ; [b [d]]] # 4 allocations and 496 bytes -- explicit hcat nested within vcat

In theory hvcat should always be more efficient than nesting vcats and hcats, but not in this case. And I don't think there's a good reason a scalar should behave differently from an 1-element array in hvcat.

I'll try to work on this, but open to ideas.

(Also worth noting that the first 3 are quite a bit worse on 1.5.3, while the last three are the same.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions