Skip to content

Commit

Permalink
Removed branching
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed May 16, 2024
1 parent bb0a25b commit 0a44651
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/DotNext.Threading/Collections/Concurrent/IndexPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IndexPool()
/// </exception>
public IndexPool(int maxValue)
{
if (maxValue < 0 || maxValue > MaxValue)
if ((uint)maxValue > (uint)MaxValue)
throw new ArgumentOutOfRangeException(nameof(maxValue));

bitmask = ulong.MaxValue;
Expand Down Expand Up @@ -148,7 +148,7 @@ public int Take(Span<int> indicies)
/// value specified for this pool.</exception>
public void Return(int value)
{
if (value < 0 || value > maxValue)
if ((uint)value > (uint)maxValue)
ThrowArgumentOutOfRangeException();

Interlocked.Or(ref bitmask, 1UL << value);
Expand Down Expand Up @@ -184,7 +184,7 @@ public void Return(ReadOnlySpan<int> indicies)
/// <param name="value">The value to check.</param>
/// <returns><see langword="true"/> if <paramref name="value"/> is available for rent; otherwise, <see langword="false"/>.</returns>
public readonly bool Contains(int value)
=> value >= 0 && value <= maxValue && Contains(Volatile.Read(in bitmask), value);
=> (uint)value <= (uint)maxValue && Contains(Volatile.Read(in bitmask), value);

private static bool Contains(ulong bitmask, int index)
=> (bitmask & (1UL << index)) is not 0UL;
Expand Down

0 comments on commit 0a44651

Please sign in to comment.