Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f36aa7e

Browse files
authored
Clean up some [InlineArray] use (#99766)
* Clean up some [InlineArray] use * Remove AsSpan method
1 parent 516f5c4 commit f36aa7e

File tree

15 files changed

+73
-140
lines changed

15 files changed

+73
-140
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public object Invoke(object? arg1)
5353
ThrowForArgCountMismatch();
5454
}
5555

56-
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1, _parameterCount));
56+
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1));
5757
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
5858
return result;
5959
}
@@ -67,9 +67,9 @@ public object Invoke(object? arg1, object? arg2)
6767
}
6868

6969
StackAllocatedArguments argStorage = default;
70-
argStorage._args.Set(0, arg1);
71-
argStorage._args.Set(1, arg2);
72-
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
70+
argStorage._args[0] = arg1;
71+
argStorage._args[1] = arg2;
72+
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 2));
7373
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
7474
return result;
7575
}
@@ -83,10 +83,10 @@ public object Invoke(object? arg1, object? arg2, object? arg3)
8383
}
8484

8585
StackAllocatedArguments argStorage = default;
86-
argStorage._args.Set(0, arg1);
87-
argStorage._args.Set(1, arg2);
88-
argStorage._args.Set(2, arg3);
89-
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
86+
argStorage._args[0] = arg1;
87+
argStorage._args[1] = arg2;
88+
argStorage._args[2] = arg3;
89+
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 3));
9090
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
9191
return result;
9292
}
@@ -100,11 +100,11 @@ public object Invoke(object? arg1, object? arg2, object? arg3, object? arg4)
100100
}
101101

102102
StackAllocatedArguments argStorage = default;
103-
argStorage._args.Set(0, arg1);
104-
argStorage._args.Set(1, arg2);
105-
argStorage._args.Set(2, arg3);
106-
argStorage._args.Set(3, arg4);
107-
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
103+
argStorage._args[0] = arg1;
104+
argStorage._args[1] = arg2;
105+
argStorage._args[2] = arg3;
106+
argStorage._args[3] = arg4;
107+
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(((Span<object?>)argStorage._args).Slice(0, 4));
108108
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
109109
return result;
110110
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/DynamicInvokeInfo.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -497,10 +497,9 @@ private unsafe ref byte InvokeWithFewArguments(
497497
object?[] parameters, BinderBundle? binderBundle, bool wrapInTargetInvocationException)
498498
{
499499
Debug.Assert(_argumentCount <= MaxStackAllocArgCount);
500-
int argCount = _argumentCount;
501500

502501
StackAllocatedArguments argStorage = default;
503-
Span<object?> copyOfParameters = argStorage._args.AsSpan(argCount);
502+
Span<object?> copyOfParameters = ((Span<object?>)argStorage._args).Slice(0, _argumentCount);
504503
StackAllocatedByRefs byrefStorage = default;
505504
#pragma warning disable CS8500
506505
void* pByRefStorage = (ByReference*)&byrefStorage;
@@ -532,10 +531,9 @@ private unsafe ref byte InvokeWithFewArguments(
532531
IntPtr methodToCall, ref byte thisArg, ref byte ret, Span<object?> parameters)
533532
{
534533
Debug.Assert(_argumentCount <= MaxStackAllocArgCount);
535-
int argCount = _argumentCount;
536534

537535
StackAllocatedArguments argStorage = default;
538-
Span<object?> copyOfParameters = argStorage._args.AsSpan(argCount);
536+
Span<object?> copyOfParameters = ((Span<object?>)argStorage._args).Slice(0, _argumentCount);
539537
StackAllocatedByRefs byrefStorage = default;
540538
#pragma warning disable CS8500
541539
void* pByRefStorage = (ByReference*)&byrefStorage;
@@ -884,19 +882,6 @@ private unsafe object ReturnTransform(ref byte byref, bool wrapInTargetInvocatio
884882
internal struct ArgumentData<T>
885883
{
886884
private T _arg0;
887-
888-
[UnscopedRef]
889-
public Span<T> AsSpan(int length)
890-
{
891-
Debug.Assert((uint)length <= MaxStackAllocArgCount);
892-
return new Span<T>(ref _arg0, length);
893-
}
894-
895-
public void Set(int index, T value)
896-
{
897-
Debug.Assert((uint)index < MaxStackAllocArgCount);
898-
Unsafe.Add(ref _arg0, index) = value;
899-
}
900885
}
901886

902887
// Helper struct to avoid intermediate object[] allocation in calls to the native reflection stack.

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/MethodInvoker.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public static MethodInvoker Create(MethodBase method)
8181
}
8282

8383
StackAllocatedArguments argStorage = default;
84-
argStorage._args.Set(0, arg1);
85-
argStorage._args.Set(1, arg2);
84+
argStorage._args[0] = arg1;
85+
argStorage._args[1] = arg2;
8686

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

100100
StackAllocatedArguments argStorage = default;
101-
argStorage._args.Set(0, arg1);
102-
argStorage._args.Set(1, arg2);
103-
argStorage._args.Set(2, arg3);
101+
argStorage._args[0] = arg1;
102+
argStorage._args[1] = arg2;
103+
argStorage._args[2] = arg3;
104104

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

118118
StackAllocatedArguments argStorage = default;
119-
argStorage._args.Set(0, arg1);
120-
argStorage._args.Set(1, arg2);
121-
argStorage._args.Set(2, arg3);
122-
argStorage._args.Set(3, arg4);
119+
argStorage._args[0] = arg1;
120+
argStorage._args[1] = arg2;
121+
argStorage._args[2] = arg3;
122+
argStorage._args[3] = arg4;
123123

124-
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
124+
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, ((Span<object?>)argStorage._args).Slice(0, 4));
125125
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
126126
return result;
127127
}

