Skip to content

Significant performance gap in assigning to a subsection of an array with and without broadcasting #40962

@jishnub

Description

@jishnub
julia> a = rand(40,40); b = rand(40,40);

julia> @btime $a[1:end,1:end] .= $b;
  5.382 μs (0 allocations: 0 bytes)

julia> @btime $a[1:end,1:end] = $b;
  2.107 μs (0 allocations: 0 bytes)

Presumably, the destination is a Base.SlowSubArray for the broadcasted assignment.

Interestingly, the performance is reversed if the index ranges are replaced by colons. (in which case the destination is a FastContiguousSubArray)

julia> @btime $a[:,:] .= $b;
  753.160 ns (0 allocations: 0 bytes)

julia> @btime $a[:,:] = $b;
  2.125 μs (0 allocations: 0 bytes)
julia> versioninfo()
Julia Version 1.11.0-DEV.1397
Commit 0588cd40786 (2024-01-29 02:21 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  LD_LIBRARY_PATH = :/usr/lib/x86_64-linux-gnu/gtk-3.0/modules
  JULIA_EDITOR = subl

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]broadcastApplying a function over a collectioncompiler:simdinstruction-level vectorizationperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions