Skip to content

Commit 45f8218

Browse files
committed
Removed collect list from GC
1 parent 18d965d commit 45f8218

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
namespace MLAPI.NetworkingManagerComponents.Binary
88
{
9-
public sealed class BitWiter : IDisposable
9+
public sealed class BitWriter : IDisposable
1010
{
11-
// Collects reusable
12-
private static readonly List<WeakReference> expired = new List<WeakReference>();
11+
private static readonly Queue<List<object>> listPool = new Queue<List<object>>();
1312

1413
private static readonly float[] holder_f = new float[1];
1514
private static readonly double[] holder_d = new double[1];
@@ -38,43 +37,45 @@ private static readonly FieldInfo
3837
dec_hi,
3938
dec_flags;
4039

41-
static BitWiter()
40+
static BitWriter()
4241
{
4342
dec_lo = typeof(decimal).GetField("lo", BindingFlags.NonPublic);
4443
dec_mid = typeof(decimal).GetField("mid", BindingFlags.NonPublic);
4544
dec_hi = typeof(decimal).GetField("hi", BindingFlags.NonPublic);
4645
dec_flags = typeof(decimal).GetField("flags", BindingFlags.NonPublic);
46+
47+
for (int i = 0; i < 10; i++)
48+
{
49+
listPool.Enqueue(new List<object>());
50+
}
4751
}
4852

49-
private object[] collect;
53+
private List<object> collect = null;
54+
private bool tempAlloc = false;
5055
private readonly int bufferSize;
5156
private int collectCount = 0;
5257

5358
/// <summary>
5459
/// Allocates a new binary collector.
5560
/// </summary>
56-
public BitWiter(int bufferSize)
61+
public BitWriter()
5762
{
58-
this.bufferSize = bufferSize;
59-
for (int i = expired.Count - 1; i >= 0; --i)
60-
if (expired[i].IsAlive)
61-
{
62-
collect = (object[])expired[i].Target;
63-
if (collect.Length >= bufferSize)
64-
{
65-
expired.RemoveAt(i); // This entry he been un-expired for now
66-
break;
67-
}
68-
}
69-
else expired.RemoveAt(i); // Entry has been collected by GC
70-
if (collect == null || collect.Length < bufferSize)
71-
collect = new object[bufferSize];
63+
if (listPool.Count == 0)
64+
{
65+
Debug.LogWarning("MLAPI: There can be no more than 10 BitWriters. Have you forgotten do dispose? (It will still work with worse performance)");
66+
collect = new List<object>();
67+
tempAlloc = true;
68+
}
69+
else
70+
{
71+
collect = listPool.Dequeue();
72+
}
7273
}
7374

7475
private void Push<T>(T b)
7576
{
7677
if (b is string || b.GetType().IsArray || IsSupportedType(b.GetType()))
77-
collect[collectCount++] = b is string ? Encoding.UTF8.GetBytes(b as string) : b as object;
78+
collect.Add(b is string ? Encoding.UTF8.GetBytes(b as string) : b as object);
7879
//else
7980
// Debug.LogWarning("MLAPI: The type \"" + b.GetType() + "\" is not supported by the Binary Serializer. It will be ignored");
8081
}
@@ -122,7 +123,7 @@ public long Finalize(ref byte[] buffer)
122123
foreach (var item in collect)
123124
Serialize(item, buffer, ref bitOffset);
124125

125-
return (bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1));
126+
return (bitCount / 8) + (bitCount % 8 == 0 ? 0 : 1);
126127
}
127128

128129
public long GetFinalizeSize()
@@ -367,8 +368,12 @@ private static long GetBitAllocation(Type t) =>
367368
// Creates a weak reference to the allocated collector so that reuse may be possible
368369
public void Dispose()
369370
{
370-
expired.Add(new WeakReference(collect));
371-
collect = null;
371+
if (!tempAlloc)
372+
{
373+
collect.Clear();
374+
listPool.Enqueue(collect);
375+
}
376+
collect = null; //GC picks this
372377
}
373378
}
374379
}

0 commit comments

Comments
 (0)