-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Use constant indices when populating Lists and arrays in collection expressions #80999
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
536a406
Initial plan
Copilot fcb6eb3
Use constant indices when populating span for List in collection expr…
Copilot c210cc9
Update src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewr…
CyrusNajmabadi 738dcea
Update src/Compilers/CSharp/Portable/Lowering/LocalRewriter/LocalRewr…
CyrusNajmabadi 7aa7cab
Fix
CyrusNajmabadi 877b12b
Add test for fixed elements before spread in collection expressions
Copilot bd54c95
Add WorkItem attribute to ListConstruction_FixedElementsBeforeSpread …
Copilot 9ffa846
Apply constant indices optimization to arrays
Copilot f9b7579
Simplify code by using null state of indexTemp
Copilot b5ed80e
Improve documentation and inline condition check
Copilot 38fdece
Add comments explaining that spreads always require runtime-tracked i…
Copilot 2fab349
Add WorkItem attributes to all updated tests
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22291,6 +22291,123 @@ .maxstack 3 | |
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ListConstruction_FixedElementsBeforeSpread() | ||
| { | ||
| // Test that when there are fixed elements before a spread element, | ||
| // we use an index variable for ALL elements (not constant indices). | ||
| string source = """ | ||
| using System.Collections.Generic; | ||
| class Program | ||
| { | ||
| static void Main() | ||
| { | ||
| List<int> y = [5, 6]; | ||
| List<int> x = [1, 2, ..y]; | ||
| x.Report(); | ||
| } | ||
| } | ||
| """; | ||
| var verifier = CompileAndVerify( | ||
| new[] { source, s_collectionExtensionsWithSpan }, | ||
| targetFramework: TargetFramework.Net80, | ||
| verify: Verification.Skipped, | ||
| expectedOutput: IncludeExpectedOutput("[1, 2, 5, 6], ")); | ||
| verifier.VerifyDiagnostics(); | ||
|
|
||
| // Verify that we use an index variable (V_6) for all elements, including the fixed elements before the spread | ||
| verifier.VerifyIL("Program.Main", """ | ||
| { | ||
| // Code size 167 (0xa7) | ||
| .maxstack 5 | ||
| .locals init (int V_0, | ||
| System.Span<int> V_1, | ||
| int V_2, | ||
| System.Collections.Generic.List<int> V_3, | ||
| int V_4, | ||
| System.Span<int> V_5, | ||
| int V_6, | ||
| System.Span<int> V_7) | ||
| IL_0000: ldc.i4.2 | ||
| IL_0001: stloc.0 | ||
| IL_0002: ldloc.0 | ||
| IL_0003: newobj "System.Collections.Generic.List<int>..ctor(int)" | ||
| IL_0008: dup | ||
| IL_0009: ldloc.0 | ||
| IL_000a: call "void System.Runtime.InteropServices.CollectionsMarshal.SetCount<int>(System.Collections.Generic.List<int>, int)" | ||
| IL_000f: dup | ||
| IL_0010: call "System.Span<int> System.Runtime.InteropServices.CollectionsMarshal.AsSpan<int>(System.Collections.Generic.List<int>)" | ||
| IL_0015: stloc.1 | ||
| IL_0016: ldloca.s V_1 | ||
| IL_0018: ldc.i4.0 | ||
| IL_0019: call "ref int System.Span<int>.this[int].get" | ||
| IL_001e: ldc.i4.5 | ||
| IL_001f: stind.i4 | ||
| IL_0020: ldloca.s V_1 | ||
| IL_0022: ldc.i4.1 | ||
| IL_0023: call "ref int System.Span<int>.this[int].get" | ||
| IL_0028: ldc.i4.6 | ||
| IL_0029: stind.i4 | ||
| IL_002a: ldc.i4.1 | ||
| IL_002b: stloc.0 | ||
| IL_002c: ldc.i4.2 | ||
| IL_002d: stloc.2 | ||
| IL_002e: stloc.3 | ||
| IL_002f: ldc.i4.2 | ||
| IL_0030: ldloc.3 | ||
| IL_0031: callvirt "int System.Collections.Generic.List<int>.Count.get" | ||
| IL_0036: add | ||
| IL_0037: stloc.s V_4 | ||
| IL_0039: ldloc.s V_4 | ||
| IL_003b: newobj "System.Collections.Generic.List<int>..ctor(int)" | ||
| IL_0040: dup | ||
| IL_0041: ldloc.s V_4 | ||
| IL_0043: call "void System.Runtime.InteropServices.CollectionsMarshal.SetCount<int>(System.Collections.Generic.List<int>, int)" | ||
| IL_0048: dup | ||
| IL_0049: call "System.Span<int> System.Runtime.InteropServices.CollectionsMarshal.AsSpan<int>(System.Collections.Generic.List<int>)" | ||
| IL_004e: stloc.s V_5 | ||
| IL_0050: ldc.i4.0 | ||
| IL_0051: stloc.s V_6 | ||
| IL_0053: ldloca.s V_5 | ||
| IL_0055: ldloc.s V_6 | ||
| IL_0057: call "ref int System.Span<int>.this[int].get" | ||
| IL_005c: ldloc.0 | ||
| IL_005d: stind.i4 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shows that we're still incrementing. |
||
| IL_005e: ldloc.s V_6 | ||
| IL_0060: ldc.i4.1 | ||
| IL_0061: add | ||
| IL_0062: stloc.s V_6 | ||
| IL_0064: ldloca.s V_5 | ||
| IL_0066: ldloc.s V_6 | ||
| IL_0068: call "ref int System.Span<int>.this[int].get" | ||
| IL_006d: ldloc.2 | ||
| IL_006e: stind.i4 | ||
| IL_006f: ldloc.s V_6 | ||
| IL_0071: ldc.i4.1 | ||
| IL_0072: add | ||
| IL_0073: stloc.s V_6 | ||
| IL_0075: ldloc.3 | ||
| IL_0076: call "System.Span<int> System.Runtime.InteropServices.CollectionsMarshal.AsSpan<int>(System.Collections.Generic.List<int>)" | ||
| IL_007b: stloc.s V_7 | ||
| IL_007d: ldloca.s V_7 | ||
| IL_007f: ldloca.s V_5 | ||
| IL_0081: ldloc.s V_6 | ||
| IL_0083: ldloca.s V_7 | ||
| IL_0085: call "int System.Span<int>.Length.get" | ||
| IL_008a: call "System.Span<int> System.Span<int>.Slice(int, int)" | ||
| IL_008f: call "void System.Span<int>.CopyTo(System.Span<int>)" | ||
| IL_0094: ldloc.s V_6 | ||
| IL_0096: ldloca.s V_7 | ||
| IL_0098: call "int System.Span<int>.Length.get" | ||
| IL_009d: add | ||
| IL_009e: stloc.s V_6 | ||
| IL_00a0: ldc.i4.0 | ||
| IL_00a1: call "void CollectionExtensions.Report(object, bool)" | ||
| IL_00a6: ret | ||
| } | ||
| """); | ||
| } | ||
|
|
||
| [CombinatorialData] | ||
| [Theory] | ||
| public void ListConstruction_02( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot add
[Fact, WorkItem("...")]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added WorkItem attribute in commit bd54c95.