Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize FixedArray8<T> indexer #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 14 additions & 64 deletions TunnelVisionLabs.Collections.Trees/Immutable/FixedArray8`1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,94 +7,44 @@ namespace TunnelVisionLabs.Collections.Trees.Immutable
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
internal struct FixedArray8<T>
{
private T _item0;
#pragma warning disable IDE0044 // Add readonly modifier
#pragma warning disable CS0169 // The field 'name' is never used
private T _item1;
private T _item2;
private T _item3;
private T _item4;
private T _item5;
private T _item6;
private T _item7;
#pragma warning restore CS0169 // The field 'name' is never used
#pragma warning restore IDE0044 // Add readonly modifier

public int Length => 8;

public T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
switch (index)
{
case 0:
return _item0;

case 1:
return _item1;

case 2:
return _item2;

case 3:
return _item3;

case 4:
return _item4;
if ((uint)index >= 8)
ThrowHelper.ThrowIndexOutOfRangeException();

case 5:
return _item5;

case 6:
return _item6;

case 7:
return _item7;

default:
throw new IndexOutOfRangeException();
}
return Unsafe.Add(ref _item0, index);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
switch (index)
{
case 0:
_item0 = value;
break;

case 1:
_item1 = value;
break;

case 2:
_item2 = value;
break;

case 3:
_item3 = value;
break;

case 4:
_item4 = value;
break;
if ((uint)index >= 8)
ThrowHelper.ThrowIndexOutOfRangeException();

case 5:
_item5 = value;
break;

case 6:
_item6 = value;
break;

case 7:
_item7 = value;
break;

default:
throw new IndexOutOfRangeException();
}
Unsafe.Add(ref _item0, index) = value;
}
}

Expand Down
17 changes: 17 additions & 0 deletions TunnelVisionLabs.Collections.Trees/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace TunnelVisionLabs.Collections.Trees
{
using System;
using System.Diagnostics.CodeAnalysis;

internal static class ThrowHelper
{
[DoesNotReturn]
internal static void ThrowIndexOutOfRangeException()
{
throw new IndexOutOfRangeException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.0" />
</ItemGroup>

</Project>