Skip to content

Optimization: Material.meshInstances change to Set - faster removing MeshInstances#8451

Merged
Maksims merged 1 commit intoplaycanvas:mainfrom
Maksims:material-ref-mesh-instances
Feb 12, 2026
Merged

Optimization: Material.meshInstances change to Set - faster removing MeshInstances#8451
Maksims merged 1 commit intoplaycanvas:mainfrom
Maksims:material-ref-mesh-instances

Conversation

@Maksims
Copy link
Copy Markdown
Collaborator

@Maksims Maksims commented Feb 11, 2026

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

  • I have read the contributing guidelines
  • My code follows the project's coding standards
  • This PR focuses on a single change

@Maksims Maksims requested a review from mvaligursky February 11, 2026 15:37
Copy link
Copy Markdown
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

@Maksims Maksims merged commit 42de788 into playcanvas:main Feb 12, 2026
5 of 8 checks passed
@Maksims Maksims deleted the material-ref-mesh-instances branch February 12, 2026 08:50
@Maksims Maksims mentioned this pull request Feb 12, 2026
3 tasks
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.

3 participants