JIT fails to escape simple usage of `List<>` and `[]`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- compilers, performance
Research direction
Start with the ListUsage, ArrayUsage, and Expected methods and reproduce their generated machine code using the linked Godbolt example against the main branch. Investigate the JIT behavior for the local List and int[] allocations and constant indexing; done means the intended optimization is implemented and verified by comparing the generated code.
Written by the indexing model from the issue text.
Description
Was tinkering around JIT generated code for List<> and raw arrays for very simple below code.
public static void ListUsage()
{
List<int> numbers = new(1);
numbers.Add(1);
Console.WriteLine(numbers[0]);
}
public static void ArrayUsage()
{
int[] numbers = new int[1] {1};
Console.WriteLine(numbers[0]);
}
public static void Expected()
{
Console.WriteLine(1);
}
The code is very self explanatory, nothing magical or complex as such. I was comparing the machine code generated by each method. I was expecting JIT to see the code flow and optimize the code gen generated for ListUsage and ArrayUsage to be similar as Expected method, but it does not.
Below are things JIT can see and I would expect it to optimize it further.
- JIT can see
List<int> numbers(in methodListUsage) andint[] numbers(in methodArrayUsage) are declared inside the method and get destroyed in method itself at the end (does not get pass to any other method). It even sees the maximum size of those data structure which is1so ideally it should stack allocate those instead of doing heap allocation. - The number that is getting added to collection is pure constant (1), so its not dynamic value. And on very next line we are even printing it directly by indexing and after that the method ends. So ideally JIT should see this flow and should optimize the code gen just like the code-gen generated by
Expectedmethod in above code.
Tested against main branch on CC => https://godbolt.org/z/eWv6Yaoaj
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·