-
Notifications
You must be signed in to change notification settings - Fork 595
Replace Enumerable.Range().Select() with direct for loops to reduce allocations #9825
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
base: master
Are you sure you want to change the base?
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 optimizes performance-critical code paths by replacing LINQ-based collection initialization with direct for loops, eliminating unnecessary intermediate allocations from Enumerable.Range().Select() chains.
Key Changes:
- Replaced LINQ-based task collection creation with direct
forloops in two high-throughput components - Properly handled loop variable capture in closures to avoid common pitfalls
- Removed unused
using System.Linqdirective where applicable
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Nethermind/Nethermind.Trie/BatchedTrieVisitor.cs |
Replaced LINQ chain with direct for loop for task creation in batched trie visiting; removed unused using System.Linq directive |
src/Nethermind/Nethermind.Network/PeerManager.cs |
Replaced LINQ chain with direct for loop for worker task creation; correctly captured loop variable to avoid closure issues |
| for (int idx = 0; idx < _outgoingConnectParallelism; idx++) | ||
| { | ||
| await foreach (Peer peer in taskChannel.Reader.ReadAllAsync(_cancellationTokenSource.Token)) | ||
| int workerIdx = idx; |
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.
we can probably avoid closure with something like:
for (int idx = 0; idx < _outgoingConnectParallelism; idx++)
{
tasks.Add(RunWorker(idx));
}
async Task RunWorker(int workerIdx)
{
await foreach (Peer peer in taskChannel.Reader.ReadAllAsync(_cancellationTokenSource.Token))
{
try
{
await SetupOutgoingPeerConnection(peer);
}
catch (TaskCanceledException)
{
if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} cancelled");
break;
}
catch (Exception e)
{
if (_logger.IsError) _logger.Error($"Error setting up connection to {peer}, {e}");
}
}
if (_logger.IsDebug) _logger.Debug($"Connect worker {workerIdx} completed");
}
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.
Thanks for the suggestion! I switched the loop to call a local RunWorker(int workerIdx) helper so each worker receives its own index instead of capturing the loop variable. This removes the closure while keeping the behavior the same.
|
Probably whitespace is off |
Replaces LINQ-based task collection initialization with direct
forloops in performance-critical code paths, eliminating unnecessary intermediate allocations.