SciML / SciML/ComponentArrays.jl

Unnecessary allocation when not unpacking?

Open
#161 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
375
Forks
42
Avg merge
7h 25m
Merged PRs (30d)
17

Description

If I don't unpack x = u.x a component vector before updating it, say with u.x .+= 1 instead of x = u.x; x .+= 1 , I get some extra allocations. The strange thing is that both x and u.x seem to be identical.

Is that intended/expected (and if so, mind me ask why?) or is it a bug?

julia> using ComponentArrays

julia> using BenchmarkTools

julia> u = ComponentVector(x = [0.9, 0.1])
ComponentVector{Float64}(x = [0.9, 0.1])

julia> function f1!(u)
           u.x .= 0.0
           nothing
       end
f1! (generic function with 1 method)

julia> function f2!(u)
           x = u.x
           x .= 0.0
           nothing
       end
f2! (generic function with 1 method)

julia> @btime f1!($(copy(u)));
  259.803 ns (2 allocations: 64 bytes)

julia> @btime f2!($(copy(u)));
  6.166 ns (0 allocations: 0 bytes)

julia> x = u.x
2-element view(::Vector{Float64}, 1:2) with eltype Float64:
 0.9
 0.1

julia> x === u.x
true

Here is the system info:

(ca) pkg> st
      Status `~/temp/ca/Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.1
  [b0b7db55] ComponentArrays v0.12.5

julia> versioninfo()
Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.5.0)
  CPU: Apple M1 Pro
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, westmere)
Environment:
  JULIA_NUM_THREADS = 8

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the f1! and f2! BenchmarkTools examples from the issue and compare their allocations for u.x .= 0.0 versus a local x. Inspect the ComponentArrays property-access and broadcast behavior involved; done means determining whether the allocation difference is expected or fixing it with a regression benchmark or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.