Search Unity

Optimizing Garbage Collection on TextInfo Constructor

Discussion in 'UGUI & TextMesh Pro' started by LorenzoNuvoletta, Jun 1, 2019.

  1. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    Is there any solution to optimize the GC on TMP_TextInfo..ctor() ?

    upload_2019-6-1_12-48-6.png
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Not really as this is when the allocations for the text and geometry occurs.

    I will be able to make some improvements but some allocations is unavoidable.
     
  3. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    Thank you Stephan,
    I did a test with 40 UI Texts, measuring them when they are enabled and disabled.

    As you would expect the Instantiate.Awake call did some work when they were Enabled, and no work when they were Disabled. Although the Instantiate.Produce and Instantiate.Copy remained identical.

    Could it be possible to move some of those .ctor() Instantiations to Awake or Start? So that only the smallest possible amount of GC is allocated if a Text is not visible yet.

    upload_2019-6-2_12-30-36.png
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Make sure you are testing in a build as there are potential allocations that occur in the Editor.

    Allocations should only occur in Awake or when the object is instantiated. No allocations should occur as a result of Enabling or Disabling text objects themselves.

    Note that when the amount of text grows beyond the initial allocations, some new allocations will occur to handle these additional characters and related geometry. These are allocated in blocks "power of two" up to a certain point.

    So if the text object is instantiated with no text (default allocations) and then enabled later while at the same time changing the text where it would require increasing the allocated buffers, you would see allocations in OnEnable but only because the text grew.

    Please provide your sample test scene so I can take a look to make sure I am seeing the same thing on my end.

    Like I said, I do plan on making improvements there and trying to reduce allocations related to the TextInfo and wherever else possible. I don't have any time frame yet on when I will be able to look at this.
     
  5. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    Hi Stephane,
    attaching the Test Project here. I was testing on both Editor and also on a Mac build connected to profiler, but I noticed very similar numbers.

    To clarify, the test was: Instantiating Enabled Texts, and the other test was: Instantiating Disabled Texts.

    If I understood your last message, you might be able to add some improvements to TextInfo in a future release, but the ETA is still unknown.
     

    Attached Files:

  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Thanks for the attached project. I'll take a look later tonight / over the weekend.

    In terms of instantiations, whether the text object is enabled or not still results in Awake() being called where the initial allocations for the buffers and TextInfo still occurs. One way to shift these allocations around might be to instantiate these object earlier maybe when a scene is loaded / less mission critical time and then disabling them and if needed even use a pool of text objects that are getting recycled.

    Correct. The rework of the TextInfo will come with revised parser / layout and TextGenerator. But before getting to that, I want to add support for some of the OpenType features as well as ability to create font assets from native fonts and rasterizing colored glyphs / emojis from local OS fonts on devices.

    Again, having said that, if there are simple / quick tweaks I can make to improve / reduce allocations here, I will. Just can't get to the major stuff related to this yet.
     
  7. LorenzoNuvoletta

    LorenzoNuvoletta

    Joined:
    Apr 28, 2014
    Posts:
    54
    Makes sense, thank you for your help.
    I will look into creating a Text Pool where possible.