优化updateList,性能提高百倍!

This commit is contained in:
yhh
2021-02-01 11:11:53 +08:00
parent 25fca1ed56
commit d7ec4d087c
6 changed files with 50 additions and 60 deletions

View File

@@ -1683,7 +1683,7 @@ declare module es {
/** /**
* 通过标签跟踪实体,便于检索 * 通过标签跟踪实体,便于检索
*/ */
_entityDict: Map<number, Entity[]>; _entityDict: Map<number, Set<Entity>>;
_unsortedTags: Set<number>; _unsortedTags: Set<number>;
constructor(scene: Scene); constructor(scene: Scene);
readonly count: number; readonly count: number;
@@ -1709,7 +1709,7 @@ declare module es {
* @param entity * @param entity
*/ */
contains(entity: Entity): boolean; contains(entity: Entity): boolean;
getTagList(tag: number): Entity[]; getTagList(tag: number): Set<Entity>;
addToTagList(entity: Entity): void; addToTagList(entity: Entity): void;
removeFromTagList(entity: Entity): void; removeFromTagList(entity: Entity): void;
update(): void; update(): void;

View File

@@ -4078,23 +4078,18 @@ var es;
EntityList.prototype.getTagList = function (tag) { EntityList.prototype.getTagList = function (tag) {
var list = this._entityDict.get(tag); var list = this._entityDict.get(tag);
if (!list) { if (!list) {
list = []; list = new Set();
this._entityDict.set(tag, list); this._entityDict.set(tag, list);
} }
return list; return list;
}; };
EntityList.prototype.addToTagList = function (entity) { EntityList.prototype.addToTagList = function (entity) {
var list = this.getTagList(entity.tag); this.getTagList(entity.tag).add(entity);
if (list.findIndex(function (e) { return e.id == entity.id; }) == -1) {
list.push(entity);
this._unsortedTags.add(entity.tag); this._unsortedTags.add(entity.tag);
}
}; };
EntityList.prototype.removeFromTagList = function (entity) { EntityList.prototype.removeFromTagList = function (entity) {
var list = this._entityDict.get(entity.tag); var list = this._entityDict.get(entity.tag);
if (list) { list.delete(entity);
new linq.List(list).remove(entity);
}
}; };
EntityList.prototype.update = function () { EntityList.prototype.update = function () {
var e_11, _a; var e_11, _a;
@@ -4144,11 +4139,6 @@ var es;
this._entities.sort(es.Entity.entityComparer.compare); this._entities.sort(es.Entity.entityComparer.compare);
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
// 根据需要对标签列表进行排序
if (this._unsortedTags.size > 0) {
this._unsortedTags.forEach(function (value) { return _this._entityDict.get(value).sort(function (a, b) { return a.compareTo(b); }); });
this._unsortedTags.clear();
}
}; };
/** /**
* 返回第一个找到的名字为name的实体。如果没有找到则返回null * 返回第一个找到的名字为name的实体。如果没有找到则返回null
@@ -4172,11 +4162,23 @@ var es;
* @param tag * @param tag
*/ */
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var e_12, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
returnList.length = this._entities.length; returnList.length = this._entities.length;
for (var i = 0; i < list.length; i++) try {
returnList.push(list[i]); for (var list_1 = __values(list), list_1_1 = list_1.next(); !list_1_1.done; list_1_1 = list_1.next()) {
var entity = list_1_1.value;
returnList.push(entity);
}
}
catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally {
try {
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
}
finally { if (e_12) throw e_12.error; }
}
return returnList; return returnList;
}; };
/** /**
@@ -7151,7 +7153,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) { SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_12, _a; var e_13, _a;
this._overlapTestBox.updateBox(rect.width, rect.height); this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location; this._overlapTestBox.position = rect.location;
var resultCounter = 0; var resultCounter = 0;
@@ -7182,12 +7184,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_12_1) { e_12 = { error: e_12_1 }; } catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally { finally {
try { try {
if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1); if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1);
} }
finally { if (e_12) throw e_12.error; } finally { if (e_13) throw e_13.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -7199,7 +7201,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var e_13, _a; var e_14, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
@@ -7232,12 +7234,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_13_1) { e_13 = { error: e_13_1 }; } catch (e_14_1) { e_14 = { error: e_14_1 }; }
finally { finally {
try { try {
if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2); if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2);
} }
finally { if (e_13) throw e_13.error; } finally { if (e_14) throw e_14.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -9072,7 +9074,7 @@ var es;
* 重置标记记录 * 重置标记记录
*/ */
TimeRuler.prototype.resetLog = function () { TimeRuler.prototype.resetLog = function () {
var e_14, _a; var e_15, _a;
if (!es.Core.Instance.debug) if (!es.Core.Instance.debug)
return; return;
try { try {
@@ -9090,12 +9092,12 @@ var es;
} }
} }
} }
catch (e_14_1) { e_14 = { error: e_14_1 }; } catch (e_15_1) { e_15 = { error: e_15_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_14) throw e_14.error; } finally { if (e_15) throw e_15.error; }
} }
}; };
/** /**
@@ -11685,7 +11687,7 @@ var linq;
* 创建一个Set从一个Enumerable.List< T>。 * 创建一个Set从一个Enumerable.List< T>。
*/ */
List.prototype.toSet = function () { List.prototype.toSet = function () {
var e_15, _a; var e_16, _a;
var result = new Set(); var result = new Set();
try { try {
for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -11693,12 +11695,12 @@ var linq;
result.add(x); result.add(x);
} }
} }
catch (e_15_1) { e_15 = { error: e_15_1 }; } catch (e_16_1) { e_16 = { error: e_16_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_15) throw e_15.error; } finally { if (e_16) throw e_16.error; }
} }
return result; return result;
}; };
@@ -11947,7 +11949,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool * 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/ */
VisibilityComputer.prototype.end = function () { VisibilityComputer.prototype.end = function () {
var e_16, _a; var e_17, _a;
var output = es.ListPool.obtain(); var output = es.ListPool.obtain();
this.updateSegments(); this.updateSegments();
this._endPoints.sort(this._radialComparer.compare); this._endPoints.sort(this._radialComparer.compare);
@@ -11986,12 +11988,12 @@ var es;
} }
} }
} }
catch (e_16_1) { e_16 = { error: e_16_1 }; } catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_16) throw e_16.error; } finally { if (e_17) throw e_17.error; }
} }
} }
VisibilityComputer._openSegments.clear(); VisibilityComputer._openSegments.clear();
@@ -12107,7 +12109,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类 * 处理片段,以便我们稍后对它们进行分类
*/ */
VisibilityComputer.prototype.updateSegments = function () { VisibilityComputer.prototype.updateSegments = function () {
var e_17, _a; var e_18, _a;
try { try {
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
var segment = _c.value; var segment = _c.value;
@@ -12125,12 +12127,12 @@ var es;
segment.p2.begin = !segment.p1.begin; segment.p2.begin = !segment.p1.begin;
} }
} }
catch (e_17_1) { e_17 = { error: e_17_1 }; } catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_17) throw e_17.error; } finally { if (e_18) throw e_18.error; }
} }
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。 // 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。 // 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -39,6 +39,5 @@ gulp.task('build', ['copy'], () => {
.pipe(gulp.dest('../extensions/behaviourTree-ai/source/lib/')) .pipe(gulp.dest('../extensions/behaviourTree-ai/source/lib/'))
.pipe(gulp.dest('../extensions/ecs-star/lib/')) .pipe(gulp.dest('../extensions/ecs-star/lib/'))
.pipe(gulp.dest('../extensions/ecs-tween/lib/')) .pipe(gulp.dest('../extensions/ecs-tween/lib/'))
.pipe(gulp.dest('../engine_support/egret/lib/'))
]) ])
}); });