src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,21 @@ public static object CreateInstance(
6767
}
6868

6969
// Attempt to use the stack allocated arg values if <= 4 ctor args.
70-
Span<object?> values;
7170
StackAllocatedObjects stackValues = default;
7271
int maxArgs = GetMaxArgCount();
73-
if (maxArgs <= StackAllocatedObjects.MaxStackAllocArgCount / 2)
74-
{
75-
values = MemoryMarshal.CreateSpan(ref stackValues._args._arg0, maxArgs * 2);
76-
}
77-
else
78-
{
79-
values = new Span<object?>(new object?[maxArgs * 2], 0, maxArgs * 2);
80-
}
72+
Span<object?> values = maxArgs <= StackAllocatedObjects.MaxStackAllocArgCount / 2 ?
73+
stackValues :
74+
new object?[maxArgs * 2];
8175

8276
Span<object?> ctorArgs = values.Slice(0, maxArgs);
83-
Span<object?> bestCtorArgs = values.Slice(maxArgs);
77+
Span<object?> bestCtorArgs = values.Slice(maxArgs, maxArgs);
8478
#else
8579
constructors = CreateConstructorInfoExs(instanceType);
8680
object?[]? ctorArgs = null;
8781
object?[]? bestCtorArgs = null;
8882
#endif
8983

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

126120
int bestLength = -1;
127-
ConstructorMatcher bestMatcher = default;
121+
scoped ConstructorMatcher bestMatcher = default;
128122
bool multipleBestLengthFound = false;
129123

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

