Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Discussion Performance Best Practices

Discussion in 'UI Toolkit' started by aniol21, May 3, 2023.

  1. aniol21

    aniol21

    Joined:
    May 3, 2023
    Posts:
    5
    Hi,

    We are currently using the UI Toolkit for a mobile project, and are running into some performance issues when the application initializes, as others have noted in other forum posts. We are using Unity 2021.3.17f1.

    In particular, we are having issues with a parent uxml that contains multiple children uxmls, resulting in a pretty big hierarchy. When it comes to assigning stylesheets in this specific case, is it better to have all of them in the parent uxml or have them in each child uxml? Is one more performant than the other? Is it better to have less stylesheets with more selectors in them, or more stylesheets with less selectors?

    Are there any other best practices to keep in mind other than the ones shared in this page and this post? Also, the post mentions that inline styles require more internal processing, but the best practices page doesn't mention that; do many inline styles have an impact on the processing time of the Update Style process?

    Thank you.
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    The other page that is worth looking at is this one: https://docs.unity3d.com/Manual/UIE-best-practices-for-managing-elements.html

    For your specific questions about where to have style sheets, indeed it can be beneficial to add them as low as possible in the hierarchy. It's difficult to predict if you benefit from this but quoting the docs, you can maybe try to anticipate the effect by understanding how the matching algorithm works:

    So if you lower the amount of applicable USS files inside a UXML, you can get benefits. Ultimately don't forget that the number of elements is still the bigger factor at play, so advice from the page I posted above is the most important.
     
  3. aniol21

    aniol21

    Joined:
    May 3, 2023
    Posts:
    5
    Thank you for your reply, that link will come in handy.

    At the end of the day, it seems that the amount of selectors doesn't matter when it comes to performance, what matters is the number of USS files each UXML is referencing and the number of classes each element has. Following this rule, would it then be better to just have 1 USS file in the TSS that contains all styles for the application? This would reduce the number of lookup tables to 1 right?
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    Yes you are right 1 USS file = 1 look up table. The number of complex selectors can matter a lot though, because they could be rejected as the hierarchy is walked up to check the selector against parents.
     
  5. aniol21

    aniol21

    Joined:
    May 3, 2023
    Posts:
    5
    I see. What do you mean by they could be rejected?
     
  6. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    Not too long ago I stumbled across this excellent blog post from Microsoft about CSS selector performance considerations: https://blogs.windows.com/msedgedev/2023/01/17/the-truth-about-css-selector-performance/

    This part highlights the work entailed by complex selectors:
    By "rejection", I am referring to the fact that while any element with the `.link` class will be evaluated as a potential candidate for the selector above, it may "rejected" when the full evaluation of its ancestor elements occurs. Matching against "link" is done through the lookup table, but the right to left evaluation of the selector walks up the hierarchy every time. While there is a "fast reject" mechanism to help in this case, any actual match incurs a full hierarchy walk up.

    I hope this helps.
     
  7. aniol21

    aniol21

    Joined:
    May 3, 2023
    Posts:
    5
    Awesome, thank you for the detailed explanation.

    How does importing other style sheets using the @ import rule affect this structure / functionality? Do all @ imports get merged into 1 lookup table for the style sheet containing them, or does each @ import act as a separate lookup table?
     
  8. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    Each style sheet gets its own lookup table, even if they are imported as part of a larger style sheet.
     
    Last edited: May 18, 2023