-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Evaluate empties set more lazily #1546
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
Conversation
Codecov ReportBase: 81.44% // Head: 81.42% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #1546 +/- ##
==========================================
- Coverage 81.44% 81.42% -0.03%
==========================================
Files 16 16
Lines 1326 1335 +9
Branches 230 233 +3
==========================================
+ Hits 1080 1087 +7
- Misses 203 204 +1
- Partials 43 44 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
|
Tests are failing due to the move of examples folder to another repo, is there a way to use them though they are in another repo? |
|
we need actually to create a flag instead of using _empties in itself to check if it was built, because
otherwise exists_empties could become very very slow when _empties was built
but became empty
Il Mar 6 Dic 2022, 10:13 rht ***@***.***> ha scritto:
… ***@***.**** commented on this pull request.
------------------------------
In mesa/space.py
<#1546 (comment)>:
> @@ -488,7 +504,7 @@ def move_to_empty(
if self.is_cell_empty(new_pos):
break
else:
- new_pos = agent.random.choice(sorted(self.empties))
+ new_pos = agent.random.choice(sorted(self._empties))
self.empties is for public consumption. The inner workings might change,
so using self.empties is more robust. Again, if the performance benefit
is marginal, it's not worth it.
—
Reply to this email directly, view it on GitHub
<#1546 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQH6VX4WZKYAV7UGCEIRZLDWL37UPANCNFSM6AAAAAASUY5YFM>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
|
ok, everything should be hopefully done |
|
|
|
no it's not true, say the user built |
|
wait but at the same time, it solves anyway the problem that can happen if the user instead of using self.exists_empty_cells uses len(self.empties) > 0, which can happen. Or more generally, whenever a read operation is done on |
|
Also using self._empties and not self.empties in exists_empty_cells would result in a bug if exists_empty_cells was called before the creation of self._empties through self.empties, because it would be an empty set edit: I wrongly removed a comment where I agreed with you before these considerations so the structure of my comments make less sense now, but anyway I now think using the flag is much better in terms of user experience with the library |
|
I see, then it sounds good to me. Need to be documented, though. One option is to initialize |
|
Added an explaination, feel free to modify/extend it if you think it can be improved :-) |
|
I saw from my inbox that you measured a 25% speedup in grid construction. Can't find it on GH. Did you remove it? |
|
yes, because I realized that it's a 1 time operation per model in the end (but anyway it was not a 25% speedup but a 4 times speed up for a 1000 x 1000 grid). I think the real perf benefits are 1) Less memory usage if empties is not built and 2) Something like (just guessing) ~15-25% speedup for place_agent, remove_agent, move_agent for Grid, SingleGrid and MultiGrid if empties is not built. |
|
These are significant improvements, and need to be communicated properly to the users. I think we should measure just one of the examples to see how much holistic speedup we get to running a simulation (without GUI), for a concrete illustration. |
|
After I run on a jupyter notebook: Results:
|
|
What about sugarscape_cg? See if it is also ~10%. |
|
No it's not, it seems much smaller. There are many more operations in this model so it makes sense. Anyway it's more like ~20% in BoltzmannWealthModel, but it's one of the best models to track the perf of this one because move_agent() is one of the main operations there. |
|
OK, so we can at least say that
is more precise, because this statement is more localized, and doesn't depend on the rest of the code. |
|
yes, I agree that saying ~15-25% speedup for place_agent, remove_agent, move_agent is more informative 👍 |
|
Though we need actual hard numbers instead of speculation of the speedup amount. |
|
i made a little script to check all cases: This gives So we arrive at 2.5x in multigrid case for removal! (Because the checking condition is simpler and the list cell has only one item inside). Another thing to notice is that place_agent in singlegrid is impacted less than the one in grid because of the super() call, I'm for the removal of it in another PR, I don't think it's more mantainable in this simple case. |
|
I didn't follow the complete conversation and hope there is nothing blocking, but I think the general approach seems quite clever @Tortar ! |
|
@rht We have those hard numbers now on the speed-up👍 |
|
OK, I'm merging. I will defer summarizing those results to later when writing up the release note. |
This implementation is better than the one in #1543 , it builds the empties set only if it is required to do so with no breaking change.