Skip to content

Commit

Permalink
using Pool for islands and nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
schteppe committed Apr 6, 2015
1 parent 01c3581 commit 3437134
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
31 changes: 31 additions & 0 deletions src/utils/IslandNodePool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var IslandNode = require('../world/IslandNode');
var Pool = require('./Pool');

module.exports = IslandNodePool;

/**
* @class
*/
function IslandNodePool() {
Pool.apply(this, arguments);
}
IslandNodePool.prototype = new Pool();
IslandNodePool.prototype.constructor = IslandNodePool;

/**
* @method create
* @return {IslandNode}
*/
IslandNodePool.prototype.create = function () {
return new IslandNode();
};

/**
* @method destroy
* @param {IslandNode} node
* @return {IslandNodePool}
*/
IslandNodePool.prototype.destroy = function (node) {
node.reset();
return this;
};
31 changes: 31 additions & 0 deletions src/utils/IslandPool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var Island = require('../world/Island');
var Pool = require('./Pool');

module.exports = IslandPool;

/**
* @class
*/
function IslandPool() {
Pool.apply(this, arguments);
}
IslandPool.prototype = new Pool();
IslandPool.prototype.constructor = IslandPool;

/**
* @method create
* @return {Island}
*/
IslandPool.prototype.create = function () {
return new Island();
};

/**
* @method destroy
* @param {Island} island
* @return {IslandPool}
*/
IslandPool.prototype.destroy = function (island) {
island.reset();
return this;
};
36 changes: 20 additions & 16 deletions src/world/IslandManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var vec2 = require('../math/vec2')
, Island = require('./Island')
, IslandNode = require('./IslandNode')
, IslandNodePool = require('./../utils/IslandNodePool')
, IslandPool = require('./../utils/IslandPool')
, Body = require('../objects/Body');

module.exports = IslandManager;
Expand All @@ -16,8 +18,8 @@ module.exports = IslandManager;
function IslandManager(options){

// Pooling of node objects saves some GC load
this._nodePool = [];
this._islandPool = [];
this._nodePool = new IslandNodePool({ size: 16 });
this._islandPool = new IslandPool({ size: 8 });

/**
* The equations to split. Manually fill this array before running .split().
Expand Down Expand Up @@ -132,19 +134,22 @@ IslandManager.prototype.split = function(world){

// Move old nodes to the node pool
while(nodes.length){
this._nodePool.push(nodes.pop());
this._nodePool.release(nodes.pop());
}

// Create needed nodes, reuse if possible
for(var i=0; i!==bodies.length; i++){
if(this._nodePool.length){
var node = this._nodePool.pop();
node.reset();
node.body = bodies[i];
nodes.push(node);
} else {
nodes.push(new IslandNode(bodies[i]));
}
var node = this._nodePool.get();
node.body = bodies[i];
nodes.push(node);
// if(this._nodePool.length){
// var node = this._nodePool.pop();
// node.reset();
// node.body = bodies[i];
// nodes.push(node);
// } else {
// nodes.push(new IslandNode(bodies[i]));
// }
}

// Add connectivity data. Each equation connects 2 bodies.
Expand All @@ -162,18 +167,17 @@ IslandManager.prototype.split = function(world){

// Move old islands to the island pool
var islands = this.islands;
while(islands.length){
var island = islands.pop();
island.reset();
this._islandPool.push(island);
for(var i=0; i<islands.length; i++){
this._islandPool.release(islands[i]);
}
islands.length = 0;

// Get islands
var child;
while((child = IslandManager.getUnvisitedNode(nodes))){

// Create new island
var island = this._islandPool.length ? this._islandPool.pop() : new Island();
var island = this._islandPool.get();

// Get all equations and bodies in this island
this.bfs(child, island.bodies, island.equations);
Expand Down

0 comments on commit 3437134

Please sign in to comment.