Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ButtonComponent remove EntityReference & optimize add/remove speed. #7204

Conversation

Maksims
Copy link
Collaborator

@Maksims Maksims commented Dec 16, 2024

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.

@Maksims Maksims changed the title ButtonComponent remove EntityReference ButtonComponent remove EntityReference & optimize add/remove speed. Dec 16, 2024
@mvaligursky mvaligursky added performance Relating to load times or frame rate area: ui UI related issue labels Jan 6, 2025

if (this._imageReference.hasComponent('element')) {
this._isApplyingSprite = true;
spriteFrame = spriteFrame || 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be just a default value for the parameter?

_applySprite(spriteAsset, spriteFrame = 0) {

Copy link
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.

Looks good to me.

@mvaligursky mvaligursky merged commit 120076a into playcanvas:main_v1 Jan 6, 2025
8 checks passed
mvaligursky pushed a commit that referenced this pull request Jan 6, 2025
…7204)

* ButtonComponent remove EntityReference

* remove reliance on component system global event lists

* lint
# Conflicts:
#	src/framework/components/button/component.js
@mvaligursky
Copy link
Contributor

cherry picked to main_v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: ui UI related issue performance Relating to load times or frame rate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants