Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Freeing memory held by visible entities vector #3009

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions crates/bevy_pbr/src/light.rs
Expand Up @@ -1551,6 +1551,22 @@ pub fn check_light_mesh_visibility(
(Without<NotShadowCaster>, Without<DirectionalLight>),
>,
) {
fn shrink_entities(visible_entities: &mut VisibleEntities) {
// Check that visible entities capacity() is no more than two times greater than len()
let capacity = visible_entities.entities.capacity();
let reserved = capacity
.checked_div(visible_entities.entities.len())
.map_or(0, |reserve| {
if reserve > 2 {
capacity / (reserve / 2)
} else {
capacity
}
});

visible_entities.entities.shrink_to(reserved);
}

// Directional lights
for (
directional_light,
Expand Down Expand Up @@ -1592,8 +1608,7 @@ pub fn check_light_mesh_visibility(
visible_entities.entities.push(entity);
}

// TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
// to prevent holding unneeded memory
shrink_entities(&mut visible_entities);
}

for visible_lights in &visible_point_lights {
Expand Down Expand Up @@ -1664,11 +1679,12 @@ pub fn check_light_mesh_visibility(
}
}

// TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
// to prevent holding unneeded memory
for visible_entities in cubemap_visible_entities.iter_mut() {
shrink_entities(visible_entities);
}
}

// spot lights
// Spot lights
if let Ok((point_light, transform, frustum, mut visible_entities, maybe_view_mask)) =
spot_lights.get_mut(light_entity)
{
Expand Down Expand Up @@ -1720,8 +1736,7 @@ pub fn check_light_mesh_visibility(
}
}

// TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
// to prevent holding unneeded memory
shrink_entities(&mut visible_entities);
}
}
}
Expand Down