diff --git a/Runtime/Scripts/Utilities/GA_HashtablePool.cs b/Runtime/Scripts/Utilities/GA_HashtablePool.cs new file mode 100644 index 0000000..b2e9163 --- /dev/null +++ b/Runtime/Scripts/Utilities/GA_HashtablePool.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace GameAnalyticsSDK.Utilities +{ + public static class GA_HashtablePool + { + private static readonly Queue _cache = new Queue(); + + public static IDisposable Get(out Hashtable result) + { + Pop(out result); + return new Cached(result); + } + + private static void Pop(out Hashtable result) + { + if (_cache.Count > 0) + { + result = _cache.Dequeue(); + } + else + { + result = new Hashtable(); + } + } + + private static void Push(Hashtable hashtable) + { + if (hashtable == null) + return; + + if (_cache.Contains(hashtable)) + return; + + hashtable.Clear(); + _cache.Enqueue(hashtable); + } + + private struct Cached : IDisposable + { + public Hashtable hashtable; + + public Cached(Hashtable hashtable) + { + this.hashtable = hashtable; + } + + public void Dispose() + { + Push(hashtable); + } + } + } +} diff --git a/Runtime/Scripts/Utilities/GA_HashtablePool.cs.meta b/Runtime/Scripts/Utilities/GA_HashtablePool.cs.meta new file mode 100644 index 0000000..e1833fd --- /dev/null +++ b/Runtime/Scripts/Utilities/GA_HashtablePool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 63a9ca9b5230f614e857f20a652ccd8f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Scripts/Wrapper/GA_Wrapper.cs b/Runtime/Scripts/Wrapper/GA_Wrapper.cs index 320b54c..5cc6e99 100644 --- a/Runtime/Scripts/Wrapper/GA_Wrapper.cs +++ b/Runtime/Scripts/Wrapper/GA_Wrapper.cs @@ -693,15 +693,17 @@ public static string GetExternalUserId() private static string DictionaryToJsonString(IDictionary dict) { - Hashtable table = new Hashtable(); - if (dict != null) + using (GA_HashtablePool.Get(out var table)) { - foreach (KeyValuePair pair in dict) + if (dict != null) { - table.Add(pair.Key, pair.Value); + foreach (KeyValuePair pair in dict) + { + table.Add(pair.Key, pair.Value); + } } + return GA_MiniJSON.Serialize(table); } - return GA_MiniJSON.Serialize(table); } // TIMER FUNCTIONS