JuliaDynamics / Agents.jl

Agent-based modeling framework in Julia
https://juliadynamics.github.io/Agents.jl/stable/
MIT License
717 stars 115 forks source link

Use a mutable struct for the state of GridSpaceIdIterator #936

Closed Tortar closed 8 months ago

Tortar commented 8 months ago

This reduces the allocations of the iterator.

As you can see on this Flocking benchmark inside the ABMFrameworkComparison repo the speed improvement is nice:

#before
julia> a = @benchmark run_model(rng, (150, 150), 400, 15.0) setup = (rng = rng_model()) evals=1 samples=n_run seconds=1e6
BenchmarkTools.Trial: 100 samples with 1 evaluation.
 Range (min … max):  37.952 ms … 61.669 ms  ┊ GC (min … max): 5.92% … 8.08%
 Time  (median):     47.160 ms              ┊ GC (median):    7.55%
 Time  (mean ± σ):   47.599 ms ±  4.318 ms  ┊ GC (mean ± σ):  7.46% ± 1.12%

                ▆   ▂▆  █▄ █  ▄ ▂                              
  ▄▄▁▁▄▁▁▁█▁▆▆▄██▆█▆██▆▄██▆█▆██▄██▄▆█▄▆▁▆▁▁▆█▁▄▁▆▁▁▁▁▁▁▁▁▁▁▁▄ ▄
  38 ms           Histogram: frequency by time          61 ms <

 Memory estimate: 98.61 MiB, allocs estimate: 2151950.

#after
julia> a = @benchmark run_model(rng, (150, 150), 400, 15.0) setup = (rng = rng_model()) evals=1 samples=n_run seconds=1e6
BenchmarkTools.Trial: 100 samples with 1 evaluation.
 Range (min … max):  32.827 ms … 52.396 ms  ┊ GC (min … max): 3.73% … 2.89%
 Time  (median):     40.189 ms              ┊ GC (median):    6.13%
 Time  (mean ± σ):   40.487 ms ±  3.500 ms  ┊ GC (mean ± σ):  6.12% ± 1.13%

                   █   ▄▆▁▆   ▁                                
  ▄▄▄▄▁▄▁▄▄▄▁▆▇▇▆▇▆█▇▇▇████▆▇▇█▆▆▄▆▄▇▁▄▇▄▆▄▄▁▄▁▁▁▄▁▁▁▁▁▁▁▁▁▁▄ ▄
  32.8 ms         Histogram: frequency by time          52 ms <

 Memory estimate: 67.06 MiB, allocs estimate: 2191950.

(using Ref.(....) to retrieve the fields can sometimes have a bad effect on inference, so I also changed that)

codecov-commenter commented 8 months ago

Codecov Report

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

Comparison is base (de82efd) 80.74% compared to head (1d722c2) 92.27%.

:exclamation: Current head 1d722c2 differs from pull request most recent head 4a4aae4. Consider uploading reports for the commit 4a4aae4 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #936 +/- ## =========================================== + Coverage 80.74% 92.27% +11.52% =========================================== Files 45 33 -12 Lines 2955 2277 -678 =========================================== - Hits 2386 2101 -285 + Misses 569 176 -393 ```

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

Tortar commented 8 months ago

made one :+1: