Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Synopsis
get_scene
is called in quite a few places. The most notable place where it gets called isget_frame_time
.It has been discovered, that calling
get_scene
is quite costly. Which by transitivity makesget_frame_time
costly.This is because in
get_scene
,SCENE.get_or_insert(Scene::new())
is used. This causes a temporaryScene
struct to be constructed each time theget_scene
call is made as Rust eagerly evaluates function arguments.This is a problem, because
Scene::new()
callsArena::new()
.Arena::new()
callsvec![Vec::with_capacity(ARENA_BLOCK)]
(aka allocates memory). This essentially causesget_scene
(andget_frame_time
) to constantly allocate and deallocate memory.The Fix
The fix is to simply replace
get_or_insert
withget_or_insert_with
, which allowsScene::new()
to be lazily computed.