jonniedie / ComponentArrays.jl

Arrays with arbitrarily nested named components.
MIT License
286 stars 34 forks source link

Switch from a mutation loop to a generator in a `make_idx` path #242

Closed LilithHafner closed 5 months ago

LilithHafner commented 5 months ago

The new version is 1.5x faster for constructing a ComponentArray with 100 fields each with a 10 element vector.

Benchmarks:

julia> using ComponentArrays, Chairmarks
Precompiling ComponentArrays
  1 dependency successfully precompiled in 1 seconds. 36 already precompiled.

julia> s(i) = Symbol("x"*string(i))
s (generic function with 1 method)

julia> const NT = (;(s(i) => randn(10) for i in 1:100)...);

julia> f() = ComponentArray(NT)
f (generic function with 1 method)

julia> @b f() # PR
28.292 μs (246 allocs: 63.703 KiB)

shell> git checkout main
Switched to branch 'main'
Your branch is up to date with 'upstream/main'.

julia> @b f() # main
42.834 μs (849 allocs: 84.344 KiB)

This is part of a journey to make ComponentArray(x = [1,2,3]) not use mutation so that it is compatible with Zygote.jl which is motivated in part by https://github.com/DARPA-ASKEM/sciml-service/issues/141.

jonniedie commented 5 months ago

Oh awesome. Thanks for this. This has been a long-standing issue that I've wanted to fix, but haven't really had the time to dive into.

codecov-commenter commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (94ab225) 73.99% compared to head (f2d9983) 73.92%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #242 +/- ## ========================================== - Coverage 73.99% 73.92% -0.07% ========================================== Files 23 23 Lines 746 744 -2 ========================================== - Hits 552 550 -2 Misses 194 194 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.