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

Reduce the cost of get_scene #811

Merged
merged 1 commit into from
Sep 15, 2024
Merged

Conversation

InnocentusLime
Copy link
Contributor

Synopsis

get_scene is called in quite a few places. The most notable place where it gets called is get_frame_time.

It has been discovered, that calling get_scene is quite costly. Which by transitivity makes get_frame_time costly.

This is because in get_scene, SCENE.get_or_insert(Scene::new()) is used. This causes a temporary Scene struct to be constructed each time the get_scene call is made as Rust eagerly evaluates function arguments.

This is a problem, because Scene::new() calls Arena::new(). Arena::new() calls vec![Vec::with_capacity(ARENA_BLOCK)] (aka allocates memory). This essentially causes get_scene (and get_frame_time) to constantly allocate and deallocate memory.

The Fix

The fix is to simply replace get_or_insert with get_or_insert_with, which allows Scene::new() to be lazily computed.

@not-fl3 not-fl3 merged commit 68e436d into not-fl3:master Sep 15, 2024
6 checks passed
@InnocentusLime InnocentusLime deleted the remove-tls branch September 15, 2024 14:30
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.

2 participants