From 3b26517370ed9fbf9cf28bf9c9babe305703dfbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Fri, 27 Nov 2026 11:36:01 -0500 Subject: [PATCH] gl: keep resources in a Vec by id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `ResourceManager` kept shaders, pipelines, passes and buffers in a `HashMap` with the standard library's SipHash, so every `apply_pipeline`, `apply_bindings`, `buffer_update` and `draw` hashed the ids it looked up. Ids are handed out in order and never reused, so they can index a `Vec` instead, as the Metal backend's resources and the GL textures already do. A deleted resource leaves its slot empty, so using its id still panics rather than reaching another resource. In the web build, throttled 4× in Chrome, with 200 draw calls a frame, the hashing took 0.16 ms of the frame and the lookups 0.2 ms; neither shows in a profile now. Co-Authored-By: Claude Opus 5.5 Co-authored-by: multica-agent --- src/lib.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 719b2c676..3ea01e951 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,6 @@ mod event; pub mod fs; pub mod graphics; pub mod native; -use std::collections::HashMap; use std::ops::{Index, IndexMut}; #[cfg(feature = "log-impl")] @@ -26,44 +25,44 @@ mod default_icon; pub use native::gl; +/// Resources by id, an id being its resource's index. A deleted resource's +/// id isn't handed out again, so using it still panics rather than reaching +/// another resource. #[derive(Clone)] pub(crate) struct ResourceManager { - id: usize, - resources: HashMap, + resources: Vec>, } impl Default for ResourceManager { fn default() -> Self { Self { - id: 0, - resources: HashMap::new(), + resources: Vec::new(), } } } impl ResourceManager { pub fn add(&mut self, resource: T) -> usize { - self.resources.insert(self.id, resource); - self.id += 1; - self.id - 1 + self.resources.push(Some(resource)); + self.resources.len() - 1 } pub fn remove(&mut self, id: usize) -> T { // Let it crash if the resource is not found - self.resources.remove(&id).unwrap() + self.resources[id].take().unwrap() } } impl Index for ResourceManager { type Output = T; fn index(&self, index: usize) -> &Self::Output { - &self.resources[&index] + self.resources[index].as_ref().unwrap() } } impl IndexMut for ResourceManager { fn index_mut(&mut self, index: usize) -> &mut Self::Output { - self.resources.get_mut(&index).unwrap() + self.resources[index].as_mut().unwrap() } }