Skip to content

gl: keep resources in a Vec by id - #715

Open
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:gl-resources-in-vec
Open

benface wants to merge 1 commit into
not-fl3:masterfrom
benface:gl-resources-in-vec

Conversation

@benface

@benface benface commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Why

ResourceManager keeps the GL backend's shaders, pipelines, passes and buffers in a HashMap<usize, _> with the standard library's hasher, SipHash. Every apply_pipeline, apply_bindings, buffer_update and draw looks resources up there, so each draw call hashes several ids. In a web build of a macroquad game (CHOMP) drawing about 200 draw calls a frame, with Chrome's CPU throttled 4×, RandomState::hash_one took 0.16 ms of a frame and the Index impls around it 0.2 ms more.

Change

Ids are handed out in order and never reused, so they index a Vec<Option<T>> instead, as the Metal backend's resources and the GL backend's textures already do with a Vec. remove takes the resource out and leaves its slot empty, so a deleted resource's id still panics when it's used, rather than reaching another resource.

Verification

  • In that web build, hash_one and ResourceManager's Index no longer show in a CPU profile of the frame.
  • The game's frames through OpenGL on macOS are byte-identical to before, over 235 frames of eight captures.

cargo build has no new warnings, natively or for wasm32-unknown-unknown.

🤖 Generated with Claude Code

`ResourceManager` kept shaders, pipelines, passes and buffers in a
`HashMap<usize, _>` 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 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant