Files
meshcore-open/lib/utils/coalesced_writes.dart
T
zjs81 603172b3ec Batch per-frame persistence so busy meshes don't stall the UI isolate
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.
2026-10-24 23:38:28 -07:00

67 lines
2.3 KiB
Dart

import 'dart:async';
import 'app_logger.dart';
/// Collapses repeated persistence requests for the same key into one write.
///
/// Busy meshes deliver many frames a second, and each one used to rewrite a
/// whole JSON blob (a channel's history, the contact list). Every write is a
/// full re-encode plus a SharedPreferences write: on Linux/Windows that
/// rewrites the entire prefs file synchronously, on Android/iOS it ships the
/// blob across the platform channel. Scheduling instead keeps only the latest
/// writer per key and runs them all once [delay] after the first request.
class CoalescedWrites {
CoalescedWrites({this.delay = const Duration(milliseconds: 750)});
final Duration delay;
final Map<String, Future<void> Function()> _pending = {};
Timer? _timer;
bool get hasPending => _pending.isNotEmpty;
/// Queues [write] under [key], replacing any write already queued for it.
/// [write] should read the current state when it runs, not a snapshot.
void schedule(String key, Future<void> Function() write) {
_pending[key] = write;
_timer ??= Timer(delay, () => unawaited(flush()));
}
/// Drops the queued write for [key], e.g. because an immediate write of
/// newer state or a delete just superseded it.
void cancel(String key) {
_pending.remove(key);
}
/// Runs every queued write now. All writes are started synchronously before
/// the first await, so a store that reads its scoped key on entry still sees
/// the key that was current when the write was queued.
Future<void> flush() {
_timer?.cancel();
_timer = null;
if (_pending.isEmpty) return Future<void>.value();
final writes = _pending.values.toList();
_pending.clear();
// Callers fire and forget, so a failed write is logged rather than left
// as an unhandled async error, and never stops the other writes.
final futures = <Future<void>>[];
for (final write in writes) {
try {
futures.add(write().catchError(_logFailure));
} catch (error) {
_logFailure(error);
}
}
return Future.wait(futures).then((_) {});
}
static void _logFailure(Object error) {
appLogger.warn('Coalesced write failed: $error', tag: 'Storage');
}
void dispose() {
_timer?.cancel();
_timer = null;
_pending.clear();
}
}