Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Runtime/Scripts/Utilities/GA_HashtablePool.cs
Original file line number Diff line number Diff line change
@@ -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<Hashtable> _cache = new Queue<Hashtable>();

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);
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Utilities/GA_HashtablePool.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions Runtime/Scripts/Wrapper/GA_Wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,15 +693,17 @@ public static string GetExternalUserId()

private static string DictionaryToJsonString(IDictionary<string, object> dict)
{
Hashtable table = new Hashtable();
if (dict != null)
using (GA_HashtablePool.Get(out var table))
{
foreach (KeyValuePair<string, object> pair in dict)
if (dict != null)
{
table.Add(pair.Key, pair.Value);
foreach (KeyValuePair<string, object> pair in dict)
{
table.Add(pair.Key, pair.Value);
}
}
return GA_MiniJSON.Serialize(table);
}
return GA_MiniJSON.Serialize(table);
}

// TIMER FUNCTIONS
Expand Down