(Case 1420752) RectTransform.rect very expensive

Accessing the .rect property of a RectTransform is very expensive compared to other operations.


On an AMD Ryzen 3900 it costs:

  • Accessing RectTransform.rect 1000 times cost 2ms :hushed:

  • Accessing RectTransform.localToWorldMatrix 1000 times cost 0.6ms

The RectTransform.rect property is used across a lot of Unity UI code. I believe the overall UI performance can be affected positively by optimizing RectTransform.rect.

5 Likes

Is it feedback or can it be considered a bug?

[quote=“Peter77”, post:2, topic: 878597]
Is it feedback or can it be considered a bug?
[/quote]
Does not feel like it would hurt to open a bug ticket. Worst case it just gets closed. Best case a dev sees whether there is actually some low hanging fruit to improve.

It does kinda make sense - the rect property is calculated, while the other properties are just fields. It also (probably) needs to walk the hierarchy and deal with all the garbage that is layout groups and whatnot.

Did this get way worse in 2022?

[quote=“Peter77”, post:2, topic: 878597]
Is it feedback or can it be considered a bug?
[/quote]
I sincerely believe uGUI should never have been shipped. It’s just a very big bug that pretends like it’s a feature.

[quote=“Baste”, post:4, topic: 878597]
Did this get way worse in 2022?
[/quote]
Not really, a little worse in 2022 only. But it could be fluctuation in profiling too, it was about 0.2ms difference between 2019.4 and 2022.2.

I just submitted it with 2022 to increase chances that Unity staff is looking at the report :wink:

1 Like

I’ve had a quick look at the code and indeed it’s not trivial - it walks the parent hierarchy looking for changes. There is nothing obvious to optimise just by reading it. If a bug report got past QA someone could profile it to get a better idea of the hotspots. The UI team may be focussing on UIToolkit these days though… sorry I dont have any good news about it.

2 Likes

uGUI has been basically abandoned since it was moved into a package to open room for UIToolkit. As per usual for anything Unity, development on the old feature stops long before the new one is ready.

1 Like

[quote=“richardkettlewell”, post:6, topic: 878597]
I’ve had a quick look at the code and indeed it’s not trivial
[/quote]
Wow, very cool of you to have looked into it. Thank you!

I wish Unity would still improve a few things in old systems, because we’re still using most of them and will continue to use them even in a few years. Where “we” refers to the company I work. But I understand that you (plural) don’t want to break old systems by working on performance improvements for example.

1 Like

Having the UI project repo available on GitHub would be epic, at least then we could submit PRs for improvements (like removing cases of transform.GetComponent())

1 Like


The problem is that it stops before the old one is ready as well.


I have to say that the regular Particle System is probably one of the most complete things in Unity. You can't really make it do any more that doesn't make sense being done elsewhere. Also a few other bits and bobs feel pretty done to me...

But IMHO UI was never done because one of the chief objectives of a built in UI would be really high performance as that's kind of why you will use a UI vs just slapping a few quads/sprites/text on the screen. In that, I would say UI was never finished.

Doing a brand new UI Toolkit thing seems again something fairly separate from DOTS and I've already decided not to use it, based on past experience and the fact it's far from finished.

Rolling my own UI for my stuff, as I can guarantee the performance and its probably a hundred thousand times easier to use.

2 Likes

Hmm, when does HippoUI Pro hit the githubs or the Asset Store? :)

2 Likes

[quote=“greg-harding”, post:12, topic: 878597]
Hmm, when does HippoUI Pro hit the githubs or the Asset Store? :slight_smile:
[/quote]
Haha I will never sell on asset stores or anything like that. What I mean is in my past games and my current even, I just normalise where the screen edges are and snap 3D objects there like TextMeshPro objects, or health bars, that sort of thing. It’s really no different to a game problem like a 2D game. I got the habit of UI like that from @MelvMay many years back when having fun with Torque2D.

Time flies but evidently UI’s don’t.

2 Likes

[quote=“richardkettlewell”, post:6, topic: 878597]
If a bug report got past QA
[/quote]
It actually just did get past QA. Thank you to everybody involved \o/
https://issuetracker.unity3d.com/issues/getting-recttransform-dot-rect-is-expensive

5 Likes