Photon Realtime Extensions: StripKeysWithNullValues and GC Alloc
Options
Comments
-
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); // } //} }
0 -
Yes, this should be better to use.
I guess you would be fine with this being used for Photon / PUN?0 -
Sure0
-
The downside of this implementation is that it's not thread safe.
I guess we'll have to pool this.1