-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Aglorithm optimization of Pairs.update method #522
Conversation
looks good 👍 |
So glad you did this! I noticed today in a simulation with 1000 bodies that |
@photonstorm , looking at your benchmark, w.r.t. some tests I made you could get an extra 10% boost without controversial changes to the engines. For instances, I imagine that the method Is the source code of your demo available online? so I can benchmark future optimizations? |
@bchevalier Sort of, the demo I was testing was this one, but you'll notice it's wrapped in Phaser 3 code. Even so, the actual matter.js parts of it are quite tiny really, so easy to re-create. You're welcome to just throw ideas at me and I'll implement locally and re-test. http://labs.phaser.io/view.html?src=src\physics\matterjs\drag%20through%20balls.js |
Cool, if you wish you can simply test my PRs, I just suggested an improvement of the |
Excellent work on these changes @bchevalier! It will take me some time to review them all since they are pretty core changes but things are looking good from my initial testing! |
@liabru, thanks for having a look! |
Hey @liabru, I was wondering if you had any feedback on those PRs, any blocker? |
Hey sorry for the delay on these guys, busy December (and I needed a little break). I still need to take a look at your new batch. Thanks for all the great contributions! |
I've had a closer look at this, there's an issue here. The Matter.Events.on(engine, 'collisionEnd', function(event) {
console.log(event.pairs.length, engine.timing.timestamp);
}); I think the fix is this on line 97:
Could you try this out and update the pull request? Otherwise I'll apply the fix. Thanks! |
Hey, thanks for the feedback, I will probably be addressing all the issues in 2 weeks' time. |
Fixed, seems to work properly now. |
Woohoo! Merged at last :) 🕺 |
Changes
Added a boolean on the
Pair
object that saves whether a pair has been confirmed as active at the current iteration.The idea was to reduce the complexity of the second loop of the function from
O(n*m)
toO(n)
. Wheren
is the number of pairs andm
the number of active pairs.Result
Profiling on a specific test case indicated that the
indexOf
method was using about 13% of CPU resources used by thematterjs
:We can notice that the
Pairs.update
method is consuming 27% of the total.After the optimization the profiling looks like this:
Notice that the
indexOf
method disappeared and thePairs.update
method is now consuming only 16% of the total.Overall the engine is ~13% faster on that particular test case (about 50 bodies). The benefit should get bigger as the number of bodies in the world increase.