CodeQL 8: perf: replace ContainsKey+indexer with TryGetValue#196
Conversation
📝 WalkthroughWalkthroughSix files refactored to replace ChangesDictionary and ViewData access pattern consolidation
🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 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)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bundle ReportBundle size has no change ✅ |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #196 +/- ##
==========================================
- Coverage 43.02% 43.02% -0.01%
==========================================
Files 881 881
Lines 51436 51437 +1
Branches 4812 4812
==========================================
Hits 22131 22131
- Misses 28779 28780 +1
Partials 526 526
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
4a20f4f to
0aa8b69
Compare
|
@bsedwards Straightforward replacement of ContainsKey with TryGetValue: |
Summary
Closes ~14 of 18
cs/inefficient-containskeyalerts. Two distinct cleanups bundled together:ContainsKey + indexer(two dictionary lookups) withTryGetValue(one lookup) at 4 call-sites.ContainsKeyguard in 4 views;ViewData["X"] ?? falsealready handles missing keys.What's actually a perf change
dict.ContainsKey(k) + dict[k]does two hash computations and two bucket walks.TryGetValuedoes one — roughly 2× cheaper per access. String-key sites get the biggest gain becausestring.GetHashCode()walks every character.RotationsController.cs:498-511ContainsKey+ 3× indexer) inside a.SelectprojectionTryGetValueper rowRAPSAuditService.cs:144VMACSExport.cs:302user.MothraId.Trim()Trim()VMACSExport.cs:344(GetServerUrl)The Rotations site is the headline: 6× reduction in dictionary operations per row in the per-week projection. Absolute wall-clock impact is on the order of nanoseconds per lookup, so this is micro-optimization, not something a profiler will flag — but it's free given the alert is firing anyway.
Secondary benefit:
TryGetValueis a single atomic lookup, so it's TOCTOU-safe against concurrent dictionary mutation. Not load-bearing for these specific callsites (the dicts aren't shared cross-thread), but it's a strictly safer pattern.What's NOT a perf change (despite the PR label)
The Razor changes are simplification, not perf:
ViewData.ContainsKey(\"X\") && (bool)(ViewData[\"X\"] ?? false)→(bool)(ViewData[\"X\"] ?? false)ViewData[\"missingKey\"]returnsnull, not an exception, so theContainsKeyguard was always redundant once the?? falseis there.Touches
Areas/RAPS/Views/Members/List.cshtml(×4),Members/Roles.cshtml(×1),Roles/Members.cshtml(×1),Roles/Templates.cshtml(×4). Also strips the UTF-8 BOM from those four files — encoding hygiene, zero runtime impact.Not addressed: 1 site in
test/Students/EmergencyContactControllerTests.cs:319(test infrastructure) and ~3 likely already covered or out-of-scope.Test plan
npm run test:backend— 1946 tests passing