1226-
[StructLayout(LayoutKind.Sequential)]
1227-
private ref struct StackAllocatedObjects
1220+
[InlineArray(MaxStackAllocArgCount)]
1221+
private struct StackAllocatedObjects
12281222
{
12291223
internal const int MaxStackAllocArgCount = 8;
1230-
internal StackAllocatedObjectValues _args;
1231-
1232-
[InlineArray(MaxStackAllocArgCount)]
1233-
internal struct StackAllocatedObjectValues
1234-
{
1235-
internal object? _arg0;
1236-
}
1224+
private object? _arg0;
12371225
}
12381226
#endif
12391227

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@
454454
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\AhoCorasickBuilder.cs" />
455455
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\AhoCorasickNode.cs" />
456456
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\CharacterFrequencyHelper.cs" />
457-
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\EightPackedReferences.cs" />
458457
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\RabinKarp.cs" />
459458
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\StringSearchValuesHelper.cs" />
460459
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\Helpers\TeddyBucketizer.cs" />
@@ -472,6 +471,7 @@
472471
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\StringSearchValuesAhoCorasick.cs" />
473472
<Compile Include="$(MSBuildThisFileDirectory)System\SearchValues\Strings\StringSearchValuesRabinKarp.cs" />
474473
<Compile Include="$(MSBuildThisFileDirectory)System\IndexOutOfRangeException.cs" />
474+
<Compile Include="$(MSBuildThisFileDirectory)System\InlineArrays.cs" />
475475
<Compile Include="$(MSBuildThisFileDirectory)System\InsufficientExecutionStackException.cs" />
476476
<Compile Include="$(MSBuildThisFileDirectory)System\InsufficientMemoryException.cs" />
477477
<Compile Include="$(MSBuildThisFileDirectory)System\Int16.cs" />
@@ -615,7 +615,6 @@
615615
<Compile Include="$(MSBuildThisFileDirectory)System\OutOfMemoryException.cs" />
616616
<Compile Include="$(MSBuildThisFileDirectory)System\OverflowException.cs" />
617617
<Compile Include="$(MSBuildThisFileDirectory)System\ParamArrayAttribute.cs" />
618-
<Compile Include="$(MSBuildThisFileDirectory)System\ParamsArray.cs" />
619618
<Compile Include="$(MSBuildThisFileDirectory)System\ParseNumbers.cs" />
620619
<Compile Include="$(MSBuildThisFileDirectory)System\PasteArguments.cs" />
621620
<Compile Include="$(MSBuildThisFileDirectory)System\PlatformID.cs" />
@@ -2766,4 +2765,4 @@
27662765
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
27672766
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
27682767
</ItemGroup>
2769-
</Project>
2768+
</Project>

src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public override void Write([StringSyntax(StringSyntaxAttribute.CompositeFormat)]
531531
if (GetType() == typeof(StreamWriter))
532532
{
533533
TwoObjects two = new TwoObjects(arg0, arg1);
534-
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref two.Arg0, 2), appendNewLine: false);
534+
WriteFormatHelper(format, two, appendNewLine: false);
535535
}
536536
else
537537
{
@@ -544,7 +544,7 @@ public override void Write([StringSyntax(StringSyntaxAttribute.CompositeFormat)]
544544
if (GetType() == typeof(StreamWriter))
545545
{
546546
ThreeObjects three = new ThreeObjects(arg0, arg1, arg2);
547-
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref three.Arg0, 3), appendNewLine: false);
547+
WriteFormatHelper(format, three, appendNewLine: false);
548548
}
549549
else
550550
{
@@ -585,7 +585,7 @@ public override void WriteLine([StringSyntax(StringSyntaxAttribute.CompositeForm
585585
if (GetType() == typeof(StreamWriter))
586586
{
587587
TwoObjects two = new TwoObjects(arg0, arg1);
588-
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref two.Arg0, 2), appendNewLine: true);
588+
WriteFormatHelper(format, two, appendNewLine: true);
589589
}
590590
else
591591
{
@@ -598,7 +598,7 @@ public override void WriteLine([StringSyntax(StringSyntaxAttribute.CompositeForm
598598
if (GetType() == typeof(StreamWriter))
599599
{
600600
ThreeObjects three = new ThreeObjects(arg0, arg1, arg2);
601-
WriteFormatHelper(format, MemoryMarshal.CreateReadOnlySpan(ref three.Arg0, 3), appendNewLine: true);
601+
WriteFormatHelper(format, three, appendNewLine: true);
602602
}
603603
else
604604
{
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
// These types are temporary workarounds for an inability to stackalloc object references.
5-
// Once we're able to do `stackalloc object[n]`, these can be removed.
6-
7-
// Suppress warnings for unused private fields
8-
#pragma warning disable CS0169, CA1823, IDE0051, IDE0044
9-
10-
using System.Diagnostics.CodeAnalysis;
114
using System.Runtime.CompilerServices;
125

136
namespace System
147
{
158
[InlineArray(2)]
169
internal struct TwoObjects
1710
{
18-
internal object? Arg0;
11+
private object? _arg0;
1912

2013
public TwoObjects(object? arg0, object? arg1)
2114
{
@@ -27,7 +20,7 @@ public TwoObjects(object? arg0, object? arg1)
2720
[InlineArray(3)]
2821
internal struct ThreeObjects
2922
{
30-
internal object? Arg0;
23+
private object? _arg0;
3124

3225
public ThreeObjects(object? arg0, object? arg1, object? arg2)
3326
{
@@ -36,4 +29,10 @@ public ThreeObjects(object? arg0, object? arg1, object? arg2)
3629
this[2] = arg2;
3730
}
3831
}
32+
33+
[InlineArray(8)]
34+
internal struct EightObjects
35+
{
36+
private object? _ref0;
37+
}
3938
}

src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ internal object InvokeWithFewArgs(Span<object?> arguments)
245245
Debug.Assert(_argCount <= MaxStackAllocArgCount);
246246

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

251251
for (int i = 0; i < _argCount; i++)
252252
{
@@ -279,7 +279,7 @@ internal object InvokeWithFewArgs(Span<object?> arguments)
279279
internal object InvokeDirectByRef(object? arg1 = null, object? arg2 = null, object? arg3 = null, object? arg4 = null)
280280
{
281281
StackAllocatedArguments stackStorage = new(arg1, arg2, arg3, arg4);
282-
return InvokeDirectByRefWithFewArgs(stackStorage._args.AsSpan(_argCount));
282+
return InvokeDirectByRefWithFewArgs(((Span<object?>)stackStorage._args).Slice(0, _argCount));
283283
}
284284

285285
internal unsafe object InvokeDirectByRefWithFewArgs(Span<object?> copyOfArgs)

src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,6 @@ internal enum InvokerArgFlags : int
190190
internal struct ArgumentData<T>
191191
{
192192
private T _arg0;
193-
194-
[UnscopedRef]
195-
public Span<T> AsSpan(int length)
196-
{
197-
Debug.Assert((uint)length <= MaxStackAllocArgCount);
198-
return new Span<T>(ref _arg0, length);
199-
}
200-
201-
public void Set(int index, T value)
202-
{
203-
Debug.Assert((uint)index < MaxStackAllocArgCount);
204-
Unsafe.Add(ref _arg0, index) = value;
205-
}
206193
}
207194

208195
// Helper struct to avoid intermediate object[] allocation in calls to the native reflection stack.
@@ -214,10 +201,10 @@ internal ref struct StackAllocatedArguments
214201
{
215202
public StackAllocatedArguments(object? obj1, object? obj2, object? obj3, object? obj4)
216203
{
217-
_args.Set(0, obj1);
218-
_args.Set(1, obj2);
219-
_args.Set(2, obj3);
220-
_args.Set(3, obj4);
204+
_args[0] = obj1;
205+
_args[1] = obj2;
206+
_args[2] = obj3;
207+
_args[3] = obj4;
221208
}
222209

223210
internal ArgumentData<object?> _args;

src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ internal static void ThrowTargetParameterCountException()
118118
Debug.Assert(_argCount <= MaxStackAllocArgCount);
119119

120120
StackAllocatedArgumentsWithCopyBack stackArgStorage = default;
121-
Span<object?> copyOfArgs = stackArgStorage._args.AsSpan(_argCount);
122-
Span<bool> shouldCopyBack = stackArgStorage._shouldCopyBack.AsSpan(_argCount);
121+
Span<object?> copyOfArgs = ((Span<object?>)stackArgStorage._args).Slice(0, _argCount);
122+
Span<bool> shouldCopyBack = ((Span<bool>)stackArgStorage._shouldCopyBack).Slice(0, _argCount);
123123

124124
object? ret;
125125
if ((_strategy & InvokerStrategy.StrategyDetermined_ObjSpanArgs) == 0)

0 commit comments

Comments
 (0)