wybo / agentbase

Minimalist Agent Based Modeling (ABM) platform that runs directly in the browser
GNU General Public License v3.0
58 stars 5 forks source link

Browser hangs for large models #2

Open ToonTalk opened 10 years ago

ToonTalk commented 10 years ago

When I edit the Template Model to have 25000 agents it took over a minute to start and during that time the browser tab was unresponsive. (5000 agents was OK.) I got a couple browser messages asking if I wanted to kill the process.

I think appropriate use of setTimeout with a second argument of 0 milliseconds could resolve this by letting the browser get some cycles sometimes.

wybo commented 10 years ago

I googled for a bit, but it does not seem directly clear to me how setTimeout would affect browser timeouts? Could you maybe point me to some description of this, or create a demo model which shows how it could?

ToonTalk commented 10 years ago

It seems that a zero timeout has a 4 millisecond overhead these days. https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.setTimeout#Notes

The same page refers to a way to get a zero timeout: http://dbaron.org/log/20100309-faster-timeouts

But even with a zero timeout it often requires the code to be rewritten.

For example the implementation of @agents.create might need to repeat

  1. create 5000 agents
  2. zero timeout

repeated until the desired number of agents are created.

Step is even more of a problem to break up especially since it is user-defined.

If there was a way to implement "for agent in @agents ..." such that it does 1000 agents and then timeout until all agents have run that might be a solution. But the body of the for loop might be quick or not, so not clear how many to do at once.

Perhaps this problem is just too hard to fix given the other constraints.

Possibly workers is another solution where the model (but not the animation or interface) is in a web worker: https://developer.mozilla.org/en/docs/Web/Guide/Performance/Using_web_workers

wybo commented 10 years ago

Thanks for giving some more background. It indeed seems to be a relatively hard problem to fix.

This, plus the fact that that:

...makes this a low priority issue for now.