perf: micro

This commit is contained in:
cha0s 2024-08-01 13:23:23 -05:00
parent 5d352bb367
commit 05350e6ccf

View File

@ -2,10 +2,9 @@ import Schema from './schema.js';
export default class Component {
data = [];
ecs;
Instance;
map = {};
instances = {};
pool = [];
static properties = {};
static $$schema;
@ -15,19 +14,6 @@ export default class Component {
this.Instance = this.instanceFromSchema();
}
allocateMany(count) {
const results = [];
while (count > 0) {
results.push(
this.pool.length > 0
? this.pool.pop()
: this.data.push(new this.Instance()) - 1,
)
count -= 1;
}
return results;
}
async create(entityId, values) {
const [created] = await this.createMany([[entityId, values]]);
return created;
@ -37,36 +23,28 @@ export default class Component {
if (0 === entries.length) {
return [];
}
const allocated = this.allocateMany(entries.length);
const {properties} = this.constructor.schema.specification.concrete;
const Schema = this.constructor.schema.constructor;
const keys = Object.keys(properties);
let {length} = entries;
const allocated = [];
while (length > 0) {
allocated.push(
this.pool.length > 0
? this.pool.pop()
: new this.Instance(),
)
length -= 1;
}
const promises = [];
for (let i = 0; i < entries.length; ++i) {
const [entityId, values = {}] = entries[i];
this.map[entityId] = allocated[i];
this.data[allocated[i]].entity = entityId;
for (let k = 0; k < keys.length; ++k) {
const j = keys[k];
const instance = this.data[allocated[i]];
if (j in values) {
instance[j] = values[j];
}
else {
const defaultValue = Schema.defaultValue(properties[j]);
if ('undefined' !== typeof defaultValue) {
instance[j] = defaultValue;
}
}
for (const [entityId, values] of entries) {
const instance = allocated.pop();
this.instances[entityId] = instance;
instance.entity = entityId;
for (const key in values) {
instance[key] = values[key];
}
promises.push(this.load(this.data[allocated[i]]));
promises.push(this.load(instance));
}
await Promise.all(promises);
const created = [];
for (let i = 0; i < allocated.length; ++i) {
created.push(this.data[allocated[i]]);
}
return created;
return allocated;
}
deserialize(entityId, view, offset) {
@ -82,19 +60,19 @@ export default class Component {
this.destroyMany([entityId]);
}
destroyMany(entities) {
destroyMany(entityIds) {
this.freeMany(
entities
entityIds
.map((entityId) => {
if ('undefined' !== typeof this.map[entityId]) {
return this.map[entityId];
if ('undefined' !== typeof this.instances[entityId]) {
return this.instances[entityId];
}
throw new Error(`can't free for non-existent id ${entityId}`);
}),
);
for (let i = 0; i < entities.length; i++) {
this.data[this.map[entities[i]]].destroy();
this.map[entities[i]] = undefined;
for (const entityId of entityIds) {
this.instances[entityId].destroy();
delete this.instances[entityId];
}
}
@ -110,14 +88,12 @@ export default class Component {
return json;
}
freeMany(indices) {
for (let i = 0; i < indices.length; ++i) {
this.pool.push(indices[i]);
}
freeMany(instances) {
this.pool.push(...instances);
}
get(entityId) {
return this.data[this.map[entityId]];
return this.instances[entityId];
}
async insertMany(entities) {