BlueGit

timfa frameworkv3

Persist scene draw-layer buckets across frames.

Reuse per-layer object arrays instead of allocating a fresh byLayer map every draw.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
e982b80cb9ec1ae5766248ca36844f2a372eb6bb
Parent
089bb8ab
1 file changed +12 −3
framework/engine/scene.js +12 −3
@@ -23,6 +23,9 @@ class Scene
23 23 * full in-place sort (cheaper than many splice reinserts on large lists).
24 24 */
25 25 _layerChangeFullSortThreshold = 64;
26 /** Reused per-draw-layer object lists (cleared + refilled each draw). */
27 _byLayer = null;
28 _byLayerNames = null;
26 29
27 30 g_primary = false;
28 31
@@ -513,8 +516,13 @@ class Scene
513 516 this.#ensureDrawOrder();
514 517 const sortedActiveObjects = this._sortedActiveObjects;
515 518
516 // Bucket visible objects once so empty bloom layers can be skipped entirely.
517 const byLayer = Object.create(null);
519 // Reuse per-layer buckets so we do not allocate new arrays every frame.
520 const byLayer = this._byLayer || (this._byLayer = Object.create(null));
521 const byLayerNames = this._byLayerNames || (this._byLayerNames = []);
522 for (let i = 0; i < byLayerNames.length; i++)
523 { byLayer[byLayerNames[i]].length = 0; }
524 byLayerNames.length = 0;
525
518 526 for (let i = 0; i < sortedActiveObjects.length; i++)
519 527 {
520 528 const obj = sortedActiveObjects[i];
@@ -528,7 +536,8 @@ class Scene
528 536 bucket = [];
529 537 byLayer[name] = bucket;
530 538 }
531
539 if (bucket.length === 0)
540 { byLayerNames.push(name); }
532 541 bucket.push(obj);
533 542 }
534 543
@@ -23,6 +23,9 @@ class Scene
23 * full in-place sort (cheaper than many splice reinserts on large lists).
23 * full in-place sort (cheaper than many splice reinserts on large lists).
24 */
24 */
25 _layerChangeFullSortThreshold = 64;
25 _layerChangeFullSortThreshold = 64;
26 /** Reused per-draw-layer object lists (cleared + refilled each draw). */
27 _byLayer = null;
28 _byLayerNames = null;
26
29
27 g_primary = false;
30 g_primary = false;
28
31
@@ -513,8 +516,13 @@ class Scene
513 this.#ensureDrawOrder();
516 this.#ensureDrawOrder();
514 const sortedActiveObjects = this._sortedActiveObjects;
517 const sortedActiveObjects = this._sortedActiveObjects;
515
518
516 // Bucket visible objects once so empty bloom layers can be skipped entirely.
519 // Reuse per-layer buckets so we do not allocate new arrays every frame.
517 const byLayer = Object.create(null);
520 const byLayer = this._byLayer || (this._byLayer = Object.create(null));
521 const byLayerNames = this._byLayerNames || (this._byLayerNames = []);
522 for (let i = 0; i < byLayerNames.length; i++)
523 { byLayer[byLayerNames[i]].length = 0; }
524 byLayerNames.length = 0;
525
518 for (let i = 0; i < sortedActiveObjects.length; i++)
526 for (let i = 0; i < sortedActiveObjects.length; i++)
519 {
527 {
520 const obj = sortedActiveObjects[i];
528 const obj = sortedActiveObjects[i];
@@ -528,7 +536,8 @@ class Scene
528 bucket = [];
536 bucket = [];
529 byLayer[name] = bucket;
537 byLayer[name] = bucket;
530 }
538 }
531
539 if (bucket.length === 0)
540 { byLayerNames.push(name); }
532 bucket.push(obj);
541 bucket.push(obj);
533 }
542 }
534
543