pool池优化性能

This commit is contained in:
YHH
2022-03-07 22:52:51 +08:00
parent e207952786
commit 131df181e6
10 changed files with 120 additions and 131 deletions

View File

@@ -94,7 +94,7 @@ declare module es {
*/ */
onSceneChanged(): void; onSceneChanged(): void;
protected initialize(): void; protected initialize(): void;
protected update(currentTime?: number): Promise<void>; protected update(currentTime?: number): void;
} }
} }
declare module es { declare module es {
@@ -4877,7 +4877,7 @@ declare module es {
* 将项推回堆栈 * 将项推回堆栈
* @param obj * @param obj
*/ */
static free<T>(type: new (...args: any[]) => T, obj: Array<T>): void; static free<T>(type: new (...args: any[]) => T, obj: T[]): void;
private static checkCreate; private static checkCreate;
} }
} }

View File

@@ -1,39 +1,4 @@
"use strict"; "use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) { var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator]; var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o; if (!m) return o;
@@ -74,6 +39,33 @@ var __values = (this && this.__values) || function (o) {
} }
}; };
}; };
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var es; var es;
(function (es) { (function (es) {
/** /**
@@ -224,31 +216,25 @@ var es;
}; };
Core.prototype.update = function (currentTime) { Core.prototype.update = function (currentTime) {
if (currentTime === void 0) { currentTime = -1; } if (currentTime === void 0) { currentTime = -1; }
return __awaiter(this, void 0, void 0, function () { if (Core.paused) {
var i; return;
return __generator(this, function (_a) { }
if (Core.paused) { es.Time.update(currentTime, currentTime != -1);
return [2 /*return*/]; if (this._scene != null) {
} for (var i = this._globalManagers.length - 1; i >= 0; i--) {
es.Time.update(currentTime, currentTime != -1); if (this._globalManagers[i].enabled)
if (this._scene != null) { this._globalManagers[i].update();
for (i = this._globalManagers.length - 1; i >= 0; i--) { }
if (this._globalManagers[i].enabled) this._scene.update();
this._globalManagers[i].update(); if (this._nextScene != null) {
} this._scene.end();
this._scene.update(); this._scene = this._nextScene;
if (this._nextScene != null) { this._nextScene = null;
this._scene.end(); this.onSceneChanged();
this._scene = this._nextScene; this._scene.begin();
this._nextScene = null; }
this.onSceneChanged(); }
this._scene.begin(); this.startDebugDraw();
}
}
this.startDebugDraw();
return [2 /*return*/];
});
});
}; };
Core.paused = false; Core.paused = false;
/** /**
@@ -8551,33 +8537,34 @@ var es;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
var resultCounter = 0; var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask); var potentials = this.aabbBroadphase(bounds, null, layerMask);
for (var i = 0; i < potentials.length; i++) { if (potentials.length > 0)
var collider = potentials[i]; for (var i = 0; i < potentials.length; i++) {
if (collider instanceof es.BoxCollider) { var collider = potentials[i];
if (collider.shape.overlaps(this._overlapTestCircle)) { if (collider instanceof es.BoxCollider) {
results[resultCounter] = collider; if (collider.shape.overlaps(this._overlapTestCircle)) {
resultCounter++; results[resultCounter] = collider;
resultCounter++;
}
} }
} else if (collider instanceof es.CircleCollider) {
else if (collider instanceof es.CircleCollider) { if (collider.shape.overlaps(this._overlapTestCircle)) {
if (collider.shape.overlaps(this._overlapTestCircle)) { results[resultCounter] = collider;
results[resultCounter] = collider; resultCounter++;
resultCounter++; }
} }
} else if (collider instanceof es.PolygonCollider) {
else if (collider instanceof es.PolygonCollider) { if (collider.shape.overlaps(this._overlapTestCircle)) {
if (collider.shape.overlaps(this._overlapTestCircle)) { results[resultCounter] = collider;
results[resultCounter] = collider; resultCounter++;
resultCounter++; }
} }
else {
throw new Error("对这个对撞机类型的overlapCircle没有实现!");
}
// 如果我们所有的结果数据有了则返回
if (resultCounter === results.length)
return resultCounter;
} }
else {
throw new Error("对这个对撞机类型的overlapCircle没有实现!");
}
// 如果我们所有的结果数据有了则返回
if (resultCounter === results.length)
return resultCounter;
}
return resultCounter; return resultCounter;
}; };
/** /**
@@ -12601,7 +12588,7 @@ var es;
cacheCount -= this._objectQueue.get(type).length; cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) { if (cacheCount > 0) {
for (var i = 0; i < cacheCount; i++) { for (var i = 0; i < cacheCount; i++) {
this._objectQueue.get(type).unshift([]); this._objectQueue.get(type).push([]);
} }
} }
}; };
@@ -12612,7 +12599,7 @@ var es;
ListPool.trimCache = function (type, cacheCount) { ListPool.trimCache = function (type, cacheCount) {
this.checkCreate(type); this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length) while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift(); this._objectQueue.get(type).splice(0, 1);
}; };
/** /**
* 清除缓存 * 清除缓存
@@ -12636,11 +12623,11 @@ var es;
*/ */
ListPool.free = function (type, obj) { ListPool.free = function (type, obj) {
this.checkCreate(type); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj); this._objectQueue.get(type).push(obj);
obj.length = 0; obj.length = 0;
}; };
ListPool.checkCreate = function (type) { ListPool.checkCreate = function (type) {
if (!this._objectQueue.get(type)) if (!this._objectQueue.has(type))
this._objectQueue.set(type, []); this._objectQueue.set(type, []);
}; };
ListPool._objectQueue = new Map(); ListPool._objectQueue = new Map();
@@ -12741,7 +12728,7 @@ var es;
cacheCount -= this._objectQueue.get(type).length; cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) { if (cacheCount > 0) {
for (var i = 0; i < cacheCount; i++) { for (var i = 0; i < cacheCount; i++) {
this._objectQueue.get(type).unshift(new type()); this._objectQueue.get(type).push(new type());
} }
} }
}; };
@@ -12752,7 +12739,7 @@ var es;
Pool.trimCache = function (type, cacheCount) { Pool.trimCache = function (type, cacheCount) {
this.checkCreate(type); this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length) while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift(); this._objectQueue.get(type).splice(0, 1);
}; };
/** /**
* 清除缓存 * 清除缓存
@@ -12776,13 +12763,13 @@ var es;
*/ */
Pool.free = function (type, obj) { Pool.free = function (type, obj) {
this.checkCreate(type); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj); this._objectQueue.get(type).push(obj);
if (es.isIPoolable(obj)) { if (es.isIPoolable(obj)) {
obj["reset"](); obj["reset"]();
} }
}; };
Pool.checkCreate = function (type) { Pool.checkCreate = function (type) {
if (!this._objectQueue.get(type)) if (!this._objectQueue.has(type))
this._objectQueue.set(type, []); this._objectQueue.set(type, []);
}; };
Pool._objectQueue = new Map(); Pool._objectQueue = new Map();
@@ -15179,7 +15166,7 @@ var es;
this._timeInSeconds = timeInsSeconds; this._timeInSeconds = timeInsSeconds;
this._repeats = repeats; this._repeats = repeats;
this.context = context; this.context = context;
this._onTime = onTime; this._onTime = onTime.bind(context);
}; };
/** /**
* 空出对象引用以便在js需要时GC可以清理它们的引用 * 空出对象引用以便在js需要时GC可以清理它们的引用
@@ -15208,7 +15195,7 @@ var es;
for (var i = this._timers.length - 1; i >= 0; i--) { for (var i = this._timers.length - 1; i >= 0; i--) {
if (this._timers[i].tick()) { if (this._timers[i].tick()) {
this._timers[i].unload(); this._timers[i].unload();
new es.List(this._timers).removeAt(i); this._timers.splice(i, 1);
} }
} }
}; };

File diff suppressed because one or more lines are too long

View File

@@ -177,7 +177,7 @@ module es {
} }
protected async update(currentTime: number = -1) { protected update(currentTime: number = -1) {
if (Core.paused) { if (Core.paused) {
return; return;
} }

View File

@@ -43,6 +43,7 @@ module es {
} }
} }
if (colliders.length > 0) { if (colliders.length > 0) {
for (let i = 0; i < colliders.length; i++) { for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i]; let collider = colliders[i];

View File

@@ -259,31 +259,32 @@ module es {
let resultCounter = 0; let resultCounter = 0;
const potentials = this.aabbBroadphase(bounds, null, layerMask); const potentials = this.aabbBroadphase(bounds, null, layerMask);
for (let i = 0; i < potentials.length; i ++) { if (potentials.length > 0)
const collider = potentials[i]; for (let i = 0; i < potentials.length; i ++) {
if (collider instanceof BoxCollider) { const collider = potentials[i];
if (collider.shape.overlaps(this._overlapTestCircle)) { if (collider instanceof BoxCollider) {
results[resultCounter] = collider; if (collider.shape.overlaps(this._overlapTestCircle)) {
resultCounter++; results[resultCounter] = collider;
resultCounter++;
}
} else if (collider instanceof CircleCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
} else if (collider instanceof PolygonCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
} else {
throw new Error("对这个对撞机类型的overlapCircle没有实现!");
} }
} else if (collider instanceof CircleCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
} else if (collider instanceof PolygonCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
resultCounter++;
}
} else {
throw new Error("对这个对撞机类型的overlapCircle没有实现!");
}
// 如果我们所有的结果数据有了则返回 // 如果我们所有的结果数据有了则返回
if (resultCounter === results.length) if (resultCounter === results.length)
return resultCounter; return resultCounter;
} }
return resultCounter; return resultCounter;
} }

View File

@@ -14,7 +14,7 @@ module es {
cacheCount -= this._objectQueue.get(type).length; cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) { if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) { for (let i = 0; i < cacheCount; i++) {
this._objectQueue.get(type).unshift([]); this._objectQueue.get(type).push([]);
} }
} }
} }
@@ -26,7 +26,7 @@ module es {
public static trimCache<T>(type: new (...args) => T, cacheCount: number) { public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
this.checkCreate(type); this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length) while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift(); this._objectQueue.get(type).splice(0, 1);
} }
/** /**
@@ -52,14 +52,14 @@ module es {
* 将项推回堆栈 * 将项推回堆栈
* @param obj * @param obj
*/ */
public static free<T>(type: new (...args) => T, obj: Array<T>) { public static free<T>(type: new (...args) => T, obj: T[]) {
this.checkCreate(type); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj); this._objectQueue.get(type).push(obj);
obj.length = 0; obj.length = 0;
} }
private static checkCreate<T>(type: new (...args) => T) { private static checkCreate<T>(type: new (...args) => T) {
if (!this._objectQueue.get(type)) if (!this._objectQueue.has(type))
this._objectQueue.set(type, []); this._objectQueue.set(type, []);
} }
} }

View File

@@ -15,7 +15,7 @@ module es {
cacheCount -= this._objectQueue.get(type).length; cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) { if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) { for (let i = 0; i < cacheCount; i++) {
this._objectQueue.get(type).unshift(new type()); this._objectQueue.get(type).push(new type());
} }
} }
} }
@@ -27,7 +27,7 @@ module es {
public static trimCache<T>(type: new (...args) => T, cacheCount: number) { public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
this.checkCreate(type); this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length) while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift(); this._objectQueue.get(type).splice(0, 1);
} }
/** /**
@@ -55,7 +55,7 @@ module es {
*/ */
public static free<T>(type: new (...args) => T, obj: T) { public static free<T>(type: new (...args) => T, obj: T) {
this.checkCreate(type); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj); this._objectQueue.get(type).push(obj);
if (isIPoolable(obj)) { if (isIPoolable(obj)) {
obj["reset"](); obj["reset"]();
@@ -63,7 +63,7 @@ module es {
} }
private static checkCreate<T>(type: new (...args) => T) { private static checkCreate<T>(type: new (...args) => T) {
if (!this._objectQueue.get(type)) if (!this._objectQueue.has(type))
this._objectQueue.set(type, []); this._objectQueue.set(type, []);
} }
} }

View File

@@ -41,7 +41,7 @@ module es {
this._timeInSeconds = timeInsSeconds; this._timeInSeconds = timeInsSeconds;
this._repeats = repeats; this._repeats = repeats;
this.context = context; this.context = context;
this._onTime = onTime; this._onTime = onTime.bind(context);
} }
/** /**

View File

@@ -9,7 +9,7 @@ module es {
for (let i = this._timers.length - 1; i >= 0; i --){ for (let i = this._timers.length - 1; i >= 0; i --){
if (this._timers[i].tick()){ if (this._timers[i].tick()){
this._timers[i].unload(); this._timers[i].unload();
new es.List(this._timers).removeAt(i); this._timers.splice(i, 1);
} }
} }
} }