LifeEngine icon indicating copy to clipboard operation
LifeEngine copied to clipboard

New timing code

Open joonazan opened this issue 3 years ago • 6 comments

#34

joonazan avatar Dec 03 '21 12:12 joonazan

Looks like you are running off of the develop branch. Can you either rebase or wait for the develop changes to land?

xkortex avatar Dec 03 '21 22:12 xkortex

Agree with @xkortex you should rebase it from develop and set the pr to merge to master, there are updates to the code there. Edit: nevermind, I just needed to change the base branch.

MaxRobinsonTheGreat avatar Dec 04 '21 05:12 MaxRobinsonTheGreat

Just checked it out, I'm seeing the same issue that I had on my branch, it seems to slow down much more significantly than the current version with setInterval.

I don't currently have a super consistent benchmark for this, but on my machine it lags to ~15 fps with just a few hundred organisms, much worse than current version. Same problem on my branch.

And this is with the console.log removed

MaxRobinsonTheGreat avatar Dec 04 '21 06:12 MaxRobinsonTheGreat

Ah, I accidentally committed the console.log.

I did not notice the same slowdown. It could be related to Chrome or different hardware, like for example if rendering is slower for you.

One thing that could improve overall performance would be putting the simulation in a web worker.

joonazan avatar Dec 04 '21 10:12 joonazan

I think it's in how it's scheduled.
setInterval with a 10ms frame interval will work like the following (100fps) code-runtime: [ 5ms ]-----[ 5ms ]-----[ 12ms ------- ][ 5ms ] -------interrupt:|-------------|-------------|------------------| <-- will wait until previous exec is done then interrupt immediately, may catch back up if next frame is < 8ms

the way it looks like the setTimeout in the code is working will produce the following, setTimeout will set a delay at the end of the simulation runtime of 1000/fps in this case 10ms. offset by the sim time from the beginning of the frame will result in always getting a delay between the end of the last frame and the beginning of the next (read more dead time). code-runtime: [ 5ms ]---------[ 5ms ]---------[ 12ms ------- ]---------[ 5ms ] -------interrupt:|-----------------|------------------|---------------------------| ---setTimeout{10ms}:|-----------------|--------------------------|------------------|

Not going to improve exec through timer manipulation I think, however rendering and simulating in parallel could be a marked improvement (see joonazan above). For more timing analysis see pull request #68.

lancelot2112 avatar Dec 04 '21 19:12 lancelot2112

if you move the "setTimeout" code to the beginning of the updateSimulation() call it will replicate the "setInterval" behavior.

updateSimulation() {
   setTimeout(func,1000/fps); //starts counting from start of exec instead of after simulation runs (less dead time)
   simulation.update();
}

lancelot2112 avatar Dec 04 '21 23:12 lancelot2112