Skip to content

Commit

Permalink
Add array pool for sorted components (#6718)
Browse files Browse the repository at this point in the history
* add pool

* move sort to module
  • Loading branch information
LeXXik authored Jun 19, 2024
1 parent 1d09168 commit eba0668
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
17 changes: 0 additions & 17 deletions src/core/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,10 @@
*/
const cmpPriority = (a, b) => a.priority - b.priority;


/**
* @param {{order: number}} a - First object with `order` property.
* @param {{order: number}} b - Second object with `order` property.
* @returns {number} A number indicating the relative position.
* @ignore
*/
const cmpStaticOrder = (a, b) => a.constructor.order - b.constructor.order;

/**
* @param {Array<{priority: number}>} arr - Array to be sorted in place where each element contains
* an object with at least a priority property.
* @returns {Array<{priority: number}>} In place sorted array.
* @ignore
*/
export const sortPriority = arr => arr.sort(cmpPriority);

/**
* @param {Array<{order: number}>} arr - Array to be sorted in place where each element contains
* an object with a static `order` property.
* @returns {Array<{order: number}>} In place sorted array.
* @ignore
*/
export const sortStaticOrder = arr => arr.sort(cmpStaticOrder);
53 changes: 42 additions & 11 deletions src/framework/entity.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
import { Debug } from '../core/debug.js';
import { guid } from '../core/guid.js';
import { sortStaticOrder } from '../core/sort.js';

import { GraphNode } from '../scene/graph-node.js';

import { getApplication } from './globals.js';

/**
* @typedef {import('./components/component.js').Component} Component
*/

/**
* @param {Component} a - First object with `order` property.
* @param {Component} b - Second object with `order` property.
* @returns {number} A number indicating the relative position.
* @ignore
*/
const cmpStaticOrder = (a, b) => a.constructor.order - b.constructor.order;

/**
* @param {Array<Component>} arr - Array to be sorted in place where each element contains
* an object with a static `order` property.
* @returns {Array<Component>} In place sorted array.
* @ignore
*/
const sortStaticOrder = arr => arr.sort(cmpStaticOrder);

/**
* @type {GraphNode[]}
* @ignore
*/
const _enableList = [];

/**
* @type {Array<import('./components/component.js').Component>}
* @type {Array<Array<Component>>}
* @ignore
*/
const tmpPool = [];

const getTempArray = () => {
return tmpPool.pop() ?? [];
};

/**
* @param {Array<Component>} a - Array to return back to pool.
* @ignore
*/
const _sortedArray = [];
const releaseTempArray = (a) => {
a.length = 0;
tmpPool.push(a);
};

/**
* The Entity is the core primitive of a PlayCanvas game. Generally speaking an object in your game
Expand Down Expand Up @@ -522,7 +554,7 @@ class Entity extends GraphNode {
}
}

components.length = 0;
releaseTempArray(components);
}

/** @private */
Expand All @@ -533,7 +565,7 @@ class Entity extends GraphNode {
components[i].onPostStateChange();
}

components.length = 0;
releaseTempArray(components);
}

/**
Expand Down Expand Up @@ -609,23 +641,22 @@ class Entity extends GraphNode {
}

_getSortedComponents() {
Debug.assert(_sortedArray.length === 0);

const components = this.c;
const sortedArray = getTempArray();
let needSort = 0;
for (const type in components) {
if (components.hasOwnProperty(type)) {
const component = components[type];
needSort |= component.constructor.order !== 0;
_sortedArray.push(component);
sortedArray.push(component);
}
}

if (needSort && _sortedArray.length > 1) {
sortStaticOrder(_sortedArray);
if (needSort && sortedArray.length > 1) {
sortStaticOrder(sortedArray);
}

return _sortedArray;
return sortedArray;
}

/**
Expand Down

0 comments on commit eba0668

Please sign in to comment.