ButtonComponent remove EntityReference & optimize add/remove speed. #7204
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.
Related to #7195
This PR does not change any APIs.
This PR removes the dependency on EntityReference by ButtonComponent. Instead, it manages its own events by subscribing and unsubscribing to imageEntity's ElementComponent's life and its relevant properties.
Also, it removes reliance on ComponentSystem's global event lists (described here: #7202), to ensure it does not get slower exponentially with the number of Button and Element Components in the scene.
Due to improvements for global event lists on ElementComponentSystem, Element and similar UI components also benefit from this PR. Similar PR for them will come later to keep each PR manageable and narrow.
We had a few projects, which are very UI heavy, with inventory buttons, a lot of UI elements, dynamically created icons all over and so on. We've noticed that the time it takes to modify hierarchy in runtime grows significantly as the project scales in size. This PR addresses some of that.
Here are some tests:
Create a number of entities with button and element components and then remove them.
Current engine:
Quantity: 777
Add: ~145ms (avg. per 1k: ~187ms)
Remove: ~87ms (avg. per 1k: ~112ms)
Average time per 1,000:
Quantity: 2840
Add: ~877ms (avg. per 1k: ~309ms)
Remove: ~839ms (avg. per 1k: ~295ms)
With this PR:
Quantity: 777
Add: ~114ms (avg. per 1k: ~147ms)
Remove: ~25ms (avg. per 1k: ~32ms)
Quantity: 2840
Add: ~286ms (avg. per 1k: ~101ms)
Remove: ~93ms (avg. per 1k: ~33ms)
Results:
From the tests, you can see that the cost of creating an Entity with Button and Element components grows exponentially (3rd test shows) with a number of similar Entities already in the scene.
And with this PR this is not the case, and the cost is constant.
The speed benefits are (it goes higher the more similar entities are in the scene):
Creation speed ~21%-67%+ improvement.
Deletion speed ~71%-89%+ improvement.
I confirm I have read the contributing guidelines and signed the Contributor License Agreement.