Author Topic: [FIX] UIDrawCall GC allocation removing  (Read 4499 times)

Leopotam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
[FIX] UIDrawCall GC allocation removing
« on: April 05, 2016, 07:03:59 PM »
Issue: each time of calling "renderer.sortingLayerName" getter GC memory will be allocated. Checked with unity 5.3.4f1.
Fix:
UIDrawCall, 137 loc:
replace
  1. set { if (mRenderer != null && mRenderer.sortingLayerName != value) mRenderer.sortingLayerName = value; }
with
  1. set { if (mRenderer != null) mRenderer.sortingLayerName = value; }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [FIX] UIDrawCall GC allocation removing
« Reply #1 on: April 06, 2016, 03:05:57 PM »
The problem with this is that you will effectively make NGUI mark the scene as edited continuously. This is a better approach:
  1.         public string sortingLayerName
  2.         {
  3.                 get
  4.                 {
  5.                         return (mRenderer != null) ? mRenderer.sortingLayerName : null;
  6.                 }
  7.                 set
  8.                 {
  9.                         if (mRenderer != null && mSortingLayerName != value)
  10.                         {
  11.                                 mSortingLayerName = value;
  12.                                 mRenderer.sortingLayerName = value;
  13.                         }
  14.                 }
  15.         }
  16.  
  17.         [System.NonSerialized] string mSortingLayerName;

Leopotam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: [FIX] UIDrawCall GC allocation removing
« Reply #2 on: April 06, 2016, 03:37:14 PM »
Well, why you think that engine dont compare new value with current one internally? About "better approach" - GC allocation on each getter call, better to return cached mSortingLayerName and fill it at OnEnable or something like this.
Ah, understood about "mark the scene as edited continuously." - this can be fixed with "#if UNITY_EDITOR" wrapping.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [FIX] UIDrawCall GC allocation removing
« Reply #3 on: April 06, 2016, 03:41:22 PM »
Try it. I bet it doesn't. Setting transform's values doesn't for example, even if you set to the same value it still marks the scene as edited.

The code snippet I pasted doesn't GC alloc on the setter. Getter is currently not used by NGUI at all.

Leopotam

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Re: [FIX] UIDrawCall GC allocation removing
« Reply #4 on: April 06, 2016, 03:47:01 PM »
Quote
you will effectively make NGUI mark the scene as edited continuously.
Checked right now - scene not marked as modified on each call of "mRenderer.sortingLayerName = value;", it means - they compare values internally and mark scene as dirty only on change.

Quote
Getter is currently not used by NGUI at all.
Why not fixed finally possible GC allocation even on getter? Its public api method and can be used by anyone with GC side effect.