From 8714b5b97748447143af779eb97b5b000e3b1811 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 5 Aug 2023 17:01:21 +0200 Subject: [PATCH] RecyclableLongList.Remove & .RemoveAt performance improvements. --- Recyclable.Collections/RecyclableLongList.cs | 46 ++++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/Recyclable.Collections/RecyclableLongList.cs b/Recyclable.Collections/RecyclableLongList.cs index 6d9fd9c..a398916 100644 --- a/Recyclable.Collections/RecyclableLongList.cs +++ b/Recyclable.Collections/RecyclableLongList.cs @@ -875,19 +875,20 @@ namespace Recyclable.Collections [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public bool Remove(T item) { - var index = LongIndexOf(item); - if (index >= 0) + long index; + if ((index = LongIndexOf(item)) >= 0) { - _longCount--; - - if (index < _longCount) + if (index < --_longCount) { Helpers.CopyFollowingItems(this, index); } if (_nextItemIndex > 0) { - _nextItemIndex--; + if (--_nextItemIndex == 0) + { + _lastBlockWithData--; + } } else { @@ -895,11 +896,6 @@ namespace Recyclable.Collections _nextItemBlockIndex--; } - if (_nextItemIndex == 0) - { - _lastBlockWithData--; - } - if (NeedsClearing) { #nullable disable @@ -948,16 +944,17 @@ namespace Recyclable.Collections Helpers.ThrowIndexOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range. Expected value between 0 and {_longCount - 1}"); } - _longCount--; - - if (index < _longCount) + if (index < --_longCount) { Helpers.CopyFollowingItems(this, index); } if (_nextItemIndex > 0) { - _nextItemIndex--; + if (--_nextItemIndex == 0) + { + _lastBlockWithData--; + } } else { @@ -965,11 +962,6 @@ namespace Recyclable.Collections _nextItemBlockIndex--; } - if (_nextItemIndex == 0) - { - _lastBlockWithData--; - } - if (NeedsClearing) { #nullable disable @@ -988,16 +980,17 @@ namespace Recyclable.Collections Helpers.ThrowIndexOutOfRangeException($"Argument \"{nameof(index)}\" = {index} is out of range. Expected value between 0 and {_longCount - 1}"); } - _longCount--; - - if (index < _longCount) + if (index < --_longCount) { Helpers.CopyFollowingItems(this, index); } if (_nextItemIndex > 0) { - _nextItemIndex--; + if (--_nextItemIndex == 0) + { + _lastBlockWithData--; + } } else { @@ -1005,11 +998,6 @@ namespace Recyclable.Collections _nextItemBlockIndex--; } - if (_nextItemIndex == 0) - { - _lastBlockWithData--; - } - if (NeedsClearing) { #nullable disable