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

Performance: Object sleep API #191

Open
parasyte opened this Issue Apr 5, 2013 · 8 comments

Comments

2 participants
@parasyte
Member

parasyte commented Apr 5, 2013

Following discussion on the forum, it sounds like a good idea to implement an object sleep mechanism to save CPU cycles for objects that have nothing to do.

The way I imagine it is having two pools for game objects; the gameObjects array in core.js, and a new array for sleepingObjects. Calling me.game.sleep(obj) will remove obj from gameObjects, and push it into sleepingObjects. And me.game.wakeup(obj) will do the opposite.

A sleeping object will never be iterated in the update or draw loops, saving precious CPU time.

Further, we can enable a flag like me.sys.enableAutoSleep to put objects to sleep when they leave the viewport. Deciding on a good method of waking up a sleeping object without polling will be crucial for this flag to be useful. I suppose worst-case, polling infrequently (once per second?) could work.

@obiot

This comment has been minimized.

Show comment
Hide comment
@obiot

obiot Apr 5, 2013

Member

I didn't really had the time to check the forum since yesterday since I was travelling, but this seems to be a good idea !

Not sure though however about the automatic feature, as both putting them to sleep or awake automatically would effectively could be quite CPU consuming .

Member

obiot commented Apr 5, 2013

I didn't really had the time to check the forum since yesterday since I was travelling, but this seems to be a good idea !

Not sure though however about the automatic feature, as both putting them to sleep or awake automatically would effectively could be quite CPU consuming .

@obiot

This comment has been minimized.

Show comment
Hide comment
@obiot

obiot Apr 5, 2013

Member

@parasyte though it's not really related, I also wanted to look at replacing the current object array by a hash table (using the object GUID for key), as it should improve performances as well when parsing and accessing the object pool/list

Member

obiot commented Apr 5, 2013

@parasyte though it's not really related, I also wanted to look at replacing the current object array by a hash table (using the object GUID for key), as it should improve performances as well when parsing and accessing the object pool/list

@obiot

This comment has been minimized.

Show comment
Hide comment
@obiot

obiot Apr 5, 2013

Member

Improvements when doing lookup : http://jsperf.com/indexof-vs-array-hash

Member

obiot commented Apr 5, 2013

Improvements when doing lookup : http://jsperf.com/indexof-vs-array-hash

@parasyte

This comment has been minimized.

Show comment
Hide comment
@parasyte

parasyte Apr 5, 2013

Member

I had the same idea. See also a similar test in #192 that shows how faking a remove operation is faster than both Array.pop() and Object.delete().

Member

parasyte commented Apr 5, 2013

I had the same idea. See also a similar test in #192 that shows how faking a remove operation is faster than both Array.pop() and Object.delete().

@parasyte

This comment has been minimized.

Show comment
Hide comment
@parasyte

parasyte Apr 5, 2013

Member

@obiot: auto-sleep is easy. Just plug it in right here: https://github.com/melonjs/melonJS/blob/master/src/core.js#L1422 But auto-wakeup is really hard.

Member

parasyte commented Apr 5, 2013

@obiot: auto-sleep is easy. Just plug it in right here: https://github.com/melonjs/melonJS/blob/master/src/core.js#L1422 But auto-wakeup is really hard.

@obiot

This comment has been minimized.

Show comment
Hide comment
@obiot

obiot Apr 5, 2013

Member

Yes I was actually more concerned about the auto-wake up :)

Member

obiot commented Apr 5, 2013

Yes I was actually more concerned about the auto-wake up :)

@parasyte

This comment has been minimized.

Show comment
Hide comment
@parasyte

parasyte Apr 26, 2013

Member

Reminder: Implementing auto-sleep through callbacks is the right way to do this. A "viewport leave" callback is very easy, as we know. With the new collision detection in #103, we will have a spatial grid that already contains objects... Seems like this can be reused for auto-wakeup, too! When a new spatial grid cell enters the viewport, fire the "wakeup" callback on all objects in that cell.

Wow ... that seems almost too easy to be true!

Member

parasyte commented Apr 26, 2013

Reminder: Implementing auto-sleep through callbacks is the right way to do this. A "viewport leave" callback is very easy, as we know. With the new collision detection in #103, we will have a spatial grid that already contains objects... Seems like this can be reused for auto-wakeup, too! When a new spatial grid cell enters the viewport, fire the "wakeup" callback on all objects in that cell.

Wow ... that seems almost too easy to be true!

@obiot

This comment has been minimized.

Show comment
Hide comment
@obiot

obiot Mar 6, 2015

Member

something interesting i just spotted in the chipmunk API :
http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpSpace-Sleeping

it's also possible to disable object collision check for idle object, and specify a sleepTimeThreshold, might be also a nice addition if we do add this sleeping API.

Member

obiot commented Mar 6, 2015

something interesting i just spotted in the chipmunk API :
http://chipmunk-physics.net/release/ChipmunkLatest-Docs/#cpSpace-Sleeping

it's also possible to disable object collision check for idle object, and specify a sleepTimeThreshold, might be also a nice addition if we do add this sleeping API.

@obiot obiot added this to To Do (Core) in Roadmap Dec 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment