优化getComponent与getComponents性能

This commit is contained in:
yhh
2021-04-26 15:23:16 +08:00
parent d576a95548
commit bc6920f829
7 changed files with 215 additions and 127 deletions

View File

@@ -11,6 +11,9 @@ module es {
* 添加到实体的组件列表
*/
public _components: Component[] = [];
/** 记录component的快速读取列表 */
public fastComponentsMap = new Map<new (...args: any[]) => Component, es.Component[]>();
public fastComponentsToAddMap = new Map<new (...args: any[]) => Component, es.Component[]>();
/**
* 所有需要更新的组件列表
*/
@@ -47,6 +50,7 @@ module es {
public add(component: Component) {
this._componentsToAdd[component.id] = component;
this.addFastComponentToAdd(component);
}
public remove(component: Component) {
@@ -55,6 +59,7 @@ module es {
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd[component.id]) {
delete this._componentsToAdd[component.id];
this.removeFastComponentToAdd(component);
return;
}
@@ -69,6 +74,8 @@ module es {
this.handleRemove(this._components[i]);
}
this.fastComponentsMap.clear();
this.fastComponentsToAddMap.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd = {};
@@ -106,11 +113,13 @@ module es {
let component = this._componentsToRemove[i];
this.handleRemove(component);
for (let index = 0; index < this._components.length; index ++) {
if (this._components[index].id == component.id) {
let searchComponent = this._components[index];
if (searchComponent.id == component.id) {
this._components.splice(index, 1);
break;
}
}
this.removeFastComponent(component);
}
this._componentsToRemove = {};
@@ -123,6 +132,7 @@ module es {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this.addFastComponent(component);
this._components.push(component);
this._tempBufferList.push(component);
@@ -130,6 +140,7 @@ module es {
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {};
this.fastComponentsToAddMap.clear();
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
@@ -157,6 +168,35 @@ module es {
component.entity = null;
}
private removeFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsMap.set(TypeUtils.getType(component), fastList);
}
private removeFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsToAddMap.set(TypeUtils.getType(component), fastList);
}
/**
* 获取类型T的第一个组件并返回它
@@ -166,18 +206,15 @@ module es {
* @param onlyReturnInitializedComponents
*/
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
for (let component of this._components) {
if (component instanceof type)
return component as T;
}
let fastList = this.fastComponentsMap.get(type);
if (fastList && fastList.length > 0)
return fastList[0] as T;
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) {
for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
if (component instanceof type)
return component as T;
}
let fastToAddList = this.fastComponentsToAddMap.get(type);
if (fastToAddList && fastToAddList.length > 0)
return fastToAddList[0] as T;
}
return null;
@@ -188,23 +225,17 @@ module es {
* @param typeName
* @param components
*/
public getComponents(typeName: any, components?) {
public getComponents(typeName: any, components?: any[]) {
if (!components)
components = [];
for (let component of this._components) {
if (component instanceof typeName) {
components.push(component);
}
}
let fastList = this.fastComponentsMap.get(typeName);
if (fastList)
components.concat(fastList);
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
if (component instanceof typeName) {
components.push(component);
}
}
let fastToAddList = this.fastComponentsToAddMap.get(typeName);
if (fastToAddList)
components.concat(fastToAddList);
return components;
}

View File

@@ -154,12 +154,6 @@ module es {
}
this._entitiesToAdded = {};
// this._isEntityListUnsorted = true;
// if (this._isEntityListUnsorted) {
// this._entities.sort(Entity.entityComparer.compare);
// this._isEntityListUnsorted = false;
// }
}
/**
@@ -172,11 +166,6 @@ module es {
return this._entities[i];
}
// for (let i = 0; i < this._entitiesToAdded.size; i++) {
// let entity = this._entitiesToAdded.values;
// if (entity.name == name)
// return entity;
// }
for (let i in this._entitiesToAdded) {
let entity = this._entitiesToAdded[i];
if (entity.name == name)
@@ -186,6 +175,20 @@ module es {
return null;
}
/**
*
* @param id
* @returns
*/
public findEntityById(id: number) {
for (let i = 0; i < this._entities.length; i ++) {
if (this._entities[i].id == id)
return this._entities[i];
}
return this._entitiesToAdded[id];
}
/**
* 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。
* 返回的List可以通过ListPool.free放回池中