Every received packet copy re-saved its channel's whole history, every
unread change re-saved the channel list, and adverts/path records re-saved
full contact and path-history blobs. Under load the SharedPreferences
backlog starved the Dart event loop (frames and timers stopped), the radio's
offline queue overflowed, and CPU stayed pinned after traffic ended.
- Add CoalescedWrites: keeps the latest write per key and runs them once
per window (750 ms; 5 s for path history). Writes start synchronously so
stores still read the current scoped key.
- Flush before the app is backgrounded, on disconnect, before switching
radios, before the channel slot map changes, before channel history
reloads, before deleteChannel drops a list, and before path history
reloads an evicted contact.
- Cap in-memory channel history at ChannelMessageStore.maxStoredMessages;
storage already kept only that many, but the list grew unbounded during a
session and every save grew with it.
- Cache Contact.publicKeyHex and build pubKeyToHex from a hex-digit lookup; it was
rebuilt on every equality/hash and per-advert contact scan.
Load test (fake TCP radio, 350 contacts, 17 channels, 10-150 packets/s,
Linux profile build): CPU at 10 packets/s 104% -> 45%, recovers to idle
after traffic instead of staying at 160%, and queued messages drained went
from 555 to 3237 over the same run.