Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[5.2] Terrible performance spike causing by Font cache

Discussion in 'UGUI & TextMesh Pro' started by BenouKat, Sep 9, 2015.

  1. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    [Unity 5.2.3p2 seems to fix the problem, thanks Unity !]

    Hi again, I'm continue to port my project in Unity 5.2

    I notice a terrible game freeze when open my UIs, due to "Font.CacheFontForText", which taking 3273ms (!), vs nothing in Unity 5.1.3. My UI doesn't contain a ton of text so... What's going on ? Does something change about using Text on UI in term of best pratices ?

     
    Last edited: Dec 8, 2015
    MrEsquire likes this.
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    Are you using best fit?
     
  3. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Yes, almost everywhere, our UIs are responsive.

    Is there a bug about this right now ?
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    I think their might be yes. I think it generating too many font sizes where its not suppose to.
     
  5. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hmm okay, thanks for the answer.

    It's very blocking us from releasing right now, when can we expect a patch on this ?

    Thanks in advance
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    i'm not sure when a patch would be ready as i'm not the one doing the font changes. I'll do my best to poke the guy responsible.
     
  7. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Thanks !
     
  8. adamt

    adamt

    Joined:
    Apr 1, 2014
    Posts:
    115
    I'm experiencing what seems to be the same or a similar issue. Spawning my UI can take anywhere from 100ms to 725ms, technically, but Unity Editor hangs for a few seconds during that time (I get an OSX beachball in the more extreme cases) and the profiler doesn't seem to report the seconds-long hitch. The UI panel I spawned that took 725ms just now doesn't have any Text components with the Best Fit flag checked. It has two ScrollRect components with 3 buttons each, and those buttons each have two child Text components. It's hardly a complex UI, but since upgrading to 5.2, Unity can't handle it.
     
    super77gg, BenouKat and MrEsquire like this.
  9. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Do you get those spikes only in editor ? Because it was (worse) on Android too for us.
     
  10. adamt

    adamt

    Joined:
    Apr 1, 2014
    Posts:
    115
    I haven't seen them in iOS, fortunately, but I haven't tried our game on Android yet.
     
  11. JakeUken

    JakeUken

    Joined:
    Apr 15, 2014
    Posts:
    2
    We're having the same problem. Huge lag spikes from Font.InvokeTextureRebuilt_Internal() not seen in 5.1. We don't use "Best Fit" anywhere, and even normalize font sizes to 18, 24, 32, 64 etc. to minimize the font texture size.
     

    Attached Files:

  12. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Glad I'm not alone :) We can't use 5.2 with this bug, we revert to 5.1.3 for now

    Any updates about this ?

    Thanks a lot
     
  13. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I have the same issue, when I SetActive(true) a panel that has Best Fit enabled on just 6 Text components, it takes ~1000ms, whereas a similar panel without Best Fit enabled is instant.
     
  14. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
  15. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
  16. chrismarch

    chrismarch

    Joined:
    Jul 24, 2013
    Posts:
    393
    I don't see an issue tracker for this. Has anyone filed a bug?
     
  17. Gizmoi

    Gizmoi

    Joined:
    Jan 9, 2013
    Posts:
    327
    I couldn't create a repro project and my main project is private and too big. Sorry.
     
  18. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Ditto.
     
  19. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hi, this bug is happening again on 5.2.1p3

    We are very afraid that when the time will force us to move on 5.2 (because iOS update or anything unsupported in 5.1) the game will be unplayable.

    The FontCache is still taking so much time, and my popups freeze at least 3 or 4 seconds on phone on 5.2, when it's instant in 5.1.
    I'm terribly disappointed that after 6 or 7 patches, the problem is still here, and it's huge. I think I will upload to you our 3Gb project, it's my last solution.

    Thanks a lot.
     
  20. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hi again,

    We sent the whole studio project, issue bug #735927 on fogBuzz. I may be forget to mention it in the bug but you'll need to play a bit (at least the tutorial) to open some menus, and you may need to build on Android to see the freeze.

    And you can use our project to test everything you want, I think this is important for you to test your bugs on a big project and well, we share ours to you, you can do every test you want, even not related to my case.

    Hope you'll find this post and our project, if you see that the cause is a bad practices, feel free to contact me or post in this thread. Thanks !
     
  21. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Sorry for the third bump but I have more news :

    First of all I downloaded Unity 5.2.1p4 and it still happen (im not very surprised), plus I have a new screenshot :

    As you can see, the garbage collection problem as been resolved (300Kb here, 300Mb in my first post), but not the process time. I'm still just open an UI in my game here. I'm not Unity engineer but I THINK that 9000 calls of CacheFontForText is maybe too much :p

    I updated my bug on fogbuzz too. If you have a quick solution, my ears are open.

    Thanks a lot !

     
  22. NoseKills

    NoseKills

    Joined:
    Jun 4, 2013
    Posts:
    25
    Same issue here after updating to 5.2.x.
    There's an old bug report for a similar issue, and seems it has still been used to track this (?) problem since reading the comments it looks like it was marked solved in 5.1 and now the issue is active again. It's not exactly the same problem i guess and why it is "Active" is a bit unclear but I guess that gives us hope. I guess voting on it doesn't hurt
     
    Last edited: Oct 15, 2015
  23. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    I voted on it, i'm not sure it's the same issue but we can try.

    phil said something about best fit and I think the problem is really the best fit generating too much font in cache or something like that.
     
  24. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Any luck with 5.2.1p4? (haven't downloaded it yet)
     
  25. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    Yea the issue was with BestFit as it was generating the character glyphs for every size while trying to find the proper size. I'll pass this thread onto the guy who knows more though.
     
  26. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    This is not something that we've been able to reproduce internally, so I'm very interested in finding out specifics about your projects in which the performance problem appears. We have an improvement to the way that best fit works coming in 5.3, but this addresses behavior that has been around since 4.6, not something that was introduced in 5.2; essentially it prevents best fit from polluting the font atlas with all the intermediate sizes used when searching for optimal font size, and while it might help/mitigate the problem, it doesn't directly address it.

    Questions for those of you experiencing the performance issue:
    1) The profiler graph above seems to indicate an unusually high number of recursive calls to Font.CacheFontForText as a result of Font.InvokeTextureRebuilt_Internal(). Do others see the same thing?
    2) Can you post a link to a bug report with a project?
    3) Anything else you can tell us about the particular way you are using text that might help isolate the problem? Lots of different fonts? Text in layout groups? Custom UI components? I'm just grasping at straws, but hopefully we can figure out the cause.
     
  27. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    One additional question: Does this performance problem appear right away, or only after running for some time and enabling/disabling a lot of UI components?
     
  28. movra

    movra

    Joined:
    Feb 16, 2013
    Posts:
    571
    I haven't seen the 9000 recursive calls, but my projects also suffer from hickups due to font caching. Perhaps I can offer some insight. Or maybe it's expected behavior, then you may remove this post.

    In the Editor the fonts are cached. So when you play a scene in the editor the profiler won't pickup all delays you would see in a build.

    I did a little test with the Sherlock example from the open-source Fungus visual novel framework. It uses the Exo2-Regular.ttf font.

    Here I highlight the spike of the first dialog when played in the Unity 5.3.0b2 editor:

    upload_2015-10-15_22-5-41.png

    And here is the moment with a standalone build on Windows 7:

    upload_2015-10-15_22-5-52.png

    There are now 2 spikes: the blue spike happens at the same moment as in the editor. The green spike is new, comes a fraction later and takes 143 ms. That will cause a visual hickup when you have

    Interestingly, when you set the dialog's Font Style to italic, the 2 spikes will converge.

    upload_2015-10-15_22-15-8.png
     
  29. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    So it seems like we have a fix now along with some other fixes for things that seemed wrong. Thanks guys :).
     
  30. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Nope, still here.

    Hi pfreese,

    I posted the whole 3Gb studio project on Unity report, the bug issue is here : https://fogbugz.unity3d.com/default.asp?735927_hg9p22mkgnqocros I hope you guys has received it.
    If you have trouble to run the project, feel free to contact me here or by mail.

    It's a complete game so you may have to play a bit, but most of our UIs are freezing when opening. Thanks a lot for your post, I'm glad you are aware of this problem.
    For us we use only 2 or 3 different font, we use shadow and outline on most of them, and best fit on most Text component. No custom UI component so far. We are using text in many scrollRect with layout element component yes.
    For your last question : It doesn't appears right away everytime, sometimes I need to open 2 or 3 UIs to see the bug coming. And some popups aren't affected, or in very small way.

    And maybe it's here since 4.6 but AGAIN, everythings works fine in 5.1.3p3.

    Hope it helps :)
     
    Last edited: Oct 16, 2015
  31. NoseKills

    NoseKills

    Joined:
    Jun 4, 2013
    Posts:
    25
    Sweet! Please let us know if there's any info whether this will be 5.3 only or also 5.2.2 / 1p5
     
  32. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,172
    Well for sure 5.3 but maybe we can get it into a patch of 5.2.2
     
  33. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hi guys,

    Sorry to insist but I'm afraid that the bug reported by movra isn't like my case (at all), and my case has been ignored (cause you find a fix before I explain).

    Can you confirm that my problem can be on the "things that seemed wrong" ? It would reassure me a lot :)

    Thanks a lot for your work Uniteam !
     
  34. pfreese

    pfreese

    Unity Technologies

    Joined:
    Feb 23, 2015
    Posts:
    85
    There will always be some performance overhead in caching dynamic fonts for glyphs that have not yet been used; each glyph bitmap must be generated and that bitmap uploaded to a texture. The perf hits that movra points out are likely unavoidable, but we might be able to make some additional optimizations in the future to reduce the overhead. Your best bet will almost always be to precache glyphs you know you will need so they don't need to be generated at times when you can't afford a frame rate hit.

    The fix noted above is for a separate issue that would cause (many) redundant callbacks whenever the font texture atlas was invalidated. That fix will be in 5.3 and a patch for 5.2 (although I'm not certain which 5.2 patch).
     
  35. movra

    movra

    Joined:
    Feb 16, 2013
    Posts:
    571
    Ah, I didn't want to point out the performance hit, but the difference between profiling a scene within the editor and remote profiling a build.

    You can't measure the run-time impact of font caching when profiling a scene that is played in the editor, because the font cache is not deleted before starting scene playback.

    Therefore I thought maybe BenouKat's issue could only be seen through remote profiling a build.
     
    Last edited: Oct 20, 2015
  36. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    I understand everything but my problem isn't related at all (I think) to Movra's problem, which is not really a problem anyway. He was just trying to explain the difference between editor and build profiling.

    My main problem is still the same (FontCacheForText taking 1 to 4 seconds to process).

    Soooo... Can this fix the bug I open this thread for ? :p You talk about redundant callbacks and it's pretty much what happens in my case (Post #21), so can you confirm we are talking about the same thing ?

    If not, then I repost this: I opened a bug here : https://fogbugz.unity3d.com/default.asp?735927_hg9p22mkgnqocros with the whole project attached

    I very appreciate your support, thanks for all your work :)
     
    Last edited: Oct 20, 2015
  37. Wrymnn

    Wrymnn

    Joined:
    Sep 24, 2014
    Posts:
    302
    The same problem here.

    Font.CacheFontForText is eating a LOT of performance. I have mostly static text, the only text that is moving is text in world canvas, which doesn`t have best fit and is not dynamic, I set it to normal Arial font.
    It is true I am instantiating prefab with this UI text object. But this is till huge spike.
     

    Attached Files:

  38. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    I know the fix wasn't in this version but I see many optimization in UI on 5.2.2f1 so I downloaded it and i confirm : It's still here, still 7000 calls to FontCacheForText, still terrible time to process.

    I see a light improvement in the process time, we go from 1-4 seconds to 0.5-3 seconds, which is, well, sure, better, but still terrible and no-go for us.

    Thanks for your work !
     
  39. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    Hello, having this issue too. Using Unity 5.2.1f1, think I have to port project to 5.1.3. Hope the problem will be fixed in 5.3.
     
  40. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    I can see such freezes even in my gamescene when scores updating (UI text, best fit is not checked), everything becomes very laggy, we planned to release next week! @freese could you tell when 5.3 will be released.
     
  41. jcredible

    jcredible

    Joined:
    Dec 4, 2012
    Posts:
    24
    Roadmap says that 5.3 will release in December.
     
  42. PedroGV

    PedroGV

    Joined:
    Nov 1, 2010
    Posts:
    415
    Hope this get fixed for 5.2.2p2
     
  43. jhkimblue

    jhkimblue

    Joined:
    Mar 27, 2015
    Posts:
    14
    I hope it too... till the performance regression is fixed, those shiny, new unity versions are not even a option for us.
     
  44. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    @BenouKat you said there's no problem in unity 5.1.3p1, I have downloaded 5.1.2 recently, didn't no what patch it was when downloading. Now I can see that it is 5.1.2f1 in editor (About Unity button). I would like to get 5.1.3p1, but I am not sure that here is the right patch I need. Could you please tell how I can get p1? There is that problem with Font.CacheFont.. in 5.1.2f1.
     
  45. johannv

    johannv

    Joined:
    Apr 10, 2014
    Posts:
    3
    I had the same problem with Unity 5.2.X.

    Downgrade in Unity 5.1.4f1 resolved all the spikes.

    Additional informations : some spikes arrives only after 15-20 minutes of playing. Maybe there are an additional problem in caching or cleaning UI
     
  46. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Same here, we are stucked until patch, this is a very serious bug. I hope for a soon 5.2 patch or a Unity 5.3 bug proof.


    Hi, as far as I can see, ALL 5.1.x versions doesn't have the problem, you can choose any patch you want. The problems appeared with 5.2.
    Go to get Unity page and then select "patch release". We work with 5.1.3p1 for now and it works fine.
     
  47. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    Hi, thanks didn’t see that link)) I have downloaded 5.1.3p1 and tested my project on iPad, unfortunately it is still happening. The freeze occurs in two cases in my game: a)when turning on/off UI.text: UI.text.enabled = true/false;
    b)updating UI.text value:

    void OnCollisionEnter (Collision collision) { //happens quite often
    score ++;
    scoreUI.text = score.ToString ();
    }


    In both cases I can see in the profiler that Font.CacheFont takes huge amount of ms.
    Maybe I am doing something wrong.
     
  48. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Hmm weird. Actually Font.CacheFont always doing a bit of treatment since 4.6, in my game Font.CacheFontForText is the "first" performance line in the profiler even in 5.1.3.

    But we are talking about 55ms and 166 calls in 5.1.3. If I do the exact same thing in 5.2 right now, by opening the exact same UI, I get between 1000 and 3000ms and 9000 calls. And if I do simple maths, the calls are related to the process time ( 55 / 166 * 9000 = 3000ms tada!).

    How many calls do you get ? May be if your scene is very light, it's not surprising that the Font.CacheFontForText is still doing a "spike".
     
  49. Jakhongir

    Jakhongir

    Joined:
    May 12, 2015
    Posts:
    37
    There are not so many calls to Font.CaheFont in the profiler. What do you mean "scene is very light"? There are 21 UI.images, 7 UI.texts with about 10 words each in it(switching on/off) the font is Arial, background, and two UI.buttons in the scene.

    On screenshot shown profiling that scene on iPad.
     

    Attached Files:

    Last edited: Nov 3, 2015
  50. BenouKat

    BenouKat

    Joined:
    Feb 29, 2012
    Posts:
    222
    Wow, these is weird, certainly linked to my case, but not identical. Hope the future patch will correct it.
     
unityunity