-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Switch ItemExpressionCapture to a struct #12150
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
Switch ItemExpressionCapture to a struct #12150
Conversation
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.
Pull Request Overview
This PR changes ItemExpressionCapture from a class to a struct to reduce heap allocations, replaces the list-based item-expression capture with a struct-based enumerator, and updates all callers and tests to use the new pattern.
- Convert
ItemExpressionCaptureto a struct and make capture methods return nullable structs - Introduce
ReferencedItemExpressionsEnumeratorin place ofList<ItemExpressionCapture> - Update callers and unit tests to use
MoveNext()andCurrent
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Build/Evaluation/LazyItemEvaluator.cs | Unwrap nullable struct before passing to helper |
| src/Build/Evaluation/ItemSpec.cs | Unwrap nullable struct when constructing fragments |
| src/Build/Evaluation/ExpressionShredder.cs | Define ReferencedItemExpressionsEnumerator and refactor enumeration code |
| src/Build/Evaluation/Expander.cs | Adapt callers to use the new struct enumerator |
| src/Build.UnitTests/Evaluation/ExpressionShredder_Tests.cs | Update tests to use MoveNext() and Current |
Comments suppressed due to low confidence (3)
src/Build/Evaluation/ExpressionShredder.cs:103
- [nitpick] Consider adding XML doc-comments for
MoveNextandCurrentto clarify usage, and indicate that reset behavior is not supported.
internal struct ReferencedItemExpressionsEnumerator
src/Build/Evaluation/ExpressionShredder.cs:107
- [nitpick] The field
currentIndexis a bit generic; consider renaming to something likescanPositionorcursorto better convey its role in the enumerator.
private int currentIndex;
src/Build/Evaluation/ExpressionShredder.cs:103
- [nitpick] Rather than a standalone struct, consider implementing
IEnumerator<ItemExpressionCapture>to align with standard .NET enumeration patterns and enable use inforeach.
internal struct ReferencedItemExpressionsEnumerator
…rndt/ItemExpressionCapture
Fixes # ### Context We can avoid several allocations by changing the usage of `ItemExpressionCapture`. Switching from a class to a struct avoids a large number of allocations, and we can further reduce the allocations using a struct-based enumerator rather than constructing a list. Before: <img width="830" height="79" alt="image" src="https://github.com/user-attachments/assets/d010528f-5cb2-4af5-8d7f-dd0d077ab3e3" /> After: <img width="832" height="65" alt="image" src="https://github.com/user-attachments/assets/44f5beef-f508-4bb3-a508-42a7044539a6" /> ### Changes Made ### Testing ### Notes
Fixes #
Context
We can avoid several allocations by changing the usage of
ItemExpressionCapture. Switching from a class to a struct avoids a large number of allocations, and we can further reduce the allocations using a struct-based enumerator rather than constructing a list.Before:

After:

Changes Made
Testing
Notes