question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lags related to Grid.update

See original GitHub issue

Hello.

I have huge world and i experience strange behavior of engine. I need to to create many objects in scene (bullets) and also i have many objects (more than 600).

When i have many objects i experience lags when also add many objects to scene*. And when i have not so many objects (about 100), i experience lags when do not add another objects to a scene, and also when i do so, there is no more lags for some time. I have very big bucketWidth and bucketHeight both equals 700. And also i have positionIterations 12 and velocityIterations 8 but this make no difference to described lags.

* It’s temporary objects (bullets as i said). After some time they removed from world. I experience lags in time that i continuously add new objects, and all ok when i stop do so.

I was do some research and was investigate that it’s something related to Grid.update method and it’s forceUpdate mode. First of all Grid.update are not optimized in google chrome due to large amount of opt/deopt cycles. I can solve this issue by this little snippet:

if (forceUpdate)
            return Grid.updateForce(grid, bodies, engine, forceUpdate)

At the top of Grid.update method. Grid.updateForce is a simple copy-paste code from Grid.update. Google chrome can optimize this, and i propose to replace all occurences of Grid.update(grid, bodies, engine, true) (all force updates of grid) with Grid.updateForce But this is not solve the lags, just a little speed up.

I can suppose two improvements that can or cannot resolve this issue. I think that using grid for broadphase structure is inefficient and it must be replaced to something as AABB tree. And also i can propose to create somethings as World.addFast method that will add objects without forcing grid/tree rebuild.

Also when i turn on showBroadphase, i see that many grid regions are empty and because i see them in viewport, they exists in grid.buckets array. Maybe better remove empty buckets?

Can you comment something or explain the actual root of this issue?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
liabrucommented, Jun 26, 2016

The forceUpdate option is called when ever the world is modified (bodies added or removed) so if you’re doing that very often (i.e. bullets) with a large amount of total bodies then you may get stuttering because it is quite expensive.

The reason it is this way is because it was a tradeoff for keeping the overall design simple (it was one of the first modules I initially wrote). For it to be more efficient, the grid would need to know exactly what has been removed and then update buckets individually accordingly. This is possible but it would require additional modifications to Matter.Composite and Matter.Engine so it’s not straightforward.

I’ll label this one as an improve as I’d like to implement this eventually.

A temporary solution for for anyone with this problem: I think there is a way you can batch this update, at least for body removals.

Rather than remove the bodies from the world do this instead:

  1. add the body to a garbage array
  2. set the body to sleep
  3. set the body collisionFilter so it is not colliding with anything
  4. stop rendering the body

Then when ever the garbage array gets to say 500 bodies, remove them all from the world in one step and clear the garbage array. This should mean the amount of times the grid is rebuilt is 1/500 times less.

0reactions
liabrucommented, Dec 20, 2021

As of the latest release 0.18.0 the grid broadphase has been replaced, so I’ll close this one as it should no longer be an issue. If anybody notices issues with the latest release like this then please do open a new issue, thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Finish line lag after new update - GRID Legends
I just came here to report the same thing so I'm 'pleased' I'm not alone. A quick pause/stutter when crossing the finish line...
Read more >
Performance is extremely slow when using show grids in ...
It turns out the problem is fixed only partially in 26.3.1, only for some users, and the performance remains slow when grid is...
Read more >
Grid control slow to update - DevExpress Support
Hello, We are using a grid control in our application that is very slow to update. It is quite a large grid, but...
Read more >
First grid update operation is very slow in UI for ASP.NET AJAX ...
Our issue is that the very first grid database update operation (insert, change, delete) is very slow (in the order of 10 to...
Read more >
c# Very slow Gridview update. - asp.net - Stack Overflow
1 Answer 1 · 1) Opening SQL connection thrice (updatetrigger, rowdatabound and UpdateMilesStones). · 2) Use using block for SQL Connection check ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found