Skip to content

Commit

Permalink
fix v8 optimisation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 24, 2016
1 parent 422c1e4 commit 86c4a61
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/collision/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ var Common = require('../core/Common');
* @return {string} bucket id
*/
var _getBucketId = function(column, row) {
return column + ',' + row;
return 'C' + column + 'R' + row;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/collision/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ var Contact = require('./Contact');
*/
Pair.id = function(bodyA, bodyB) {
if (bodyA.id < bodyB.id) {
return bodyA.id + '_' + bodyB.id;
return 'A' + bodyA.id + 'B' + bodyB.id;
} else {
return bodyB.id + '_' + bodyA.id;
return 'A' + bodyB.id + 'B' + bodyA.id;
}
};

Expand Down
35 changes: 20 additions & 15 deletions src/collision/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ var Vector = require('../geometry/Vector');
overlapBA,
minOverlap,
collision,
prevCol = previousCollision,
canReusePrevCol = false;

if (prevCol) {
if (previousCollision) {
// estimate total motion
var parentA = bodyA.parent,
parentB = bodyB.parent,
Expand All @@ -40,20 +39,20 @@ var Vector = require('../geometry/Vector');

// we may be able to (partially) reuse collision result
// but only safe if collision was resting
canReusePrevCol = prevCol && prevCol.collided && motion < 0.2;
canReusePrevCol = previousCollision && previousCollision.collided && motion < 0.2;

// reuse collision object
collision = prevCol;
collision = previousCollision;
} else {
collision = { collided: false, bodyA: bodyA, bodyB: bodyB };
}

if (prevCol && canReusePrevCol) {
if (previousCollision && canReusePrevCol) {
// if we can reuse the collision result
// we only need to test the previously found axis
var axisBodyA = collision.axisBody,
axisBodyB = axisBodyA === bodyA ? bodyB : bodyA,
axes = [axisBodyA.axes[prevCol.axisNumber]];
axes = [axisBodyA.axes[previousCollision.axisNumber]];

minOverlap = _overlapAxes(axisBodyA.vertices, axisBodyB.vertices, axes);
collision.reused = true;
Expand Down Expand Up @@ -94,7 +93,6 @@ var Vector = require('../geometry/Vector');
collision.bodyA = bodyA.id < bodyB.id ? bodyA : bodyB;
collision.bodyB = bodyA.id < bodyB.id ? bodyB : bodyA;
collision.collided = true;
collision.normal = minOverlap.axis;
collision.depth = minOverlap.overlap;
collision.parentA = collision.bodyA.parent;
collision.parentB = collision.bodyB.parent;
Expand All @@ -103,20 +101,27 @@ var Vector = require('../geometry/Vector');
bodyB = collision.bodyB;

// ensure normal is facing away from bodyA
if (Vector.dot(collision.normal, Vector.sub(bodyB.position, bodyA.position)) > 0)
collision.normal = Vector.neg(collision.normal);
if (Vector.dot(minOverlap.axis, Vector.sub(bodyB.position, bodyA.position)) < 0) {
collision.normal = {
x: minOverlap.axis.x,
y: minOverlap.axis.y
};
} else {
collision.normal = {
x: -minOverlap.axis.x,
y: -minOverlap.axis.y
};
}

collision.tangent = Vector.perp(collision.normal);

collision.penetration = {
x: collision.normal.x * collision.depth,
y: collision.normal.y * collision.depth
};
collision.penetration = collision.penetration || {};
collision.penetration.x = collision.normal.x * collision.depth;
collision.penetration.y = collision.normal.y * collision.depth;

// find support points, there is always either exactly one or two
var verticesB = _findSupports(bodyA, bodyB, collision.normal),
supports = collision.supports || [];
supports.length = 0;
supports = [];

// find the supports from bodyB that are inside bodyA
if (Vertices.contains(bodyA.vertices, verticesB[0]))
Expand Down
25 changes: 14 additions & 11 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ module.exports = Common;
deepClone = true;
}

args = Array.prototype.slice.call(arguments, argsStart);

for (var i = 0; i < args.length; i++) {
var source = args[i];
for (var i = argsStart; i < arguments.length; i++) {
var source = arguments[i];

if (source) {
for (var prop in source) {
Expand Down Expand Up @@ -493,11 +491,10 @@ module.exports = Common;
* @return {function} A new function that calls the passed functions in order.
*/
Common.chain = function() {
var args = Array.prototype.slice.call(arguments),
funcs = [];
var funcs = [];

for (var i = 0; i < args.length; i += 1) {
var func = args[i];
for (var i = 0; i < arguments.length; i += 1) {
var func = arguments[i];

if (func._chained) {
// flatten already chained functions
Expand All @@ -508,10 +505,16 @@ module.exports = Common;
}

var chain = function() {
var lastResult;
// https://github.com/GoogleChrome/devtools-docs/issues/53#issuecomment-51941358
var lastResult,
args = new Array(arguments.length);

for (var i = 0, l = arguments.length; i < l; i++) {
args[i] = arguments[i];
}

for (var i = 0; i < funcs.length; i += 1) {
var result = funcs[i].apply(lastResult, arguments);
for (i = 0; i < funcs.length; i += 1) {
var result = funcs[i].apply(lastResult, args);

if (typeof result !== 'undefined') {
lastResult = result;
Expand Down

0 comments on commit 86c4a61

Please sign in to comment.