Optimization: Material.meshInstances change to Set - faster removing MeshInstances#8451
Merged
Maksims merged 1 commit intoplaycanvas:mainfrom Feb 12, 2026
Merged
Conversation
willeastcott
approved these changes
Feb 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Material stores a referenced list of meshInstances, to be updated when material might change blending, or gets destroyed.
It is private list (not public API). Currently it uses an array, and when compared to Sets they have cons and pros.
Based on many applications tested, the most referenced part is when adding/removing meshInstances, either through creation of many entities, or re-assigning materials.
In small number of mesh instances per material, the impact of this list is neglectable, but when some materials used on thousands of meshes, this leads to pretty slow removal.
This PR, changes array to set. Through tests the effect on iterators is neglectable as there is no hot code (does not iterate every frame). Adding to Set is tiny bit more expensive then pushing into an array. But deleting from Set is way faster O(1) vs indexOf + splice of array O(n).
When add/remove stats combined, this leads to ~26% speed up in this particular code (in one of our projects this is significant time).
Checklist