Skip to content

Commit 28e1436

Browse files
authored
perf: Prioritize non-weak streams over weak referenced streams. (#304)
If we prioritize pulling from the overflow streams (as it was before), it's possible for us to retain the weak references for a lot longer than is desirable as we will keep accessing them. It's therefore preferable to return a stream from the normal queue to allow the GC time to clean the weak references, and only return an overflow value if there is no other option.
1 parent 77a7648 commit 28e1436

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

MLAPI/Serialization/Pooled/BitStreamPool.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ public static class BitStreamPool
1919
/// <returns>An expandable PooledBitStream</returns>
2020
public static PooledBitStream GetStream()
2121
{
22-
if (overflowStreams.Count > 0)
22+
if (streams.Count == 0)
2323
{
24-
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("Retrieving PooledBitStream from overflow pool. Recent burst?");
24+
if (overflowStreams.Count > 0)
25+
{
26+
if (LogHelper.CurrentLogLevel <= LogLevel.Developer) LogHelper.LogInfo("Retrieving PooledBitStream from overflow pool. Recent burst?");
2527

26-
object weakStream = null;
27-
while (overflowStreams.Count > 0 && ((weakStream = overflowStreams.Dequeue().Target) == null)) ;
28+
object weakStream = null;
29+
while (overflowStreams.Count > 0 && ((weakStream = overflowStreams.Dequeue().Target) == null)) ;
2830

29-
if (weakStream != null)
30-
{
31-
PooledBitStream strongStream = (PooledBitStream)weakStream;
31+
if (weakStream != null)
32+
{
33+
PooledBitStream strongStream = (PooledBitStream)weakStream;
3234

33-
strongStream.SetLength(0);
34-
strongStream.Position = 0;
35+
strongStream.SetLength(0);
36+
strongStream.Position = 0;
3537

36-
return strongStream;
38+
return strongStream;
39+
}
3740
}
38-
}
39-
40-
if (streams.Count == 0)
41-
{
41+
4242
if (createdStreams == 254)
4343
{
4444
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("255 streams have been created. Did you forget to dispose?");

0 commit comments

Comments
 (0)