Skip to content

[clr-interp] Fix performance of compiling huge methods#119514

Merged
davidwrighton merged 2 commits intodotnet:mainfrom
davidwrighton:fix_huge_method_compile_time
Sep 11, 2025
Merged

[clr-interp] Fix performance of compiling huge methods#119514
davidwrighton merged 2 commits intodotnet:mainfrom
davidwrighton:fix_huge_method_compile_time

Conversation

@davidwrighton
Copy link
Member

@davidwrighton davidwrighton commented Sep 9, 2025

  • Remove O(n) algorithm in InterpCompiler::EmitCodeIns which was debug only, replace with O(1) algorithm
  • The new conservative range computuation code also has an O(N^2) algorithm for ranges, and this is replaced with an O(n*logN) algorithm with better constant factors (Binary search in an array instead of linear scan of a linked list)
  • Adjust apis on TArray so that the RemoveAt and Remove functions don't re-order the array. Change the names of the existing apis to have a Unordered suffix, and add more typical implementations of RemoveAt and InsertAt

- Remove O(n) algorithm in InterpCompiler::EmitCodeIns which was debug only, replace with O(1) algorithm
- The new conservative range computuation code also has an O(N^2) algorithm for ranges, and this is replaced with an O(n*logN) algorithm with better constant factors (Binary search in an array instead of linear scan of a linked list)
- Adjust apis on TArray so that the RemoveAt and Remove functions don't re-order the array. Change the names of the existing apis to have a _ArrayIsBag suffix, and add more typical implementations of RemoveAt and InsertAt
Copilot AI review requested due to automatic review settings September 9, 2025 23:45
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 9, 2025
@davidwrighton davidwrighton added area-CodeGen-Interpreter-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Sep 9, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the performance of compiling large methods in the CLR interpreter by addressing O(N²) and O(N) algorithmic bottlenecks in critical compilation paths.

Key changes:

  • Replaces O(N) debug-only IL offset validation with O(1) hash table lookup using a side table
  • Refactors conservative GC range computation from O(N²) linked list operations to O(N log N) binary search with array-based storage
  • Enhances TArray class with proper order-preserving RemoveAt/InsertAt methods while renaming existing bag-style operations

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/interpreter/datastructs.h Adds Set method, order-preserving RemoveAt/InsertAt, and renames bag-style operations
src/coreclr/interpreter/compileropt.cpp Updates method calls to use renamed bag-style array operations
src/coreclr/interpreter/compiler.h Adds debug-only side table field and fixes operator formatting
src/coreclr/interpreter/compiler.cpp Implements O(1) IL offset validation and O(N log N) conservative range computation

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @janvorli, @kg
See info in area-owners.md if you want to be subscribed.

@davidwrighton davidwrighton marked this pull request as draft September 9, 2025 23:48
@davidwrighton
Copy link
Member Author

Oops, the allocation changes aren't safe. Sigh. I'll have to think on that.

…and one based on malloc/free

- Fix the TArray InsertAt function
Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo the unused local comment, thank you!

@davidwrighton davidwrighton merged commit 8fd0afd into dotnet:main Sep 11, 2025
95 of 97 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants