Skip to content

fix: optimize ConsoleViewModel trimming and LogsViewModel batch dispatch#422

Merged
laurentiu021 merged 1 commit into
mainfrom
fix/perf-console-logs
May 18, 2026
Merged

fix: optimize ConsoleViewModel trimming and LogsViewModel batch dispatch#422
laurentiu021 merged 1 commit into
mainfrom
fix/perf-console-logs

Conversation

@laurentiu021

@laurentiu021 laurentiu021 commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Resolves two performance LOWs from the full code review.

Changes

ConsoleViewModel O(n^2) trimming

  • Same clear-and-rebuild optimization as PERF-M3 (NetworkSharedState)
  • When excess lines exceed 25%% of buffer, snapshot kept items, clear, re-add
  • Reduces worst-case from O(n*excess) to O(n)

LogsViewModel batch dispatch

  • Previously dispatched each event entry individually to the UI thread (500 entries = 500 dispatcher calls)
  • Now batches entries in groups of 50 before dispatching
  • Reduces dispatcher overhead by ~98%% for large event log loads
  • UI remains responsive — batches are small enough to not block rendering

Testing

  • Build: 0 errors
  • No behavioral changes — same data displayed, faster rendering

Summary by CodeRabbit

  • Chores
    • Improved console view performance during buffer management with an optimized trimming strategy for large data sets
    • Enhanced event log loading performance through optimized batch processing of log entries
    • Updated changelog documenting recent performance improvements

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR optimizes the UI thread dispatch and buffer management in two ViewModels. ConsoleViewModel now uses a conditional trimming strategy for its capped line buffer, and LogsViewModel batches event entries before dispatching them to the UI, replacing per-item posting.

Changes

UI ViewModel Performance Optimizations

Layer / File(s) Summary
ConsoleViewModel conditional buffer trimming
SysManager/SysManager/ViewModels/ConsoleViewModel.cs
When Lines.Count exceeds MaxLines, the logic now conditionally chooses between clear-and-rebuild (if excess ≥ 25% of buffer) or incremental RemoveAt(0) (if excess is smaller) to reduce worst-case O(n²) complexity.
LogsViewModel batched event dispatch
SysManager/SysManager/ViewModels/LogsViewModel.cs
RefreshAsync now accumulates FriendlyEventEntry results into batches of 50 and posts them together to the UI context, replacing individual per-entry dispatch to reduce dispatcher overhead.
Release notes
CHANGELOG.md
Added release notes for version 0.48.33 documenting the ConsoleViewModel trimming and LogsViewModel batching optimizations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 Clear and rebuild when the buffer bloats,
Batch the events in fifty-item boats,
Less dispatcher churn, less thrashing pain,
Smooth sailing through the logs again! 📜✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the two main performance optimizations in the changeset: ConsoleViewModel trimming optimization and LogsViewModel batch dispatch optimization.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/perf-console-logs

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
SysManager/SysManager/ViewModels/ConsoleViewModel.cs (1)

44-44: ⚡ Quick win

Anchor the 25% cutoff to MaxLines instead of Lines.Count.

Line 44 uses Lines.Count / 4, which shifts the cutoff as the overflow grows. For a stable “25% of buffer” policy, compare against MaxLines / 4.

Proposed change
-                if (excess > Lines.Count / 4)
+                if (excess > MaxLines / 4)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@SysManager/SysManager/ViewModels/ConsoleViewModel.cs` at line 44, The cutoff
comparison currently uses the dynamic buffer size (Lines.Count) so the 25%
threshold moves as content grows; change the comparison to use the fixed buffer
size by replacing the use of Lines.Count with MaxLines (i.e., compare excess >
MaxLines / 4) in the method where excess is computed so the “25% of buffer”
policy is anchored to MaxLines (look for the conditional using excess and
Lines.Count in ConsoleViewModel).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@SysManager/SysManager/ViewModels/ConsoleViewModel.cs`:
- Line 44: The cutoff comparison currently uses the dynamic buffer size
(Lines.Count) so the 25% threshold moves as content grows; change the comparison
to use the fixed buffer size by replacing the use of Lines.Count with MaxLines
(i.e., compare excess > MaxLines / 4) in the method where excess is computed so
the “25% of buffer” policy is anchored to MaxLines (look for the conditional
using excess and Lines.Count in ConsoleViewModel).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 6b68c255-3801-4ce8-a904-e15777846027

📥 Commits

Reviewing files that changed from the base of the PR and between cdeef39 and db18a39.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • SysManager/SysManager/ViewModels/ConsoleViewModel.cs
  • SysManager/SysManager/ViewModels/LogsViewModel.cs
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Build & unit tests
  • GitHub Check: Analyze (csharp)
🔇 Additional comments (3)
CHANGELOG.md (2)

9-10: LGTM!


11-17: LGTM!

SysManager/SysManager/ViewModels/LogsViewModel.cs (1)

135-169: LGTM!

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 6.89655% with 27 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
SysManager/SysManager/ViewModels/LogsViewModel.cs 0.00% 20 Missing ⚠️
...sManager/SysManager/ViewModels/ConsoleViewModel.cs 22.22% 6 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@laurentiu021
laurentiu021 merged commit 6e212ac into main May 18, 2026
5 checks passed
@laurentiu021
laurentiu021 deleted the fix/perf-console-logs branch May 18, 2026 10:10
laurentiu021 added a commit that referenced this pull request May 22, 2026
## Summary

**Batch 4** of the QA bug fix series.

### Issue #400 — Missing error handling for JSON parsing

**SpeedTestService.RunOoklaAsync:**
- \JsonDocument.Parse(stdout)\ now wrapped in try-catch for
\JsonException\
- \GetProperty()\ calls wrapped for \KeyNotFoundException\
- Both re-thrown as \InvalidOperationException\ with descriptive
messages
- \JsonDocument\ properly disposed via try/finally

**WingetService.ParseUpgradeTable** — verified already safe:
- \Slice\ helper uses \Math.Min(end, line.Length)\ for bounds checking
- Lines shorter than expected are skipped via \if (line.Length <
idxAvailable) continue\

**DuplicateFileService.ComputePartialHash** — verified already safe:
- Uses \�uffer.AsSpan(0, totalRead)\ to hash only bytes actually read
- Loop correctly handles files smaller than 4 KB

Closes #400

Co-authored-by: laurentiu021 <laurentiu021@users.noreply.github.com>
laurentiu021 added a commit that referenced this pull request May 22, 2026
…tch (#422)

Co-authored-by: laurentiu021 <laurentiu021@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants