From 60d316fa717d5c668a6781d08ed9601f296e5749 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Sat, 4 Jul 2026 20:52:22 +0300 Subject: [PATCH] fixed uncontrollable growth of dynamic surface cache --- fyrox-impl/src/renderer/cache/mod.rs | 26 ++++++++++++++++++++++---- fyrox-impl/src/renderer/mod.rs | 1 + 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/fyrox-impl/src/renderer/cache/mod.rs b/fyrox-impl/src/renderer/cache/mod.rs index de78f51b3..f57515e25 100644 --- a/fyrox-impl/src/renderer/cache/mod.rs +++ b/fyrox-impl/src/renderer/cache/mod.rs @@ -29,6 +29,7 @@ use crate::{ }, }; use fxhash::FxHashMap; +use fyrox_resource::entry::TimedEntry; use fyrox_resource::untyped::ResourceKind; use std::{ ops::{Deref, DerefMut}, @@ -223,10 +224,12 @@ impl TemporaryCache { /// as 2D sprites, tile maps, etc. #[derive(Default)] pub struct DynamicSurfaceCache { - cache: FxHashMap, + cache: FxHashMap>, } impl DynamicSurfaceCache { + const TTL: f32 = 1.0; + /// Creates a new empty cache. pub fn new() -> Self { Self::default() @@ -239,8 +242,9 @@ impl DynamicSurfaceCache { unique_id: u64, layout: &[VertexAttributeDescriptor], ) -> SurfaceResource { - if let Some(surface) = self.cache.get(&unique_id) { - surface.clone() + if let Some(surface) = self.cache.get_mut(&unique_id) { + surface.time_to_live = Self::TTL; + surface.value.clone() } else { let default_capacity = 4096; @@ -261,7 +265,13 @@ impl DynamicSurfaceCache { SurfaceData::new(vertex_buffer, triangle_buffer), ); - self.cache.insert(unique_id, surface.clone()); + self.cache.insert( + unique_id, + TimedEntry { + value: surface.clone(), + time_to_live: Self::TTL, + }, + ); surface } @@ -275,4 +285,12 @@ impl DynamicSurfaceCache { surface_data.geometry_buffer.modify().clear(); } } + + /// Updates the cache entries and removes ones that wasn't used for some time. + pub fn update(&mut self, dt: f32) { + self.cache.retain(|_, entry| { + entry.time_to_live -= dt; + entry.time_to_live > 0.0 + }) + } } diff --git a/fyrox-impl/src/renderer/mod.rs b/fyrox-impl/src/renderer/mod.rs index 7a707a24e..f3e645a72 100644 --- a/fyrox-impl/src/renderer/mod.rs +++ b/fyrox-impl/src/renderer/mod.rs @@ -924,6 +924,7 @@ impl Renderer { self.update_texture_cache(resource_manager, dt); self.update_shader_cache(dt); self.geometry_cache.update(dt); + self.dynamic_surface_cache.update(dt); } fn render_scene_observer(