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

Add array pool for sorted components #6718

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
LeXXik marked this conversation as resolved.
Show resolved Hide resolved
}

/** @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