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

Clean up some [InlineArray] use #99766

Merged
merged 4 commits into from
Mar 15, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public object Invoke(object? arg1)
ThrowForArgCountMismatch();
}

object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1, _parameterCount));
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -67,9 +67,9 @@ public object Invoke(object? arg1, object? arg2)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 2));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -83,10 +83,10 @@ public object Invoke(object? arg1, object? arg2, object? arg3)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;
argStorage._args[2] = arg3;
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 3));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -100,11 +100,11 @@ public object Invoke(object? arg1, object? arg2, object? arg3, object? arg4)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
argStorage._args.Set(3, arg4);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;
argStorage._args[2] = arg3;
argStorage._args[3] = arg4;
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 4));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,9 @@ private unsafe ref byte InvokeWithFewArguments(
object?[] parameters, BinderBundle? binderBundle, bool wrapInTargetInvocationException)
{
Debug.Assert(_argumentCount <= MaxStackAllocArgCount);
int argCount = _argumentCount;

StackAllocatedArguments argStorage = default;
Span<object?> copyOfParameters = argStorage._args.AsSpan(argCount);
Span<object?> copyOfParameters = ((Span<object?>)argStorage._args).Slice(0, _argumentCount);
StackAllocatedByRefs byrefStorage = default;
#pragma warning disable CS8500
void* pByRefStorage = (ByReference*)&byrefStorage;
Expand Down Expand Up @@ -532,10 +531,9 @@ private unsafe ref byte InvokeWithFewArguments(
IntPtr methodToCall, ref byte thisArg, ref byte ret, Span<object?> parameters)
{
Debug.Assert(_argumentCount <= MaxStackAllocArgCount);
int argCount = _argumentCount;

StackAllocatedArguments argStorage = default;
Span<object?> copyOfParameters = argStorage._args.AsSpan(argCount);
Span<object?> copyOfParameters = ((Span<object?>)argStorage._args).Slice(0, _argumentCount);
StackAllocatedByRefs byrefStorage = default;
#pragma warning disable CS8500
void* pByRefStorage = (ByReference*)&byrefStorage;
Expand Down Expand Up @@ -884,19 +882,6 @@ private unsafe object ReturnTransform(ref byte byref, bool wrapInTargetInvocatio
internal struct ArgumentData<T>
{
private T _arg0;

[UnscopedRef]
public Span<T> AsSpan(int length)
{
Debug.Assert((uint)length <= MaxStackAllocArgCount);
return new Span<T>(ref _arg0, length);
}

public void Set(int index, T value)
{
Debug.Assert((uint)index < MaxStackAllocArgCount);
Unsafe.Add(ref _arg0, index) = value;
}
}

// Helper struct to avoid intermediate object[] allocation in calls to the native reflection stack.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public static MethodInvoker Create(MethodBase method)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, ((Span<object?>)argStorage._args).Slice(0, 2));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -98,11 +98,11 @@ public static MethodInvoker Create(MethodBase method)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;
argStorage._args[2] = arg3;

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, ((Span<object?>)argStorage._args).Slice(0, 3));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -116,12 +116,12 @@ public static MethodInvoker Create(MethodBase method)
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
argStorage._args.Set(3, arg4);
argStorage._args[0] = arg1;
argStorage._args[1] = arg2;
argStorage._args[2] = arg3;
argStorage._args[3] = arg4;

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, ((Span<object?>)argStorage._args).Slice(0, 4));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,21 @@ public static object CreateInstance(
}

// Attempt to use the stack allocated arg values if <= 4 ctor args.
Span<object?> values;
StackAllocatedObjects stackValues = default;
int maxArgs = GetMaxArgCount();
if (maxArgs <= StackAllocatedObjects.MaxStackAllocArgCount / 2)
{
values = MemoryMarshal.CreateSpan(ref stackValues._args._arg0, maxArgs * 2);
}
else
{
values = new Span<object?>(new object?[maxArgs * 2], 0, maxArgs * 2);
}
Span<object?> values = maxArgs <= StackAllocatedObjects.MaxStackAllocArgCount / 2 ?
stackValues :
new object?[maxArgs * 2];

Span<object?> ctorArgs = values.Slice(0, maxArgs);
Span<object?> bestCtorArgs = values.Slice(maxArgs);
Span<object?> bestCtorArgs = values.Slice(maxArgs, maxArgs);
#else
constructors = CreateConstructorInfoExs(instanceType);
object?[]? ctorArgs = null;
object?[]? bestCtorArgs = null;
#endif

ConstructorMatcher matcher = default;
scoped ConstructorMatcher matcher = default;
ConstructorInfoEx? constructor;
IServiceProviderIsService? serviceProviderIsService = provider.GetService<IServiceProviderIsService>();
// if container supports using IServiceProviderIsService, we try to find the longest ctor that
Expand Down Expand Up @@ -124,7 +118,7 @@ public static object CreateInstance(
}

int bestLength = -1;
ConstructorMatcher bestMatcher = default;
scoped ConstructorMatcher bestMatcher = default;
bool multipleBestLengthFound = false;

// Find the constructor with the most matches.
Expand Down Expand Up @@ -1223,17 +1217,11 @@ public static void ClearCache(Type[]? _)
}
}

