From b9a0589597633cd49a866e85dba2f2ba2dff5a26 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 16 Sep 2023 21:24:06 +0200 Subject: [PATCH] Micro improvements to get even a bit smaller code size. --- Recyclable.Collections/RecyclableList.cs | 4 ++-- Recyclable.Collections/RecyclableLongList.Helpers.cs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 3d1f34a..cc90c49 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -211,13 +211,13 @@ namespace Recyclable.Collections } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Contains(T item) => _count > 0 && Array.IndexOf(_memoryBlock, item, 0, _count) >= 0; + public bool Contains(T item) => _count != 0 && Array.IndexOf(_memoryBlock, item, 0, _count) >= 0; [MethodImpl(MethodImplOptions.NoInlining)] public void CopyTo(T[] array, int index) => Array.Copy(_memoryBlock, 0, array, index, _count); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int IndexOf(T itemToFind) => _count > 0 ? Array.IndexOf(_memoryBlock, itemToFind, 0, _count) : RecyclableDefaults.ItemNotFoundIndex; + public int IndexOf(T itemToFind) => _count != 0 ? Array.IndexOf(_memoryBlock, itemToFind, 0, _count) : RecyclableDefaults.ItemNotFoundIndex; [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] public void Insert(int index, T item) diff --git a/Recyclable.Collections/RecyclableLongList.Helpers.cs b/Recyclable.Collections/RecyclableLongList.Helpers.cs index 8848b0f..e2c43cf 100644 --- a/Recyclable.Collections/RecyclableLongList.Helpers.cs +++ b/Recyclable.Collections/RecyclableLongList.Helpers.cs @@ -79,7 +79,7 @@ namespace Recyclable.Collections Span sourceBlockMemory; Span destinationArrayMemory; int startingBlockIndex = (int)(startingIndex / blockSize); - int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize > 0 ? 1 : 0)) - 1; + int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize != 0 ? 1 : 0)) - 1; Span sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1); destinationArrayMemory = new Span(destinationArray, destinationArrayIndex, (int)Math.Min(destinationArray.Length - destinationArrayIndex, itemsCount)); int memoryBlockIndex; @@ -90,7 +90,7 @@ namespace Recyclable.Collections destinationArrayMemory = destinationArrayMemory[blockSize..]; } - if (itemsCount - (lastBlockIndex * blockSize) > 0) + if (itemsCount - (lastBlockIndex * blockSize) != 0) { sourceBlockMemory = new(sourceMemoryBlocksSpan[lastBlockIndex], 0, (int)(itemsCount - (lastBlockIndex * blockSize))); sourceBlockMemory.CopyTo(destinationArrayMemory); @@ -105,7 +105,7 @@ namespace Recyclable.Collections } int startingBlockIndex = (int)(startingIndex / blockSize); - int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize > 0 ? 1 : 0)) - 1; + int lastBlockIndex = (int)((itemsCount / blockSize) + (itemsCount % blockSize != 0 ? 1 : 0)) - 1; var destinationIndex = destinationArrayIndex; Span sourceMemoryBlocksSpan = new(sourceMemoryBlocks, startingBlockIndex, lastBlockIndex + 1); @@ -143,7 +143,7 @@ namespace Recyclable.Collections public static long Resize(RecyclableLongList list, int minBlockSize, byte minBlockSizePow2Shift, long newCapacity) { int sourceBlockCount = list._memoryBlocks?.Length ?? 0; - int requiredBlockCount = checked((int)(newCapacity >> minBlockSizePow2Shift) + ((newCapacity & (minBlockSize - 1)) > 0 ? 1 : 0)); + int requiredBlockCount = checked((int)(newCapacity >> minBlockSizePow2Shift) + ((newCapacity & (minBlockSize - 1)) != 0 ? 1 : 0)); int blockIndex; T[][] newMemoryBlocks;