Search Unity

TextMesh Pro TextMeshProUGUI.Rebuild performance on Mobile

Discussion in 'UGUI & TextMesh Pro' started by Peter77, Feb 28, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I've a simple HUD that displays Rank, Time, Lap, Speed and Score. Most (can) change per frame, so there is no clever caching.

    upload_2020-2-28_15-53-48.png

    I've noticed
    TextMeshProUGUI.Rebuild
    takes a significant amount of time. Reproducing it in an isolated project (attached to post) that only contains these five text elements costs about 1ms when running it on a Samsung Galaxy S6, which isn't a slow device actually.

    I'm using Unity 2018.4.17f1 and TMP 1.4.1.

    Is there a to way make this faster?

    Test project running on the device. Cost is basically SendWillRenderCanvases+NewBehaviourScript:
    upload_2020-2-28_15-57-11.png

    Deep Profile when running in the editor:
    upload_2020-2-28_15-57-30.png
     

    Attached Files:

  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Thank you for providing the repro project.

    I will take a look and provide feedback as soon as I have more information to share.
     
    Peter77 likes this.
  3. prometeoproduction

    prometeoproduction

    Joined:
    Apr 19, 2019
    Posts:
    17
    I have the same performance issue with TextMeshProUGUI.Rebuild.

    There is an update?
     
  4. cLick1338

    cLick1338

    Joined:
    Feb 23, 2017
    Posts:
    74
    Isn't this of concern only when deep profiling is enabled (especially on a mobile build)?
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Mostly. Deep Profile numbers are substantially slower than release code, typically by more than 10x.

    Deep Profile is good to get a sense of the relative performance of code making it easier to identify performance sensitive areas.

    The is also Editor overhead combined with code running in the Editor not being optimized. Everything is slower in the Editor. You have also to factor code that may only run in the Editor. For instance, TMP has an event system to track assets and inspector related stuff where none of that code / overhead is present in release builds.

    Having said all of the above, I will still be looking at this as for instance "TextMeshProUGUI.Rebuild() with 10 calls points to perhaps 10 text objects in the scene or the use of text auto-size which is expensive and sadly something that many users turn on by default which is a bad idea.
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I posted two profiler screenshots. The 1st one running on a device without deep profile, the 2nd one running in the editor with deep profile.

    The performance issue exists without deep profile. The deep profile screenshot was just a service from my side to make it easier for Stephan to see if it's related to TMP.


    The provided test project (see first post) does not make use of text auto-size.

    I'm still crossing fingers Stephan can tell me how I can significantly reduce the cost of updating those 5 texts. It's hard to grasp why that would be so expensive.
     
    GuirieSanchez likes this.
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Looking at your project.

    Will provide an update as soon as I have more information which will likely be tomorrow.
     
    Peter77 likes this.
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Quick Update
    I am getting 0.61 ms on a Galaxy Tab A in developer build using the latest 2018.4 with Preview 11. In a release build, we would likely be below 0.2 or 0.1 ms.

    The TMP_Text.SetText is not as efficient as the TMP_Text.text property mostly due to the formatting handling. It should be more efficient than string.format but that is something to keep in mind.

    The example code is also using lots of modulus operator which on some mobile device can impact performance.

    P.S. Just for fun test the same number of text object and about the same amount of text without SetText and modulus.

    P.S.S. Will look tomorrow at the 10 calls on Rebuild() as that seems wrong given we have 5 text objects.
     
    Peter77 likes this.
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Did you have a chance to look at it yet?
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The calls were correct.

    Preview 12 is now available. So please re-test with that version. Again be mindful that profiling in the Editor is substantially slower than in a release build. As such, you cannot rely on those results other than to get a relative sense of where performance overhead / bottlenecks might be coming from.
     
  11. ProgrammingWhileSleeping

    ProgrammingWhileSleeping

    Joined:
    Nov 10, 2017
    Posts:
    17
    Hi, I possibly have the same problem. I have 30+ cars and each of them have their own canvas and TMProUGUI instance to display their name. This is the Profiler's report.

    upload_2020-7-12_18-44-13.png
     
  12. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Having lots of Canvases in World Space will kill performance. I would recommend switching over to using normal <TextMeshPro> objects without a Canvas. The scaling will be different and all but the performance should be much better.

    For instance, in the TMP Examples & Extras, there is a scene called Benchmark Floating Text, I tried to produce an example using Canvas but the performance was so bad that it was pointless to do so.
     
  13. ProgrammingWhileSleeping

    ProgrammingWhileSleeping

    Joined:
    Nov 10, 2017
    Posts:
    17
    Hi Stephan and thank you again. I see, if I change to normal TMP object, is there a function where the text will still face the camera every time? Awesome support here!
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Take a look at the example Benchmark Floating Text and related scripted included in the TMP Examples & Extras. You will notice the floating text always turn to face the camera.
     
  15. ProgrammingWhileSleeping

    ProgrammingWhileSleeping

    Joined:
    Nov 10, 2017
    Posts:
    17
    Thanks, I've now implemented the functionality thank you for your awesome support Stephan! Cheers!
     
    Stephan_B likes this.
  16. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Thanks for your help.

    I've re-tested with TMP 1.5.1. I've also updated the test-code, removed the modulus operations as you suggested. The updated test project is attached to this post. However, it continues to be slow for updating 5 text labels.

    Updating the text labels cost about 1.3ms in a development build on a Samsung Galaxy S6. You wrote it should be below 0.2ms in a release build. How can I measure that this assertion is true, when I need the development mode flag to be enabled, to actually profile the code?

    Here is the profiler capture running on a Samsung Galaxy S6:
    upload_2020-8-13_20-9-34.png


    Here follows a deep profile from running inside the editor, just to show "Layout" in above screenshot contains TextMeshPro code. I'm not trying to say it's slow with deep profile, let's ignore the numbers here.

    upload_2020-8-13_20-11-35.png

    Thanks for your help!
     

    Attached Files:

    GuirieSanchez likes this.
  17. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I will take a look at the project sample you provided.

    In the meantime, see the following post which should provide answers to some of your newer questions.
     
    Peter77 likes this.