Fix superfluous delegate allocations in Unix WaitSubsystem (dotnet/coreclr#7454)
authorJan Kotas <jkotas@microsoft.com>
Tue, 28 May 2019 20:55:06 +0000 (13:55 -0700)
committerJan Kotas <jkotas@microsoft.com>
Wed, 29 May 2019 01:39:25 +0000 (18:39 -0700)
Fixes dotnet/coreclr#7452

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Commit migrated from https://github.com/dotnet/coreclr/commit/cdcc588fba1158a3ec7f45a539eece687426e917

src/libraries/System.Private.CoreLib/src/System/Threading/WaitHandle.cs

index 75965a3..dae3ddc 100644 (file)
@@ -183,10 +183,11 @@ namespace System.Threading
             // t_safeWaitHandlesForRent can be null when it was not initialized yet or
             // if a re-entrant wait is performed and the array is already rented. In
             // that case we just allocate a new one and reuse it as necessary.
-            if (safeWaitHandles == null || safeWaitHandles.Length < capacity)
+            int currentLength = (safeWaitHandles != null) ? safeWaitHandles.Length : 0;
+            if (currentLength < capacity)
             {
-                // Always allocate at least 4 slots to prevent unnecessary reallocations
-                safeWaitHandles = new SafeWaitHandle[Math.Max(capacity, 4)];
+                safeWaitHandles = new SafeWaitHandle[Math.Max(capacity,
+                    Math.Min(MaxWaitHandles, 2 * currentLength))];
             }
 
             return safeWaitHandles;