From 76dd62a5e2e5bd6e0905cdbb87abcac31367bb34 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Wed, 28 Jun 2023 23:15:53 +0200 Subject: [PATCH] RecyclableList.Clear functionality copied to .Dispose for better performance. --- Recyclable.Collections/RecyclableList.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 5e86217..9cb245c 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -534,14 +534,22 @@ namespace Recyclable.Collections public void Dispose() { - if (_capacity > 0) + _version++; + if (_count != 0) { - Clear(); - if (_count is >= RecyclableDefaults.MinPooledArrayLength) + if (NeedsClearing) { - // If anything, it has been already cleared by .Clear(), Remove() or RemoveAt() methods, as the list was modified / disposed. - _arrayPool.Return(_memoryBlock, false); + Array.Clear(_memoryBlock, 0, _count); } + + _count = 0; + } + + if (_capacity >= RecyclableDefaults.MinPooledArrayLength) + { + _capacity = 0; + // If anything, it has been already cleared by .Clear(), Remove() or RemoveAt() methods, as the list was modified / disposed. + _arrayPool.Return(_memoryBlock, false); } GC.SuppressFinalize(this);