Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Leopotam on April 05, 2016, 07:03:59 PM

Title: [FIX] UIDrawCall GC allocation removing
Post by: Leopotam 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; }
Title: Re: [FIX] UIDrawCall GC allocation removing
Post by: ArenMook 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;
Title: Re: [FIX] UIDrawCall GC allocation removing
Post by: Leopotam 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.
Title: Re: [FIX] UIDrawCall GC allocation removing
Post by: ArenMook 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.
Title: Re: [FIX] UIDrawCall GC allocation removing
Post by: Leopotam 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.