playcanvas / playcanvas/engine

ComponentSystems add/beforeremove events issue.

Open
#7,202 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
JavaScript
Stars
16.8k
Forks
2k
Avg merge
4h 32m
Merged PRs (30d)
222

Description

The problem:

ComponentSystem has a very large subscriber lists for add/beforeremove events with mostly irrelevant subscribers. Leading to higher cost of adding/removing components.

Details:

Many ComponentSystems do provide a way to inform when component has been added or is about to be removed, and it provides a single event name for that: add and beforeremove on ComponentSystem. As an argument, it provide an Entity and a Component.
Then every single system's Component, will subscribe to such event, only to check then if then entity is the same as this Component's entity.

This leads to a single callbacks list that has a very large number of subscribers, where only a few of hundreds/thousands of subscribers are relevant.

So then on every added/remove Component, this will lead to wasted time going through irrelevant callbacks and calling them only to return early.
This has also an extra overhead due to high cost of removing event handles from massive arrays. With the introduction of a more efficient EventHandle.off, this can be improved, but this does not solve the original problem.

The solution:

If a subscriber needs to know when it is about to be removed or added, it then should be subscribed and handled on that subscriber (Component) itself. This will avoid large callback lists on a ComponentSystem, and will be way more efficient when adding/removing entities with components.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing ComponentSystem's add and beforeremove event subscriptions, then inspect how components currently receive and filter those events. Done means subscribers handle relevant add/remove notifications on the component itself, avoiding large ComponentSystem callback lists while preserving the existing notification behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
game-dev
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.