Photon Realtime Extensions: StripKeysWithNullValues and GC Alloc

Options
Hi everyone,
In our current project, we use Photon room properties to sync entities. and i noticed some heavy GC Allocs while deep profiling the game and it always ends down to this Method.

YMrCJa6.png

Have anyone faced this issue before ? or attempted to fix it ?

Comments

  • DrZepam
    Options
    Changed the implementation to reduce GC.
    /// <summary>
            /// This removes all key-value pairs that have a null-reference as value.
            /// Photon properties are removed by setting their value to null.
            /// Changes the original passed IDictionary!
            /// </summary>
            /// <param name="original">The IDictionary to strip of keys with null-values.</param>
            public static List<object> keysToRemove = new List<object>(); 
            public static void StripKeysWithNullValues(this IDictionary original)
            {
                /*
                 * by making keysToRemove a static variable that you clear before using. It will trigger allocations only during the warm-up phase
                 * as the list needs to grow. Once it's hit the high water mark for keys you need to remove. it will not allocate further during steady-state operation.
                 */
                keysToRemove.Clear();
                foreach (DictionaryEntry entry in original)
                {
                    if (entry.Value == null)
                    {
                        keysToRemove.Add(entry.Key);
                    }
                }
                foreach (var key in keysToRemove)
                {
                    original.Remove(key);
                }
    
                // ORIGINAL PHOTON IMPLEMENTATION 
    
                //object[] keys = new object[original.Count];
                //original.Keys.CopyTo(keys, 0);
    
                //for (int index = 0; index < keys.Length; index++)
                //{
                //    object key = keys[index];
                //    if (original[key] == null)
                //    {
                //        original.Remove(key);
                //    }
                //}
            }
    
  • Tobias
    Options
    Yes, this should be better to use.
    I guess you would be fine with this being used for Photon / PUN?
  • DrZepam
    Options
    Sure
  • Tobias
    Options
    The downside of this implementation is that it's not thread safe.
    I guess we'll have to pool this.