Skip to content

Commit 63f8ce0

Browse files
committed
Refactor ToArray to less footprint not making a list and then an array.
castleproject#565
1 parent 619631c commit 63f8ce0

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/Castle.Windsor/Core/Internal/SimpleThreadSafeSet.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ public bool Remove(T item)
6767

6868
public T[] ToArray()
6969
{
70-
List<T> hashSetCopy;
71-
using (@lock.ForReading())
70+
@lock.EnterReadLock();
71+
try
72+
{
73+
if (implementation.Count == 0) return new T[0];
74+
var hashSetCopy = new T[implementation.Count];
75+
implementation.CopyTo(hashSetCopy);
76+
return hashSetCopy;
77+
}
78+
finally
7279
{
73-
hashSetCopy = new List<T>(implementation);
80+
@lock.ExitReadLock();
7481
}
75-
return hashSetCopy.ToArray();
7682
}
7783
}
7884
}

0 commit comments

Comments
 (0)