From d6b351b1ebc0ef9637ef6278cda3052a548e9a26 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 1 Apr 2023 20:01:34 +0200 Subject: [PATCH] RecyclableList.DoIndexOf multiplication changed to bit shifting for better performance. --- Recyclable.Collections/RecyclableList.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 3967338..217ea6c 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -209,7 +209,7 @@ namespace Recyclable.Collections itemIndex = Array.IndexOf(memoryBlocksSpan[blockIndex], item, 0, blockSize); if (itemIndex >= 0) { - return itemIndex + (blockIndex * blockSize); + return itemIndex + (blockIndex << (31 - BitOperations.LeadingZeroCount((uint)blockSize))); } } @@ -224,7 +224,7 @@ namespace Recyclable.Collections return ItemNotFoundIndex; } - return itemIndex >= 0 ? itemIndex + (lastBlockIndex * blockSize) : ItemNotFoundIndex; + return itemIndex >= 0 ? itemIndex + (lastBlockIndex << (31 - BitOperations.LeadingZeroCount((uint)blockSize))) : ItemNotFoundIndex; } [MethodImpl(MethodImplOptions.AggressiveInlining)]