Skip to content

Commit d1d4027

Browse files
committed
avoid unnecessary allocation for visible_lights
1 parent 5de55f3 commit d1d4027

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

crates/bevy_pbr/src/light.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -964,16 +964,7 @@ pub(crate) fn assign_lights_to_clusters(
964964
.lights
965965
.resize_with(clusters.aabbs.len(), VisiblePointLights::default);
966966

967-
let mut visible_lights_scratch = Vec::new();
968-
969-
{
970-
// reuse existing visible lights Vec, if it exists
971-
let visible_lights = if let Some(visible_lights) = visible_lights.as_mut() {
972-
visible_lights.entities.clear();
973-
&mut visible_lights.entities
974-
} else {
975-
&mut visible_lights_scratch
976-
};
967+
let mut update_from_light_intersections = |visible_lights: &mut Vec<Entity>| {
977968
for light in lights.iter() {
978969
let light_sphere = Sphere {
979970
center: Vec3A::from(light.translation),
@@ -1029,12 +1020,18 @@ pub(crate) fn assign_lights_to_clusters(
10291020
}
10301021
}
10311022
}
1032-
}
1023+
};
10331024

1034-
if visible_lights.is_none() {
1035-
commands.entity(view_entity).insert(VisiblePointLights {
1036-
entities: visible_lights_scratch,
1037-
});
1025+
// reuse existing visible lights Vec, if it exists
1026+
if let Some(visible_lights) = visible_lights.as_mut() {
1027+
visible_lights.entities.clear();
1028+
update_from_light_intersections(&mut visible_lights.entities);
1029+
} else {
1030+
let mut entities = Vec::new();
1031+
update_from_light_intersections(&mut entities);
1032+
commands
1033+
.entity(view_entity)
1034+
.insert(VisiblePointLights { entities });
10381035
}
10391036
}
10401037
}

0 commit comments

Comments
 (0)