minor performance upgrade

This commit is contained in:
Schluffe
2026-04-07 23:32:22 +02:00
parent 03f4119e8c
commit c349dcec29
4 changed files with 46 additions and 34 deletions
+7 -7
View File
@@ -6,7 +6,7 @@ import {
} from "#/engine/Entity.ts"; } from "#/engine/Entity.ts";
import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { type Graph, getNeighbours } from "#/engine/Graph.ts";
import { useWithAsyncCache } from "#/engine/lib/cache.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts";
import MapProxy from "#/engine/lib/MapProxy.ts"; import MapArray from "#/engine/lib/MapArray.ts";
export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void; export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void;
export type PreProcess = () => void; export type PreProcess = () => void;
@@ -24,7 +24,7 @@ export type SceneData = {
entities: Array<EntityData<unknown>>; entities: Array<EntityData<unknown>>;
}; };
export type ComponentsMap = Map<string, MapProxy<string, unknown>>; export type ComponentsMap = Map<string, MapArray<string, unknown>>;
export type Scene = { export type Scene = {
data: SceneData; data: SceneData;
@@ -113,7 +113,7 @@ const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => {
const localEntityProcessMap = new Map<string, EntityProcess>(); const localEntityProcessMap = new Map<string, EntityProcess>();
sceneEntityProcessMap.set(name, localEntityProcessMap); sceneEntityProcessMap.set(name, localEntityProcessMap);
const componentsMap = new Map<string, MapProxy<string, unknown>>(); const componentsMap = new Map<string, MapArray<string, unknown>>();
sceneComponentsMap.set(name, componentsMap); sceneComponentsMap.set(name, componentsMap);
await Promise.all( await Promise.all(
@@ -136,9 +136,9 @@ const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => {
), ),
); );
if (componentsMap.has(key)) { if (componentsMap.has(key)) {
componentsMap.get(key)!.map.set(id, boxedValue); componentsMap.get(key)!.set(id, boxedValue);
} else { } else {
componentsMap.set(key, new MapProxy([[id, boxedValue]])); componentsMap.set(key, new MapArray([[id, boxedValue]]));
} }
} }
}, },
@@ -179,6 +179,6 @@ export async function setScene(name: string): Promise<void> {
} }
} }
export default function <T>(name: string): MapProxy<string, T> { export default function <T>(name: string): MapArray<string, T> {
return currentScene.components.get(name) as MapProxy<string, T>; return currentScene.components.get(name) as MapArray<string, T>;
} }
+37
View File
@@ -0,0 +1,37 @@
import type { Box } from "#/engine/Box.ts";
export default class MapArray<K, V> {
map: Map<K, Box<V>>;
array: Array<[K, Box<V>]>;
constructor(iterable?: Iterable<[K, Box<V>]>) {
this.map = new Map(iterable);
this.array = iterable ? [...iterable] : [];
}
delete(key: K): void {
if (this.map.delete(key)) {
this.array[this.array.findIndex((e) => e[0] === key)] =
this.array[this.array.length - 1];
this.array.pop();
}
}
set(key: K, value: Box<V>): void {
if (this.map.has(key)) {
this.array.find((e) => e[0] === key)![1] = value;
} else {
this.array.push([key, value]);
}
this.map.set(key, value);
}
get(key: K): Box<V> | undefined {
return this.map.get(key);
}
entries(): Array<[K, Box<V>]> {
return this.array;
}
}
-25
View File
@@ -1,25 +0,0 @@
import type { Box } from "#/engine/Box.ts";
export default class MapProxy<K, V> {
map: Map<K, Box<V>>;
constructor(iterable?: Iterable<readonly [K, Box<V>]>) {
this.map = new Map(iterable);
}
get(key: K): Box<V> | undefined {
return this.map.get(key);
}
entries(): MapIterator<[K, Box<V>]> {
return this.map.entries();
}
keys(): MapIterator<K> {
return this.map.keys();
}
values(): MapIterator<Box<V>> {
return this.map.values();
}
}
+2 -2
View File
@@ -8,13 +8,13 @@ sceneChangeSet.add((sceneData) => {
drawables.length = 0; drawables.length = 0;
const sprites = [ const sprites = [
...sceneComponentsMap.get(sceneData.name)!.get("sprite")!.values(), ...sceneComponentsMap.get(sceneData.name)!.get("sprite")!.map.values(),
] as Array<Box<Sprite>>; ] as Array<Box<Sprite>>;
sceneComponentsMap sceneComponentsMap
.get(sceneData.name)! .get(sceneData.name)!
.get("position")! .get("position")!
.values() .map.values()
.forEach((box, index) => { .forEach((box, index) => {
drawables.push({ drawables.push({
position: box as Box<Vector>, position: box as Box<Vector>,