Skip to content

Performance of broadcasting scalars over views is order-dependent #22165

@GravityAssisted

Description

@GravityAssisted

Consider these two simple examples. work is a vector by byr1 is a scalar. I am trying to achieve a non-allocating array assignment.

work = rand(6)
byr1 = 0.4
julia> @benchmark @inbounds @views $work[4:6]  .= $byr1.*$work[4:6]
BenchmarkTools.Trial: 
  memory estimate:  48 bytes
  allocs estimate:  1
  --------------
  minimum time:     18.073 ns (0.00% GC)
  median time:      18.703 ns (0.00% GC)
  mean time:        24.711 ns (20.62% GC)
  maximum time:     2.557 μs (97.40% GC)
  --------------
  samples:          10000
  evals/sample:     997

julia> @benchmark @inbounds @views $work[4:6]  .= $work[4:6].*$byr1
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     8.319 ns (0.00% GC)
  median time:      8.599 ns (0.00% GC)
  mean time:        8.725 ns (0.00% GC)
  maximum time:     25.867 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     999

Why is the first case allocating while the second case is not ? Looks like the allocation happens only if I multiple byr1 before work array. These seems like a bug or maybe I missed something here?

thanks

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