Skip to content

perf/ux(etb): Sync is O(n²) on a batch resync, and an edited entry loses its selection #529

Closed

Description

@DeepDiver1975

Split out of #294. EtbViewModel.Sync (src/LageBuch.AppLogic/ViewModels/EtbViewModel.cs) already avoids the Clear() rebuild that #294 is about, but it has two smaller flaws.

1. The O(n) claim in its doc comment is wrong

The comment above Sync() says "O(n) total per Sync() call, not O(n²)". The code says otherwise:

  • _all.Insert(0, row) (:95) shifts the whole list once per new entry. A batch resync, such as joining a device mid-Einsatz or a snapshot with many new lines, is O(n²).
  • _all[_all.IndexOf(current)] = updated (:111) is a linear scan per edited entry. _byId makes finding the row O(1), but not finding its index.

Nothing breaks at today's sizes (hundreds of lines). The comment, though, is what stops the next reader from checking.

2. Editing an entry drops its selection

Entries[entriesIndex] = updated (:116) swaps in a new EtbEntryRow instance, which is a Replace on the collection. The DataGrid treats the new instance as a different item, so if the operator had the edited row selected (another device saved an edit, or this device's own Save), the selection is gone.

What to do

  • Keep _all oldest-first (append, O(1) amortised) and expose newest-first only in Entries, or keep an index map next to _byId. Either way, make the comment true or correct it.
  • Update the existing EtbEntryRow in place instead of replacing the instance. It is a plain sealed class today (:385), so this means making it an ObservableObject with an Update(...), the same way ScbaTruppRow.Update (ScbaViewModel.cs:191) works since feat(atemschutz): model the Sicherheitstrupp instead of hoping for it #408. The EditingEntry → CancelEdit() path must still fire when the entry being edited changes underneath.

Verify

  • A view-model test: an edit arriving via Changed raises no Replace/Reset on Entries, and the row instance is the same as before.
  • A test that a batch of N new journal entries lands newest-first and in order.
  • The existing EtbViewModel tests stay green.
make test PROJECT=tests/LageBuch.AppLogic.Tests FILTER=Name~Etb

Difficulty: easy–medium.


Before you open the PR, please skim CONTRIBUTING.md: commits need DCO sign-off (git commit -s), a cryptographic signature and a Conventional Commit subject. Changes under src/ need a changelog entry as a new file changelog.d/+<slug>.fixed.md (Changelog entries); CI fails the PR otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions