Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve speed of random_agent #897

Merged
merged 3 commits into from
Oct 5, 2023
Merged

Improve speed of random_agent #897

merged 3 commits into from
Oct 5, 2023

Conversation

Tortar
Copy link
Member

@Tortar Tortar commented Oct 5, 2023

It was actually very slow before, consider this silly model I was using to benchmark a different thing:

using Agents, Random, BenchmarkTools

function init_model()
	model = StandardABM(GridAgent{2}, GridSpace((50, 50)), rng=Xoshiro(42))
	for _ in 1:1000
		add_agent!(model)
	end
	return model
end

function model_step!(model)
	add_agent!(model)
	remove_agent!(random_agent(model), model)
end

model = init_model()
@benchmark step!(model, dummystep, model_step!, 10^5)

before:

julia> @benchmark step!(model, dummystep, model_step!, 10^5)
BenchmarkTools.Trial: 27 samples with 1 evaluation.
 Range (min  max):  183.797 ms  194.004 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     187.722 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   188.308 ms ±   2.707 ms  ┊ GC (mean ± σ):  0.13% ± 0.37%

                 ▃ █     ▃
  ▇▁▁▇▁▁▁▁▁▇▁▇▁▁▇█▁█▇▁▇▇▁█▁▇▁▁▁▁▇▇▁▇▇▇▁▇▁▁▁▁▇▁▁▁▁▁▁▁▁▇▁▁▁▁▇▇▁▁▇ ▁
  184 ms           Histogram: frequency by time          194 ms <

 Memory estimate: 5.19 MiB, allocs estimate: 100169.

after:

julia> @benchmark step!(model, dummystep, model_step!, 10^5)
BenchmarkTools.Trial: 306 samples with 1 evaluation.
 Range (min  max):  15.251 ms  26.520 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     15.822 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   16.360 ms ±  1.440 ms  ┊ GC (mean ± σ):  1.50% ± 3.67%

    ▄█▆▁
  ▃▅████▇▆▃▃▄▅▄▄▃▃▁▃▃▃▃▄▃▃▃▃▃▂▂▂▃▂▃▁▁▂▁▁▁▁▁▃▂▁▁▁▁▂▁▂▁▁▁▁▁▂▂▁▂ ▃
  15.3 ms         Histogram: frequency by time        21.7 ms <

 Memory estimate: 5.19 MiB, allocs estimate: 100162.

A micro-benchmark tells that now random_agent for a StandardABM is 40x faster!

I exported a new function random_id in the process, which is in line to the other random functions which export also the id version

@Tortar Tortar requested a review from Datseris October 5, 2023 19:25
@Tortar
Copy link
Member Author

Tortar commented Oct 5, 2023

I have to say that this means that probably there is something to improve in Base, since that the previous method is so slow sounds strange

@codecov-commenter
Copy link

codecov-commenter commented Oct 5, 2023

Codecov Report

Merging #897 (737312d) into main (a71cc12) will increase coverage by 12.09%.
The diff coverage is 60.00%.

❗ Current head 737312d differs from pull request most recent head 90a22d6. Consider uploading reports for the commit 90a22d6 to get more accurate results

@@             Coverage Diff             @@
##             main     #897       +/-   ##
===========================================
+ Coverage   80.27%   92.37%   +12.09%     
===========================================
  Files          44       32       -12     
  Lines        3027     2334      -693     
===========================================
- Hits         2430     2156      -274     
+ Misses        597      178      -419     
Files Coverage Δ
src/core/model_abstract.jl 89.06% <66.66%> (-0.03%) ⬇️
src/core/model_concrete.jl 88.23% <50.00%> (-1.18%) ⬇️

... and 18 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@Tortar
Copy link
Member Author

Tortar commented Oct 5, 2023

Actually it was a O(N) operation while this one is not, this means that the improvement in speed is arbitrary large!

Copy link
Member

@Datseris Datseris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is a bug in base. It is probably an inferrence issue since the agent container is complicated. The function barrier makes the inference work.

DOn't forge tto add changelog entry and then merge!

@Tortar
Copy link
Member Author

Tortar commented Oct 5, 2023

I think it should be JuliaLang/julia#51605 , let's see :-)

@Tortar Tortar merged commit eed9f0d into main Oct 5, 2023
5 checks passed
@Tortar Tortar deleted the random_agent branch October 5, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants