listpool根据type划分池

This commit is contained in:
yhh
2021-08-06 11:10:45 +08:00
parent 0beadf8e5a
commit f533186c8d
10 changed files with 71 additions and 51 deletions
+8 -7
View File
@@ -1314,7 +1314,7 @@ declare module es {
} }
} }
declare module es { declare module es {
abstract class Collider extends Component { class Collider extends Component {
static readonly lateSortOrder: number; static readonly lateSortOrder: number;
castSortOrder: number; castSortOrder: number;
/** /**
@@ -5328,25 +5328,26 @@ declare module es {
* 预热缓存,使用最大的cacheCount对象填充缓存 * 预热缓存,使用最大的cacheCount对象填充缓存
* @param cacheCount * @param cacheCount
*/ */
static warmCache(cacheCount: number): void; static warmCache<T>(type: new (...args: any[]) => T, cacheCount: number): void;
/** /**
* 将缓存修剪为cacheCount项目 * 将缓存修剪为cacheCount项目
* @param cacheCount * @param cacheCount
*/ */
static trimCache(cacheCount: any): void; static trimCache<T>(type: new (...args: any[]) => T, cacheCount: number): void;
/** /**
* 清除缓存 * 清除缓存
*/ */
static clearCache(): void; static clearCache<T>(type: new (...args: any[]) => T): void;
/** /**
* 如果可以的话,从堆栈中弹出一个项 * 如果可以的话,从堆栈中弹出一个项
*/ */
static obtain<T>(): T[]; static obtain<T>(type: new (...args: any[]) => T): T[];
/** /**
* 将项推回堆栈 * 将项推回堆栈
* @param obj * @param obj
*/ */
static free<T>(obj: Array<T>): void; static free<T>(type: new (...args: any[]) => T, obj: Array<T>): void;
private static checkCreate;
} }
} }
declare module es { declare module es {
@@ -5674,7 +5675,7 @@ declare module es {
* @param list * @param list
* @param itemCount 从列表中返回的随机项目的数量 * @param itemCount 从列表中返回的随机项目的数量
*/ */
static randomItems<T>(list: T[], itemCount: number): T[]; static randomItems<T>(type: any, list: T[], itemCount: number): T[];
} }
} }
declare module es { declare module es {
+30 -21
View File
@@ -3120,7 +3120,7 @@ var es;
finally { if (e_3) throw e_3.error; } finally { if (e_3) throw e_3.error; }
} }
} }
es.ListPool.free(colliders); es.ListPool.free(es.Collider, colliders);
return collisionResult.collider != null; return collisionResult.collider != null;
}; };
/** /**
@@ -4964,7 +4964,7 @@ var es;
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var e_7, _a; var e_7, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain(es.Entity);
if (list.size > 0) { if (list.size > 0) {
try { try {
for (var list_1 = __values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) { for (var list_1 = __values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) {
@@ -5040,7 +5040,7 @@ var es;
* @param type * @param type
*/ */
EntityList.prototype.findComponentsOfType = function (type) { EntityList.prototype.findComponentsOfType = function (type) {
var comps = es.ListPool.obtain(); var comps = es.ListPool.obtain(type);
if (this._entities.length > 0) { if (this._entities.length > 0) {
for (var i = 0, s = this._entities.length; i < s; i++) { for (var i = 0, s = this._entities.length; i < s; i++) {
var entity = this._entities[i]; var entity = this._entities[i];
@@ -6547,7 +6547,7 @@ var es;
*/ */
Bezier.getOptimizedDrawingPoints = function (start, firstCtrlPoint, secondCtrlPoint, end, distanceTolerance) { Bezier.getOptimizedDrawingPoints = function (start, firstCtrlPoint, secondCtrlPoint, end, distanceTolerance) {
if (distanceTolerance === void 0) { distanceTolerance = 1; } if (distanceTolerance === void 0) { distanceTolerance = 1; }
var points = es.ListPool.obtain(); var points = es.ListPool.obtain(es.Vector2);
points.push(start); points.push(start);
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance); this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
points.push(end); points.push(end);
@@ -14012,11 +14012,12 @@ var es;
* 预热缓存使用最大的cacheCount对象填充缓存 * 预热缓存使用最大的cacheCount对象填充缓存
* @param cacheCount * @param cacheCount
*/ */
ListPool.warmCache = function (cacheCount) { ListPool.warmCache = function (type, cacheCount) {
cacheCount -= this._objectQueue.length; this.checkCreate(type);
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.unshift([]); this._objectQueue.get(type).unshift([]);
} }
} }
}; };
@@ -14024,33 +14025,41 @@ var es;
* 将缓存修剪为cacheCount项目 * 将缓存修剪为cacheCount项目
* @param cacheCount * @param cacheCount
*/ */
ListPool.trimCache = function (cacheCount) { ListPool.trimCache = function (type, cacheCount) {
while (cacheCount > this._objectQueue.length) this.checkCreate(type);
this._objectQueue.shift(); while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift();
}; };
/** /**
* 清除缓存 * 清除缓存
*/ */
ListPool.clearCache = function () { ListPool.clearCache = function (type) {
this._objectQueue.length = 0; this.checkCreate(type);
this._objectQueue.get(type).length = 0;
}; };
/** /**
* 如果可以的话从堆栈中弹出一个项 * 如果可以的话从堆栈中弹出一个项
*/ */
ListPool.obtain = function () { ListPool.obtain = function (type) {
if (this._objectQueue.length > 0) this.checkCreate(type);
return this._objectQueue.shift(); if (this._objectQueue.get(type).length > 0)
return this._objectQueue.get(type).shift();
return []; return [];
}; };
/** /**
* 将项推回堆栈 * 将项推回堆栈
* @param obj * @param obj
*/ */
ListPool.free = function (obj) { ListPool.free = function (type, obj) {
this._objectQueue.unshift(obj); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj);
obj.length = 0; obj.length = 0;
}; };
ListPool._objectQueue = []; ListPool.checkCreate = function (type) {
if (!this._objectQueue.get(type))
this._objectQueue.set(type, []);
};
ListPool._objectQueue = new Map();
return ListPool; return ListPool;
}()); }());
es.ListPool = ListPool; es.ListPool = ListPool;
@@ -14910,14 +14919,14 @@ var es;
* @param list * @param list
* @param itemCount 从列表中返回的随机项目的数量 * @param itemCount 从列表中返回的随机项目的数量
*/ */
ArrayUtils.randomItems = function (list, itemCount) { ArrayUtils.randomItems = function (type, list, itemCount) {
var set = new Set(); var set = new Set();
while (set.size != itemCount) { while (set.size != itemCount) {
var item = this.randomItem(list); var item = this.randomItem(list);
if (!set.has(item)) if (!set.has(item))
set.add(item); set.add(item);
} }
var items = es.ListPool.obtain(); var items = es.ListPool.obtain(type);
set.forEach(function (value) { return items.push(value); }); set.forEach(function (value) { return items.push(value); });
return items; return items;
}; };
@@ -16481,7 +16490,7 @@ var es;
*/ */
VisibilityComputer.prototype.end = function () { VisibilityComputer.prototype.end = function () {
var e_13, _a; var e_13, _a;
var output = es.ListPool.obtain(); var output = es.ListPool.obtain(es.Vector2);
this.updateSegments(); this.updateSegments();
this._endPoints.sort(this._radialComparer.compare); this._endPoints.sort(this._radialComparer.compare);
var currentAngle = 0; var currentAngle = 0;
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
module es { module es {
export abstract class Collider extends Component { export class Collider extends Component {
public static readonly lateSortOrder = 999; public static readonly lateSortOrder = 999;
public castSortOrder: number = 0; public castSortOrder: number = 0;
/** /**
+1 -1
View File
@@ -73,7 +73,7 @@ module es {
} }
} }
ListPool.free(colliders); ListPool.free(Collider, colliders);
return collisionResult.collider != null; return collisionResult.collider != null;
} }
+2 -2
View File
@@ -223,7 +223,7 @@ module es {
public entitiesWithTag(tag: number) { public entitiesWithTag(tag: number) {
let list = this.getTagList(tag); let list = this.getTagList(tag);
let returnList = ListPool.obtain<Entity>(); let returnList = ListPool.obtain<Entity>(Entity);
if (list.size > 0) { if (list.size > 0) {
for (let entity of list) { for (let entity of list) {
returnList.push(entity); returnList.push(entity);
@@ -286,7 +286,7 @@ module es {
* @param type * @param type
*/ */
public findComponentsOfType<T extends Component>(type): T[] { public findComponentsOfType<T extends Component>(type): T[] {
let comps = ListPool.obtain<T>(); let comps = ListPool.obtain<T>(type);
if (this._entities.length > 0) { if (this._entities.length > 0) {
for (let i = 0, s = this._entities.length; i < s; i++) { for (let i = 0, s = this._entities.length; i < s; i++) {
let entity = this._entities[i]; let entity = this._entities[i];
+1 -1
View File
@@ -76,7 +76,7 @@ module es {
*/ */
public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2, public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
end: Vector2, distanceTolerance: number = 1) { end: Vector2, distanceTolerance: number = 1) {
let points = ListPool.obtain<Vector2>(); let points = ListPool.obtain<Vector2>(Vector2);
points.push(start); points.push(start);
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance); this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
points.push(end); points.push(end);
+24 -14
View File
@@ -3,17 +3,18 @@ module es {
* *
*/ */
export class ListPool { export class ListPool {
private static readonly _objectQueue = []; private static readonly _objectQueue: Map<any, any[]> = new Map();
/** /**
* 使cacheCount对象填充缓存 * 使cacheCount对象填充缓存
* @param cacheCount * @param cacheCount
*/ */
public static warmCache(cacheCount: number) { public static warmCache<T>(type: new (...args) => T, cacheCount: number) {
cacheCount -= this._objectQueue.length; this.checkCreate(type);
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.unshift([]); this._objectQueue.get(type).unshift([]);
} }
} }
} }
@@ -22,24 +23,27 @@ module es {
* cacheCount项目 * cacheCount项目
* @param cacheCount * @param cacheCount
*/ */
public static trimCache(cacheCount) { public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
while (cacheCount > this._objectQueue.length) this.checkCreate(type);
this._objectQueue.shift(); while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift();
} }
/** /**
* *
*/ */
public static clearCache() { public static clearCache<T>(type: new (...args) => T) {
this._objectQueue.length = 0; this.checkCreate(type);
this._objectQueue.get(type).length = 0;
} }
/** /**
* *
*/ */
public static obtain<T>(): T[] { public static obtain<T>(type: new (...args) => T): T[] {
if (this._objectQueue.length > 0) this.checkCreate(type);
return this._objectQueue.shift(); if (this._objectQueue.get(type).length > 0)
return this._objectQueue.get(type).shift();
return []; return [];
} }
@@ -48,9 +52,15 @@ module es {
* *
* @param obj * @param obj
*/ */
public static free<T>(obj: Array<T>) { public static free<T>(type: new (...args) => T, obj: Array<T>) {
this._objectQueue.unshift(obj); this.checkCreate(type);
this._objectQueue.get(type).unshift(obj);
obj.length = 0; obj.length = 0;
} }
private static checkCreate<T>(type: new (...args) => T) {
if (!this._objectQueue.get(type))
this._objectQueue.set(type, []);
}
} }
} }
+2 -2
View File
@@ -283,7 +283,7 @@ module es {
* @param list * @param list
* @param itemCount * @param itemCount
*/ */
public static randomItems<T>(list: T[], itemCount: number){ public static randomItems<T>(type: any, list: T[], itemCount: number){
let set = new Set<T>(); let set = new Set<T>();
while (set.size != itemCount) { while (set.size != itemCount) {
let item = this.randomItem(list); let item = this.randomItem(list);
@@ -291,7 +291,7 @@ module es {
set.add(item); set.add(item);
} }
let items = es.ListPool.obtain<T>(); let items = es.ListPool.obtain<T>(type);
set.forEach(value => items.push(value)); set.forEach(value => items.push(value));
return items; return items;
} }
@@ -148,7 +148,7 @@ module es {
* ListPool * ListPool
*/ */
public end(): Vector2[] { public end(): Vector2[] {
let output = ListPool.obtain<Vector2>(); let output = ListPool.obtain<Vector2>(Vector2);
this.updateSegments(); this.updateSegments();
this._endPoints.sort(this._radialComparer.compare); this._endPoints.sort(this._radialComparer.compare);