[StructLayout(LayoutKind.Sequential)]
private ref struct StackAllocatedObjects
[InlineArray(MaxStackAllocArgCount)]
private struct StackAllocatedObjects
{
internal const int MaxStackAllocArgCount = 8;
internal StackAllocatedObjectValues _args;

[InlineArray(MaxStackAllocArgCount)]
internal struct StackAllocatedObjectValues
{
internal object? _arg0;
}
private object? _arg0;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\AhoCorasickBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\AhoCorasickNode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\CharacterFrequencyHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\EightPackedReferences.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\RabinKarp.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\StringSearchValuesHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\TeddyBucketizer.cs" />
Expand All @@ -472,6 +471,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\StringSearchValuesAhoCorasick.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\StringSearchValuesRabinKarp.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOutOfRangeException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\InlineArrays.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\InsufficientExecutionStackException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\InsufficientMemoryException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Int16.cs" />
Expand Down Expand Up @@ -615,7 +615,6 @@
<Compile Include="$(MSBuildThisFileDirectory)System\OutOfMemoryException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\OverflowException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ParamArrayAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ParamsArray.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\ParseNumbers.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\PasteArguments.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\PlatformID.cs" />
Expand Down Expand Up @@ -2766,4 +2765,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public override void Write([StringSyntax(StringSyntaxAttribute.CompositeFormat)]
if (GetType() == typeof(StreamWriter))
{
TwoObjects two = new TwoObjects(arg0, arg1);
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref two.Arg0, 2), appendNewLine: false);
WriteFormatHelper(format, two, appendNewLine: false);
}
else
{
Expand All @@ -544,7 +544,7 @@ public override void Write([StringSyntax(StringSyntaxAttribute.CompositeFormat)]
if (GetType() == typeof(StreamWriter))
{
ThreeObjects three = new ThreeObjects(arg0, arg1, arg2);
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref three.Arg0, 3), appendNewLine: false);
WriteFormatHelper(format, three, appendNewLine: false);
}
else
{
Expand Down Expand Up @@ -585,7 +585,7 @@ public override void WriteLine([StringSyntax(StringSyntaxAttribute.CompositeForm
if (GetType() == typeof(StreamWriter))
{
TwoObjects two = new TwoObjects(arg0, arg1);
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref two.Arg0, 2), appendNewLine: true);
WriteFormatHelper(format, two, appendNewLine: true);
}
else
{
Expand All @@ -598,7 +598,7 @@ public override void WriteLine([StringSyntax(StringSyntaxAttribute.CompositeForm
if (GetType() == typeof(StreamWriter))
{
ThreeObjects three = new ThreeObjects(arg0, arg1, arg2);
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref three.Arg0, 3), appendNewLine: true);
WriteFormatHelper(format, three, appendNewLine: true);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

// These types are temporary workarounds for an inability to stackalloc object references.
// Once we're able to do `stackalloc object[n]`, these can be removed.

// Suppress warnings for unused private fields
#pragma warning disable CS0169, CA1823, IDE0051, IDE0044

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System
{
[InlineArray(2)]
internal struct TwoObjects
{
internal object? Arg0;
private object? _arg0;

public TwoObjects(object? arg0, object? arg1)
{
Expand All @@ -27,7 +20,7 @@ public TwoObjects(object? arg0, object? arg1)
[InlineArray(3)]
internal struct ThreeObjects
{
internal object? Arg0;
private object? _arg0;

public ThreeObjects(object? arg0, object? arg1, object? arg2)
{
Expand All @@ -36,4 +29,10 @@ public ThreeObjects(object? arg0, object? arg1, object? arg2)
this[2] = arg2;
}
}

[InlineArray(8)]
internal struct EightObjects
{
private object? _ref0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ internal object InvokeWithFewArgs(Span<object?> arguments)
Debug.Assert(_argCount <= MaxStackAllocArgCount);

StackAllocatedArgumentsWithCopyBack stackArgStorage = default;
Span<object?> copyOfArgs = stackArgStorage._args.AsSpan(_argCount);
scoped Span<bool> shouldCopyBack = stackArgStorage._shouldCopyBack.AsSpan(_argCount);
Span<object?> copyOfArgs = ((Span<object?>)stackArgStorage._args).Slice(0, _argCount);
scoped Span<bool> shouldCopyBack = ((Span<bool>)stackArgStorage._shouldCopyBack).Slice(0, _argCount);

for (int i = 0; i < _argCount; i++)
{
Expand Down Expand Up @@ -279,7 +279,7 @@ internal object InvokeWithFewArgs(Span<object?> arguments)
internal object InvokeDirectByRef(object? arg1 = null, object? arg2 = null, object? arg3 = null, object? arg4 = null)
{
StackAllocatedArguments stackStorage = new(arg1, arg2, arg3, arg4);
return InvokeDirectByRefWithFewArgs(stackStorage._args.AsSpan(_argCount));
return InvokeDirectByRefWithFewArgs(((Span<object?>)stackStorage._args).Slice(0, _argCount));
}

internal unsafe object InvokeDirectByRefWithFewArgs(Span<object?> copyOfArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,6 @@ internal enum InvokerArgFlags : int
internal struct ArgumentData<T>
{
private T _arg0;

[UnscopedRef]
public Span<T> AsSpan(int length)
{
Debug.Assert((uint)length <= MaxStackAllocArgCount);
return new Span<T>(ref _arg0, length);
}

public void Set(int index, T value)
{
Debug.Assert((uint)index < MaxStackAllocArgCount);
Unsafe.Add(ref _arg0, index) = value;
}
}

// Helper struct to avoid intermediate object[] allocation in calls to the native reflection stack.
Expand All @@ -214,10 +201,10 @@ internal ref struct StackAllocatedArguments
{
public StackAllocatedArguments(object? obj1, object? obj2, object? obj3, object? obj4)
{
_args.Set(0, obj1);
_args.Set(1, obj2);
_args.Set(2, obj3);
_args.Set(3, obj4);
_args[0] = obj1;
_args[1] = obj2;
_args[2] = obj3;
_args[3] = obj4;
}

internal ArgumentData<object?> _args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ internal static void ThrowTargetParameterCountException()
Debug.Assert(_argCount <= MaxStackAllocArgCount);

StackAllocatedArgumentsWithCopyBack stackArgStorage = default;
Span<object?> copyOfArgs = stackArgStorage._args.AsSpan(_argCount);
Span<bool> shouldCopyBack = stackArgStorage._shouldCopyBack.AsSpan(_argCount);
Span<object?> copyOfArgs = ((Span<object?>)stackArgStorage._args).Slice(0, _argCount);
Span<bool> shouldCopyBack = ((Span<bool>)stackArgStorage._shouldCopyBack).Slice(0, _argCount);

object? ret;
if ((_strategy & InvokerStrategy.StrategyDetermined_ObjSpanArgs) == 0)
Expand Down
Loading