Skip to content

Commit a3106f0

Browse files
authored
Micro optimisations & perf updates (#557)
Work in progress but this PR does: * Reduce branching in hot paths (or attempt to clean up) * Reduce look ups in variables, const them out earlier * Reduce assert fn calls where we no longer need them * Deduplicate a lot of code Avoiding Text paths for now, as that's part of another change. Big gains on node creation / node deletion, still investigating the update/swap cases.
2 parents c684a64 + cdd25bc commit a3106f0

File tree

9 files changed

+398
-413
lines changed

9 files changed

+398
-413
lines changed

examples/tests/texture-memory-allocation.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ export default async function test({ renderer, testRoot }: ExampleSettings) {
103103
const gap = 10; // Define the gap between items
104104

105105
const spawnRow = function (rowIndex = 0, amount = 20) {
106-
const items = [];
107-
108106
let totalWidth = 0; // Track the total width used in the current row
109107
const y = rowIndex * (nodeHeight + gap);
110108

@@ -115,6 +113,7 @@ export default async function test({ renderer, testRoot }: ExampleSettings) {
115113
height: nodeHeight,
116114
parent: containerNode,
117115
color: 0x000000ff,
116+
clipping: true,
118117
});
119118

120119
for (let i = 0; i < amount; i++) {
@@ -153,17 +152,47 @@ export default async function test({ renderer, testRoot }: ExampleSettings) {
153152
autosize: true,
154153
parent: childNode,
155154
text: `Card ${id}`,
155+
fontFamily: 'Ubuntu',
156156
fontSize: 20,
157157
color: 0xffffffff,
158158
});
159159

160-
items.push(childNode);
160+
// items.push(childNode);
161161
}
162162

163-
return items;
163+
return rowNode;
164+
};
165+
166+
const destroyAll = () => {
167+
// Destroy all nodes
168+
while (nodes.length > 0) {
169+
const node = nodes.pop();
170+
if (node) {
171+
node.destroy();
172+
}
173+
}
164174
};
165175

166-
const nodes: INode[][] = [];
176+
const runRoutine = (count = 150) => {
177+
count--;
178+
179+
if (count < 0) {
180+
return;
181+
}
182+
183+
// Destroy all nodes
184+
destroyAll();
185+
186+
setTimeout(async () => {
187+
await spawnRows(20);
188+
189+
setTimeout(() => {
190+
runRoutine(count);
191+
}, 300);
192+
}, 300);
193+
};
194+
195+
const nodes: INode[] = [];
167196
const spawnRows = async (amountOfRows: number) => {
168197
for (let rowIndex = 0; rowIndex < amountOfRows; rowIndex++) {
169198
console.log(`Spawning row ${rowIndex + 1}`);
@@ -194,16 +223,27 @@ export default async function test({ renderer, testRoot }: ExampleSettings) {
194223
containerNode.y -= 50;
195224
}
196225

226+
if (e.key === 'c') {
227+
spawnRows(20);
228+
}
229+
230+
if (e.key === 'd') {
231+
// destroy all nodes
232+
destroyAll();
233+
}
234+
235+
if (e.key === 'r') {
236+
runRoutine(150);
237+
}
238+
197239
// space switches mode
198240
if (e.key === ' ' || e.key === 'Enter') {
199241
testMode = testMode === 'normal' ? 'rtt' : 'normal';
200242

201243
currentModeText.text = `Current mode: ${testMode}`;
202244

203245
nodes.forEach((row) => {
204-
row.forEach((node) => {
205-
node.destroy();
206-
});
246+
row.destroy();
207247
});
208248

209249
spawnRows(20);

0 commit comments

Comments
 (0)