Skip to content

Commit dbad1d4

Browse files
NodeMaterialObserver: Reuse lightsData cache entry per frame.
`getLights` allocated a fresh `{ renderId, lightsData }` object every time `renderId` changed (i.e. once per frame per lightsNode). Mutate the cached entry's fields in place instead, so the cache-miss path doesn't allocate after the first frame. `getLightsData` also allocated a fresh `const lights = []` per call; change the signature to accept the output array so it can be reused. Measured on webgpu_backdrop_water (Inspector stripped, 1 KB sampling): site no longer appears in the top 30 allocators. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2e164b2 commit dbad1d4

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/materials/nodes/manager/NodeMaterialObserver.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class NodeMaterialObserver {
216216

217217
}
218218

219-
data.lights = this.getLightsData( renderObject.lightsNode.getLights() );
219+
data.lights = this.getLightsData( renderObject.lightsNode.getLights(), [] );
220220

221221
this.renderObjects.set( renderObject, data );
222222

@@ -629,9 +629,9 @@ class NodeMaterialObserver {
629629
* @param {Array<Light>} materialLights - The material lights.
630630
* @return {Array<Object>} The lights data for the given material lights.
631631
*/
632-
getLightsData( materialLights ) {
632+
getLightsData( materialLights, lights ) {
633633

634-
const lights = [];
634+
lights.length = 0;
635635

636636
for ( const light of materialLights ) {
637637

@@ -658,23 +658,25 @@ class NodeMaterialObserver {
658658
*/
659659
getLights( lightsNode, renderId ) {
660660

661-
if ( _lightsCache.has( lightsNode ) ) {
661+
let cached = _lightsCache.get( lightsNode );
662662

663-
const cached = _lightsCache.get( lightsNode );
663+
if ( cached !== undefined && cached.renderId === renderId ) {
664664

665-
if ( cached.renderId === renderId ) {
665+
return cached.lightsData;
666666

667-
return cached.lightsData;
667+
}
668668

669-
}
669+
if ( cached === undefined ) {
670670

671-
}
671+
cached = { renderId: - 1, lightsData: [] };
672+
_lightsCache.set( lightsNode, cached );
672673

673-
const lightsData = this.getLightsData( lightsNode.getLights() );
674+
}
674675

675-
_lightsCache.set( lightsNode, { renderId, lightsData } );
676+
cached.renderId = renderId;
677+
this.getLightsData( lightsNode.getLights(), cached.lightsData );
676678

677-
return lightsData;
679+
return cached.lightsData;
678680

679681
}
680682

0 commit comments

Comments
 (0)