Skip to content
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

Optimize hit() and _SAT() #1147

Merged
merged 1 commit into from
Jul 6, 2017
Merged

Optimize hit() and _SAT() #1147

merged 1 commit into from
Jul 6, 2017

Conversation

starwed
Copy link
Member

@starwed starwed commented Jul 1, 2017

The main win here is reducing the number of allocations.

hit() was also doing an intermediate check between the broad-phase search and the _SAT collision test. I believe this increases the amount of work in the common case (and in checking performance in Firefox, the check was certainly using up about the same amount of CPU time as the _SAT check itself, so...)

  • Allow results object to be passed to hit()
  • Flatten the results of _SAT to reduce allocations a bit when hits are found.
  • Optimize hit a bit to reduce the amount of work done
    • Remove an intermediate overlap check; it's unnecessary when we're already using broad-phase + SAT
    • Assume that the collision component always has map
    • Return as early as possible
  • Updated the documentation to include the nx and ny properties.

var SAT = this._SAT(this.map, obj.map);
SAT.obj = obj;
SAT.type = "SAT";
if (SAT) finalresult.push(SAT);
Copy link
Member Author

@starwed starwed Jul 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: when _SAT returned false, this code was previously trying to set the obj and type properties on that value. This 'works' in javascript in the sense that it doesn't produce an error; instead, the boolean primitive is implicitly coerced to an object, the value is set on that object, and then the object is ignored (since it isn't assigned or referenced anywhere.) I think the intermediate object was probably getting optimized away (since I didn't see this code get flagged as a source of allocations) but this was at best very confusing. (Here is an article that describes what happens when you set properties on primitives.)

The main win here is reducing the number of allocations.
hit() was also doing an intermediate check between the broad-phase search and the _SAT collision test.
I believe this increases the amount of work in the common case

- Allow results object to be passed to hit()
- Flatten the results of _SAT to reduce allocations a bit when hits are found.
- Optimize hit a bit to reduce the amount of work done
 - Remove an intermediate overlap check; it's unnecessary when we're already using broad-phase + SAT
 - Assume that the collision component always has map
 - Return as early as possible
@starwed starwed merged commit 602340b into craftyjs:develop Jul 6, 2017
@starwed starwed deleted the better-hit branch July 6, 2017 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant