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

View File

@@ -1314,7 +1314,7 @@ declare module es {
}
}
declare module es {
abstract class Collider extends Component {
class Collider extends Component {
static readonly lateSortOrder: number;
castSortOrder: number;
/**
@@ -5328,25 +5328,26 @@ declare module es {
* 预热缓存使用最大的cacheCount对象填充缓存
* @param cacheCount
*/
static warmCache(cacheCount: number): void;
static warmCache<T>(type: new (...args: any[]) => T, cacheCount: number): void;
/**
* 将缓存修剪为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
*/
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 {
@@ -5674,7 +5675,7 @@ declare module es {
* @param list
* @param itemCount 从列表中返回的随机项目的数量
*/
static randomItems<T>(list: T[], itemCount: number): T[];
static randomItems<T>(type: any, list: T[], itemCount: number): T[];
}
}
declare module es {

View File

@@ -3120,7 +3120,7 @@ var es;
finally { if (e_3) throw e_3.error; }
}
}
es.ListPool.free(colliders);
es.ListPool.free(es.Collider, colliders);
return collisionResult.collider != null;
};
/**
@@ -4964,7 +4964,7 @@ var es;
EntityList.prototype.entitiesWithTag = function (tag) {
var e_7, _a;
var list = this.getTagList(tag);
var returnList = es.ListPool.obtain();
var returnList = es.ListPool.obtain(es.Entity);
if (list.size > 0) {
try {
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
*/
EntityList.prototype.findComponentsOfType = function (type) {
var comps = es.ListPool.obtain();
var comps = es.ListPool.obtain(type);
if (this._entities.length > 0) {
for (var i = 0, s = this._entities.length; i < s; i++) {
var entity = this._entities[i];
@@ -6547,7 +6547,7 @@ var es;
*/
Bezier.getOptimizedDrawingPoints = function (start, firstCtrlPoint, secondCtrlPoint, end, distanceTolerance) {
if (distanceTolerance === void 0) { distanceTolerance = 1; }
var points = es.ListPool.obtain();
var points = es.ListPool.obtain(es.Vector2);
points.push(start);
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
points.push(end);
@@ -14012,11 +14012,12 @@ var es;
* 预热缓存使用最大的cacheCount对象填充缓存
* @param cacheCount
*/
ListPool.warmCache = function (cacheCount) {
cacheCount -= this._objectQueue.length;
ListPool.warmCache = function (type, cacheCount) {
this.checkCreate(type);
cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) {
for (var i = 0; i < cacheCount; i++) {
this._objectQueue.unshift([]);
this._objectQueue.get(type).unshift([]);
}
}
};
@@ -14024,33 +14025,41 @@ var es;
* 将缓存修剪为cacheCount项目
* @param cacheCount
*/
ListPool.trimCache = function (cacheCount) {
while (cacheCount > this._objectQueue.length)
this._objectQueue.shift();
ListPool.trimCache = function (type, cacheCount) {
this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift();
};
/**
* 清除缓存
*/
ListPool.clearCache = function () {
this._objectQueue.length = 0;
ListPool.clearCache = function (type) {
this.checkCreate(type);
this._objectQueue.get(type).length = 0;
};
/**
* 如果可以的话,从堆栈中弹出一个项
*/
ListPool.obtain = function () {
if (this._objectQueue.length > 0)
return this._objectQueue.shift();
ListPool.obtain = function (type) {
this.checkCreate(type);
if (this._objectQueue.get(type).length > 0)
return this._objectQueue.get(type).shift();
return [];
};
/**
* 将项推回堆栈
* @param obj
*/
ListPool.free = function (obj) {
this._objectQueue.unshift(obj);
ListPool.free = function (type, obj) {
this.checkCreate(type);
this._objectQueue.get(type).unshift(obj);
obj.length = 0;
};
ListPool._objectQueue = [];
ListPool.checkCreate = function (type) {
if (!this._objectQueue.get(type))
this._objectQueue.set(type, []);
};
ListPool._objectQueue = new Map();
return ListPool;
}());
es.ListPool = ListPool;
@@ -14910,14 +14919,14 @@ var es;
* @param list
* @param itemCount 从列表中返回的随机项目的数量
*/
ArrayUtils.randomItems = function (list, itemCount) {
ArrayUtils.randomItems = function (type, list, itemCount) {
var set = new Set();
while (set.size != itemCount) {
var item = this.randomItem(list);
if (!set.has(item))
set.add(item);
}
var items = es.ListPool.obtain();
var items = es.ListPool.obtain(type);
set.forEach(function (value) { return items.push(value); });
return items;
};
@@ -16481,7 +16490,7 @@ var es;
*/
VisibilityComputer.prototype.end = function () {
var e_13, _a;
var output = es.ListPool.obtain();
var output = es.ListPool.obtain(es.Vector2);
this.updateSegments();
this._endPoints.sort(this._radialComparer.compare);
var currentAngle = 0;

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
module es {
export abstract class Collider extends Component {
export class Collider extends Component {
public static readonly lateSortOrder = 999;
public castSortOrder: number = 0;
/**

View File

@@ -73,7 +73,7 @@ module es {
}
}
ListPool.free(colliders);
ListPool.free(Collider, colliders);
return collisionResult.collider != null;
}

View File

@@ -223,7 +223,7 @@ module es {
public entitiesWithTag(tag: number) {
let list = this.getTagList(tag);
let returnList = ListPool.obtain<Entity>();
let returnList = ListPool.obtain<Entity>(Entity);
if (list.size > 0) {
for (let entity of list) {
returnList.push(entity);
@@ -286,7 +286,7 @@ module es {
* @param type
*/
public findComponentsOfType<T extends Component>(type): T[] {
let comps = ListPool.obtain<T>();
let comps = ListPool.obtain<T>(type);
if (this._entities.length > 0) {
for (let i = 0, s = this._entities.length; i < s; i++) {
let entity = this._entities[i];

View File

@@ -76,7 +76,7 @@ module es {
*/
public static getOptimizedDrawingPoints(start: Vector2, firstCtrlPoint: Vector2, secondCtrlPoint: Vector2,
end: Vector2, distanceTolerance: number = 1) {
let points = ListPool.obtain<Vector2>();
let points = ListPool.obtain<Vector2>(Vector2);
points.push(start);
this.recursiveGetOptimizedDrawingPoints(start, firstCtrlPoint, secondCtrlPoint, end, points, distanceTolerance);
points.push(end);

View File

@@ -3,17 +3,18 @@ module es {
* 可以用于列表池的简单类
*/
export class ListPool {
private static readonly _objectQueue = [];
private static readonly _objectQueue: Map<any, any[]> = new Map();
/**
* 预热缓存使用最大的cacheCount对象填充缓存
* @param cacheCount
*/
public static warmCache(cacheCount: number) {
cacheCount -= this._objectQueue.length;
public static warmCache<T>(type: new (...args) => T, cacheCount: number) {
this.checkCreate(type);
cacheCount -= this._objectQueue.get(type).length;
if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) {
this._objectQueue.unshift([]);
this._objectQueue.get(type).unshift([]);
}
}
}
@@ -22,24 +23,27 @@ module es {
* 将缓存修剪为cacheCount项目
* @param cacheCount
*/
public static trimCache(cacheCount) {
while (cacheCount > this._objectQueue.length)
this._objectQueue.shift();
public static trimCache<T>(type: new (...args) => T, cacheCount: number) {
this.checkCreate(type);
while (cacheCount > this._objectQueue.get(type).length)
this._objectQueue.get(type).shift();
}
/**
* 清除缓存
*/
public static clearCache() {
this._objectQueue.length = 0;
public static clearCache<T>(type: new (...args) => T) {
this.checkCreate(type);
this._objectQueue.get(type).length = 0;
}
/**
* 如果可以的话,从堆栈中弹出一个项
*/
public static obtain<T>(): T[] {
if (this._objectQueue.length > 0)
return this._objectQueue.shift();
public static obtain<T>(type: new (...args) => T): T[] {
this.checkCreate(type);
if (this._objectQueue.get(type).length > 0)
return this._objectQueue.get(type).shift();
return [];
}
@@ -48,9 +52,15 @@ module es {
* 将项推回堆栈
* @param obj
*/
public static free<T>(obj: Array<T>) {
this._objectQueue.unshift(obj);
public static free<T>(type: new (...args) => T, obj: Array<T>) {
this.checkCreate(type);
this._objectQueue.get(type).unshift(obj);
obj.length = 0;
}
private static checkCreate<T>(type: new (...args) => T) {
if (!this._objectQueue.get(type))
this._objectQueue.set(type, []);
}
}
}

View File

@@ -283,7 +283,7 @@ module es {
* @param list
* @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>();
while (set.size != itemCount) {
let item = this.randomItem(list);
@@ -291,7 +291,7 @@ module es {
set.add(item);
}
let items = es.ListPool.obtain<T>();
let items = es.ListPool.obtain<T>(type);
set.forEach(value => items.push(value));
return items;
}

View File

@@ -148,7 +148,7 @@ module es {
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/
public end(): Vector2[] {
let output = ListPool.obtain<Vector2>();
let output = ListPool.obtain<Vector2>(Vector2);
this.updateSegments();
this._endPoints.sort(this._radialComparer.compare);