perf(densenet): O(n²) IndexOf-in-foreach in DenseBlock.SetExtraParameters - #1241
Conversation
…enseBlock.SetExtraParameters DenseBlock<T>.SetExtraParameters used `_layers.IndexOf(layer)` inside a `foreach` loop. The IndexOf call only appears inside the truncation error message, but it ran on every iteration and turned the loop O(n²). Switched to an index-tracked `for` loop so the index is available in O(1) for the error message and the happy path is plain O(n). Reviewer-flagged in PR #1229; PR was merged before the comment was addressed, so applying the fix as a small follow-up here. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughThe ChangesLayer Iteration & Error Reporting Optimization
Estimated Code Review Effort🎯 1 (Trivial) | ⏱️ ~4 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Review rate limit: 3/5 reviews remaining, refill in 15 minutes and 38 seconds. Comment |
There was a problem hiding this comment.
Pull request overview
This PR applies a small follow-up performance fix in DenseNet’s DenseBlock<T> serialization path by removing an unnecessary List.IndexOf call from the SetExtraParameters loop. It keeps the same behavior while avoiding quadratic work in a method that iterates across the block’s child layers.
Changes:
- Replace
foreach+_layers.IndexOf(layer)with an index-basedforloop inDenseBlock<T>.SetExtraParameters. - Preserve the truncation error message semantics while computing the layer index in O(1) per iteration.
- Keep DenseBlock extra-parameter deserialization behavior otherwise unchanged.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
_layers.IndexOf(layer)insideforeachwith an index-trackedforloop inDenseBlock<T>.SetExtraParameters. The IndexOf only appears in the truncation error message but ran on every iteration, turning the loop O(n²).Background
Reviewer-flagged in PR #1229 (PRRT_kwDOKSXUF85_NPlD). PR #1229 merged before the comment was addressed, so applying the fix here as a small follow-up.
Test plan
dotnet buildclean on net10.0 + net471🤖 Generated with Claude Code
Summary by CodeRabbit