Skip to content

Commit e25f420

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents 47bee69 + f57ab02 commit e25f420

3 files changed

Lines changed: 25 additions & 45 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Impact Physics Game Objects have changed `setPassive` to `setPassiveCollision`.
1515
* Impact Physics Game Objects have changed `setFixed` to `setFixedCollision`.
1616
* Impact Physics Game Objects have changed `setActive` to `setActiveCollision`, previously the `setActive` collision method was overwriting the Game Objects `setActive` method, hence the renaming.
17+
* The modifications made to the RTree class in Phaser 3.4.0 to avoid CSP policy violations caused a significant performance hit once a substantial number of bodies were involved. We have recoded how the class deals with its accessor formats and returned to 3.3 level performance while still maintaining CSP policy adherence. Fix #3594 (thanks @16patsle)
1718

1819
### Bug Fixes
1920

src/physics/arcade/World.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ var World = new Class({
293293
* @type {Phaser.Structs.RTree}
294294
* @since 3.0.0
295295
*/
296-
this.tree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]);
296+
this.tree = new RTree(this.maxEntries);
297297

298298
/**
299299
* [description]
@@ -302,7 +302,7 @@ var World = new Class({
302302
* @type {Phaser.Structs.RTree}
303303
* @since 3.0.0
304304
*/
305-
this.staticTree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]);
305+
this.staticTree = new RTree(this.maxEntries);
306306

307307
/**
308308
* [description]

src/structs/RTree.js

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ var quickselect = require('../utils/array/QuickSelect');
1414
* Spatial index is a special data structure for points and rectangles that allows you to perform queries like
1515
* "all items within this bounding box" very efficiently (e.g. hundreds of times faster than looping over all items).
1616
*
17+
* This version of RBush uses a fixed min/max accessor structure of `[ '.left', '.top', '.right', '.bottom' ]`.
18+
* This is to avoid the eval like function creation that the original library used, which caused CSP policy violations.
19+
*
1720
* @class RTree
1821
* @memberOf Phaser.Structs
1922
* @constructor
2023
* @since 3.0.0
2124
*/
2225

23-
function rbush (maxEntries, format)
26+
function rbush (maxEntries)
2427
{
28+
var format = [ '.left', '.top', '.right', '.bottom' ];
29+
2530
if (!(this instanceof rbush)) return new rbush(maxEntries, format);
2631

2732
// max entries in a node is 9 by default; min node fill is 40% for best performance
2833
this._maxEntries = Math.max(4, maxEntries || 9);
2934
this._minEntries = Math.max(2, Math.ceil(this._maxEntries * 0.4));
3035

31-
if (format)
32-
{
33-
this._initFormat(format);
34-
}
35-
3636
this.clear();
3737
}
3838

@@ -463,44 +463,23 @@ rbush.prototype = {
463463
}
464464
},
465465

466-
_initFormat: function (format)
466+
compareMinX: function (a, b)
467467
{
468-
// format: [minX, minY, maxX, maxY accessors]
469-
// accessors will be dotted names
470-
471-
// Because we have historically used eval-based function constructor
472-
// the format accerrsors need to have their leading dots stripped to
473-
// obtain the actual accessor
474-
format = format.map(
475-
function (f)
476-
{
477-
return f.substring(1);
478-
}
479-
);
480-
481-
// Do not use string-generated Functions for CSP policies
482-
// Instead a combination of anonymous functions and grabbing properties
483-
// by string is used.
484-
// cf. https://github.com/photonstorm/phaser/issues/3441
485-
// and https://github.com/photonstorm/phaser/issues/3535
486-
487-
var mkCompareFn = function(attr) {
488-
return function(a, b) {
489-
return a[attr] - b[attr];
490-
};
491-
};
468+
return a.left - b.left;
469+
},
470+
471+
compareMinY: function (a, b)
472+
{
473+
return a.top - b.top;
474+
},
492475

493-
this.compareMinX = mkCompareFn(format[0]);
494-
this.compareMinY = mkCompareFn(format[1]);
495-
496-
this.toBBox = function(a)
497-
{
498-
return {
499-
minX: a[format[0]],
500-
minY: a[format[1]],
501-
maxX: a[format[2]],
502-
maxY: a[format[3]],
503-
};
476+
toBBox: function (a)
477+
{
478+
return {
479+
minX: a.left,
480+
minY: a.top,
481+
maxX: a.right,
482+
maxY: a.bottom
504483
};
505484
}
506485
};
@@ -621,4 +600,4 @@ function multiSelect (arr, left, right, n, compare)
621600
}
622601
}
623602

624-
module.exports = rbush;
603+
module.exports = rbush;

0 commit comments

Comments
 (0)