Freeing memory held by visible entities vector (#3009)
- Freeing unused memory held by visible entities - Fixed comment style # Objective With Rust 1.56 it's possible to shrink vectors to a specified capacity. Visibility system had a comment before asking for that feature to free unused memory by a vector if its capacity is two times larger than the length. ## Solution Shrinking the vector of visible entities to the nearest power of 2 elements next to `len()`, if capacity exceeds it more than two times.
This commit is contained in:
		
							parent
							
								
									75403289b2
								
							
						
					
					
						commit
						8b9aa2cceb
					
				@ -1557,6 +1557,22 @@ pub fn check_light_mesh_visibility(
 | 
				
			|||||||
        (Without<NotShadowCaster>, Without<DirectionalLight>),
 | 
					        (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
 | 
					    // Directional lights
 | 
				
			||||||
    for (
 | 
					    for (
 | 
				
			||||||
        directional_light,
 | 
					        directional_light,
 | 
				
			||||||
@ -1598,8 +1614,7 @@ pub fn check_light_mesh_visibility(
 | 
				
			|||||||
            visible_entities.entities.push(entity);
 | 
					            visible_entities.entities.push(entity);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
 | 
					        shrink_entities(&mut visible_entities);
 | 
				
			||||||
        // to prevent holding unneeded memory
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for visible_lights in &visible_point_lights {
 | 
					    for visible_lights in &visible_point_lights {
 | 
				
			||||||
@ -1670,11 +1685,12 @@ pub fn check_light_mesh_visibility(
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
 | 
					                for visible_entities in cubemap_visible_entities.iter_mut() {
 | 
				
			||||||
                // to prevent holding unneeded memory
 | 
					                    shrink_entities(visible_entities);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // spot lights
 | 
					            // Spot lights
 | 
				
			||||||
            if let Ok((point_light, transform, frustum, mut visible_entities, maybe_view_mask)) =
 | 
					            if let Ok((point_light, transform, frustum, mut visible_entities, maybe_view_mask)) =
 | 
				
			||||||
                spot_lights.get_mut(light_entity)
 | 
					                spot_lights.get_mut(light_entity)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
@ -1726,8 +1742,7 @@ pub fn check_light_mesh_visibility(
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // TODO: check for big changes in visible entities len() vs capacity() (ex: 2x) and resize
 | 
					                shrink_entities(&mut visible_entities);
 | 
				
			||||||
                // to prevent holding unneeded memory
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user