-
Notifications
You must be signed in to change notification settings - Fork 1
[EPIC-5-PERF] T-W1-FIX: Actually Eliminate LINQ Allocations #13
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -476,15 +476,14 @@ private void ShouldSkipFleet_RunHealthCheck(Account acct, StringBuilder dispatch | |
| { | ||
| try | ||
| { | ||
| // [939-P0]: Snapshot Positions to prevent broker-thread mutation during iteration. | ||
| // T-W1-Perf: for-loop replaces FirstOrDefault lambda -- eliminates delegate allocation. | ||
| Position[] _posSnapshot = acct.Positions.ToArray(); | ||
| // T-W1-Perf: Direct iteration over Positions collection - zero heap allocations | ||
| // Early-break on match preserves O(1) best-case performance | ||
| Position brokerPos = null; | ||
| for (int _pi = 0; _pi < _posSnapshot.Length; _pi++) | ||
| foreach (var pos in acct.Positions) | ||
| { | ||
|
Comment on lines
+482
to
483
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. Suggestion: Iterating Severity Level: Major
|
||
| if (_posSnapshot[_pi] != null && _posSnapshot[_pi].Instrument.FullName == Instrument.FullName) | ||
| if (pos != null && pos.Instrument.FullName == Instrument.FullName) | ||
|
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. Suggestion: The predicate dereferences Severity Level: Major
|
||
| { | ||
| brokerPos = _posSnapshot[_pi]; | ||
| brokerPos = pos; | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
481
to
489
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. Concurrency & Stability RiskDirectly enumerating Additionally, RecommendationTo preserve the performance optimization while guaranteeing 100% stability, we can attempt the allocation-free direct iteration first, and gracefully fall back to a safe Position brokerPos = null;
try
{
foreach (var pos in acct.Positions)
{
if (pos != null && pos.Instrument.FullName == Instrument.FullName)
{
brokerPos = pos;
break;
}
}
}
catch (InvalidOperationException)
{
// Fallback to snapshot if concurrent modification occurs
Position[] _posSnapshot = acct.Positions.ToArray();
for (int _pi = 0; _pi < _posSnapshot.Length; _pi++)
{
if (_posSnapshot[_pi] != null && _posSnapshot[_pi].Instrument.FullName == Instrument.FullName)
{
brokerPos = _posSnapshot[_pi];
break;
}
}
}
Comment on lines
+479
to
489
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. 🚨 Bug: Removing ToArray() snapshot risks InvalidOperationException on broker thread mutationThe original code used The project's own documentation ( Direct The allocation cost of Restore the ToArray() snapshot to maintain thread safety against broker-thread mutation. The for-loop (vs LINQ FirstOrDefault) still eliminates the delegate/closure allocation that was the original perf concern.: Was this helpful? React with 👍 / 👎
Comment on lines
+479
to
489
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.
The deleted comment explicitly documented the reason for
Comment on lines
+482
to
489
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. 🔴 HIGH RISK Removing the Additionally, if |
||
|
|
||
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.
The PR description and new comment claim
foreach (var pos in acct.Positions)produces "zero heap allocations." This is only true ifacct.Positionsexposes a struct-basedGetEnumerator(). NinjaTrader'sPositionCollection(the type returned byAccount.Positions) returns a class-basedIEnumerator<Position>, meaning eachforeachcall allocates a new enumerator object on the heap. The optimization claim that drove this PR may be factually incorrect — the allocation is moved from an explicitPosition[]array to an implicit enumerator object, which is still a hot-path allocation per the ALLOCATION IS A BUG principle.