Repository navigation
Replace some stackallocs with collection expressions - #93126
Conversation
These will result in using an InlineArray rather than a localloc
|
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsThese will result in using an InlineArray rather than a localloc
|
tannergooding
left a comment
There was a problem hiding this comment.
LGTM.
Would be good to resolve the styling question I asked on the other PR and update if we decide having the space is better
gfoidl
left a comment
There was a problem hiding this comment.
@stephentoub I played around with collection expression, and therefore your PRs are always a welcoming guide to get familiar with new features.
But here something seems strange, where either I miss something or this PR is actually a de-optimization.
will result in using an InlineArray rather than a localloc
When looking at the generated IL or reverse engineered C# with sharplab (main, 12 Oct 2023, so quite actual) I see array allocations instead the use of InlineArrays.
Can you please have a look and clarify?
I didn't comment on all places, to avoid noise.
|
|
||
| string[] result = new string[count]; | ||
| Span<char> root = stackalloc char[] { 'A', ':', '\\' }; | ||
| Span<char> root = ['A', ':', '\\']; |
There was a problem hiding this comment.
According to sharplab, main 12 Oct 2023 this allocates.
Looks like the collection expression only avoid the allocation for ROS, but a ROS can't be used here.
|
|
||
| // Make local copy of data to avoid modifying input. | ||
| Span<uint> rgulNumeric = stackalloc uint[4] { _data1, _data2, _data3, _data4 }; | ||
| Span<uint> rgulNumeric = [_data1, _data2, _data3, _data4]; |
| ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_Need2DArray); | ||
|
|
||
| return InternalGetValue(GetFlattenedIndex(stackalloc int[] { index1, index2 })); | ||
| return InternalGetValue(GetFlattenedIndex([index1, index2])); |
| Debug.Assert(char.IsLowSurrogate(l)); | ||
|
|
||
| Span<char> chars = stackalloc char[] { h, l }; | ||
| ReadOnlySpan<char> chars = [h, l]; |
|
sharplab is targeting a runtime that doesn't have InlineArray yet. Its main is about roslyn, not runtime. |
|
Locally with .NET 8 RC2 I also see the array allocations ( For example: static void Test0(int a, int b)
{
Span<int> foo = [a, b];
ReadOnlySpan<int> bar = [a, b];
}emits .method assembly hidebysig static
void '<<Main>$>g__Test0|0_0' (
int32 a,
int32 b
) cil managed
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2080
// Header size: 12
// Code size: 44 (0x2c)
.maxstack 5
.locals init (
[0] valuetype [System.Runtime]System.Span`1<int32> foo,
[1] valuetype [System.Runtime]System.ReadOnlySpan`1<int32> bar
)
IL_0000: nop
IL_0001: ldloca.s 0
IL_0003: ldc.i4.2
IL_0004: newarr [System.Runtime]System.Int32 // <-- newarr for Span<int> foo = [a, b];
IL_0009: dup
IL_000a: ldc.i4.0
IL_000b: ldarg.0
IL_000c: stelem.i4
IL_000d: dup
IL_000e: ldc.i4.1
IL_000f: ldarg.1
IL_0010: stelem.i4
IL_0011: call instance void valuetype [System.Runtime]System.Span`1<int32>::.ctor(!0[])
IL_0016: ldloca.s 1
IL_0018: ldc.i4.2
IL_0019: newarr [System.Runtime]System.Int32 // <-- newarr for ReadOnlySpan<int> bar = [a, b];
IL_001e: dup
IL_001f: ldc.i4.0
IL_0020: ldarg.0
IL_0021: stelem.i4
IL_0022: dup
IL_0023: ldc.i4.1
IL_0024: ldarg.1
IL_0025: stelem.i4
IL_0026: call instance void valuetype [System.Runtime]System.ReadOnlySpan`1<int32>::.ctor(!0[])
IL_002b: ret
} // end of method Program::'<<Main>$>g__Test0|0_0'Or when running: int a = 3;
int b = 4;
long allocated0 = GC.GetTotalAllocatedBytes(precise: true);
Test0(a, b);
long allocated1 = GC.GetTotalAllocatedBytes(precise: true);
Console.WriteLine(allocated1 - allocated0);
static void Test0(int a, int b)
{
Span<int> foo = [a, b];
ReadOnlySpan<int> bar = [a, b];
}it prints (in Release): but I'd expect with that optimization to be 0. Or when looking at godbolt (main) I see the two allocations that should not be there. |
|
Sounds like your C# compiler is too old. For your Test0, I get the equivalent of this: private static void Test0(int a, int b)
{
<>y__InlineArray2<int> buffer = default(<>y__InlineArray2<int>);
global::<PrivateImplementationDetails>.InlineArrayElementRef<<>y__InlineArray2<int>, int>(ref buffer, 0) = a;
global::<PrivateImplementationDetails>.InlineArrayElementRef<<>y__InlineArray2<int>, int>(ref buffer, 1) = b;
global::<PrivateImplementationDetails>.InlineArrayAsSpan<<>y__InlineArray2<int>, int>(ref buffer, 2);
<>y__InlineArray2<int> buffer2 = default(<>y__InlineArray2<int>);
global::<PrivateImplementationDetails>.InlineArrayElementRef<<>y__InlineArray2<int>, int>(ref buffer2, 0) = a;
global::<PrivateImplementationDetails>.InlineArrayElementRef<<>y__InlineArray2<int>, int>(ref buffer2, 1) = b;
global::<PrivateImplementationDetails>.InlineArrayAsReadOnlySpan<<>y__InlineArray2<int>, int>(in buffer2, 2);
} |
|
Fine, than it's as intended / expected. Thanks for clarification!
Today .NET 8 (RC2 and RTM (latest 8.0.1xx)) is already too old for this optimization... |
That should not be the case. Are you building in VS or from the command line? Regardless, you need a compiler with this: |
VS tricked me into this. Thanks! |
|
This broke a bunch of |
Sounds like it's an issue with the mono interpreter not handling [InlineArray]s correctly. |
These will result in using an InlineArray rather than a localloc