View File

@@ -20,7 +20,7 @@ module es {
/** /**
* 通过标签跟踪实体,便于检索 * 通过标签跟踪实体,便于检索
*/ */
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>(); public _entityDict: Map<number, Set<Entity>> = new Map<number, Set<Entity>>();
public _unsortedTags: Set<number> = new Set<number>(); public _unsortedTags: Set<number> = new Set<number>();
constructor(scene: Scene) { constructor(scene: Scene) {
@@ -101,7 +101,7 @@ module es {
public getTagList(tag: number) { public getTagList(tag: number) {
let list = this._entityDict.get(tag); let list = this._entityDict.get(tag);
if (!list) { if (!list) {
list = []; list = new Set();
this._entityDict.set(tag, list); this._entityDict.set(tag, list);
} }
@@ -109,18 +109,13 @@ module es {
} }
public addToTagList(entity: Entity) { public addToTagList(entity: Entity) {
let list = this.getTagList(entity.tag); this.getTagList(entity.tag).add(entity);
if (list.findIndex(e => e.id == entity.id) == -1) {
list.push(entity);
this._unsortedTags.add(entity.tag); this._unsortedTags.add(entity.tag);
} }
}
public removeFromTagList(entity: Entity) { public removeFromTagList(entity: Entity) {
let list = this._entityDict.get(entity.tag); let list = this._entityDict.get(entity.tag);
if (list) { list.delete(entity);
new linq.List(list).remove(entity);
}
} }
public update() { public update() {
@@ -168,13 +163,6 @@ module es {
this._entities.sort(Entity.entityComparer.compare); this._entities.sort(Entity.entityComparer.compare);
this._isEntityListUnsorted = false; this._isEntityListUnsorted = false;
} }
// 根据需要对标签列表进行排序
if (this._unsortedTags.size > 0) {
this._unsortedTags.forEach(value => this._entityDict.get(value).sort((a, b) => a.compareTo(b)));
this._unsortedTags.clear();
}
} }
/** /**
@@ -206,8 +194,9 @@ module es {
let returnList = ListPool.obtain<Entity>(); let returnList = ListPool.obtain<Entity>();
returnList.length = this._entities.length; returnList.length = this._entities.length;
for (let i = 0; i < list.length; i++) for (let entity of list) {
returnList.push(list[i]); returnList.push(entity);
}
return returnList; return returnList;
} }

View File

@@ -10,7 +10,7 @@ module es {
* @param type * @param type
* @param cacheCount * @param cacheCount
*/ */
public static warmCache(type: any, cacheCount: number){ public static warmCache(type: any, cacheCount: number) {
cacheCount -= this._objectQueue.length; cacheCount -= this._objectQueue.length;
if (cacheCount > 0) { if (cacheCount > 0) {
for (let i = 0; i < cacheCount; i++) { for (let i = 0; i < cacheCount; i++) {
@@ -23,7 +23,7 @@ module es {
* 将缓存修剪为cacheCount项目 * 将缓存修剪为cacheCount项目
* @param cacheCount * @param cacheCount
*/ */
public static trimCache(cacheCount: number){ public static trimCache(cacheCount: number) {
while (cacheCount > this._objectQueue.length) while (cacheCount > this._objectQueue.length)
this._objectQueue.shift(); this._objectQueue.shift();
} }
@@ -52,7 +52,7 @@ module es {
public static free<T>(obj: T) { public static free<T>(obj: T) {
this._objectQueue.unshift(obj); this._objectQueue.unshift(obj);
if (isIPoolable(obj)){ if (isIPoolable(obj)) {
obj["reset"](); obj["reset"]();
} }
} }