mirror of
https://github.com/block/buzz.git
synced 2026-11-24 02:02:07 +00:00
perf(mobile): save own thread interest once per unread catch-up (#9614)
iOS was killing the app with watchdog terminations (`0x8BADF00D`, "scene-update watchdog transgression" and "Failed to terminate gracefully") because the main thread spent 5 to 10 seconds in unread catch-up. Symbolicated reports from a 1.4.0-rc.1 user all point at the same stack: `UnreadCatchUp._catchUpUnreadEvents` → `ChannelsNotifier._writeThreadInterestStores` → `jsonEncode` / `SharedPreferences.setString`. For every self-authored event in a catch-up batch whose thread was new to the interest sets, the notifier re-encoded **both** whole root-id sets and wrote both to SharedPreferences. That is quadratic in the batch: a fresh sign-in, whose catch-up returns many of the user's own messages, ran thousands of growing encodes and platform-channel writes on the main thread. Now the catch-up adds the whole batch to the in-memory sets and saves once if anything changed. The live single-event path is unchanged (one write per new thread). ### Regressions None expected. The persisted keys, format and contents are identical; only the number of intermediate writes changes. The storage contract is unchanged: saves were and still are fire-and-forget `setString` calls, so neither version guarantees durability before a process kill. A kill before the save lands now loses the whole batch rather than a prefix of it; a later catch-up can re-add those threads only while they are still after the channel's read timestamp and within the catch-up window. ### Validation - **Writes per catch-up**, 50 own messages across 50 threads: 50 writes per key before, 1 after (new test). - Added test: `unread catch-up saves own thread interest once per batch` (channels_provider_thread_interest_cases.dart). It checks the saved sets and the write count; it fails on the old code. - Full mobile check and test suite pass. - Automated full-app run on an iOS simulator (PR head build, signed in to a real community): cold launch and unread catch-up saved both thread-interest sets; background and foreground; Activity rows and unread dots render; opened a thread and sent a reply, and the participation set grew by one (live path still saves). No errors in the app log. Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Tom Brow <tomb@squareup.com>
This commit is contained in:
@@ -113,13 +113,16 @@ extension _UnreadCatchUp on ChannelsNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
var threadInterestChanged = false;
|
||||
for (final event in events) {
|
||||
if (batchDeletedIds.contains(event.id)) continue;
|
||||
if (EventKind.channelMessageEventKinds.contains(event.kind) &&
|
||||
event.pubkey.toLowerCase() == myPk.toLowerCase()) {
|
||||
_recordSelfThreadInterest(event, myPk);
|
||||
event.pubkey.toLowerCase() == myPk.toLowerCase() &&
|
||||
_addSelfThreadInterest(event)) {
|
||||
threadInterestChanged = true;
|
||||
}
|
||||
}
|
||||
if (threadInterestChanged) _writeThreadInterestStores(myPk);
|
||||
|
||||
final recordedAtByChannel = <String, int>{};
|
||||
final windowSizeByChannel = <String, int>{};
|
||||
|
||||
@@ -861,8 +861,10 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
|
||||
final updated = List<Channel>.of(channels);
|
||||
final channel = updated[idx];
|
||||
|
||||
if (myPk != null && event.pubkey.toLowerCase() == myPk.toLowerCase()) {
|
||||
_recordSelfThreadInterest(event, myPk);
|
||||
if (myPk != null &&
|
||||
event.pubkey.toLowerCase() == myPk.toLowerCase() &&
|
||||
_addSelfThreadInterest(event)) {
|
||||
_writeThreadInterestStores(myPk);
|
||||
}
|
||||
|
||||
if (myPk != null &&
|
||||
@@ -916,12 +918,13 @@ class ChannelsNotifier extends AsyncNotifier<List<Channel>> {
|
||||
}
|
||||
}
|
||||
|
||||
void _recordSelfThreadInterest(NostrEvent event, String pubkey) {
|
||||
/// Adds [event]'s thread to the in-memory interest sets; true when new.
|
||||
/// Callers persist with [_writeThreadInterestStores], once per batch: each
|
||||
/// write re-encodes both whole sets on the main thread.
|
||||
bool _addSelfThreadInterest(NostrEvent event) {
|
||||
final ref = event.threadReference;
|
||||
final target = ref.rootId != null ? _participatedRootIds : _authoredRootIds;
|
||||
final id = ref.rootId ?? event.id;
|
||||
if (!target.add(id)) return;
|
||||
_writeThreadInterestStores(pubkey);
|
||||
return target.add(ref.rootId ?? event.id);
|
||||
}
|
||||
|
||||
void _writeThreadInterestStores(String pubkey) {
|
||||
|
||||
@@ -10,6 +10,7 @@ import 'package:flutter/foundation.dart' show listEquals;
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
|
||||
import 'package:buzz/shared/theme/theme.dart';
|
||||
import 'package:buzz/features/activity/activity_page.dart';
|
||||
import 'package:buzz/features/channels/channel_detail_page.dart';
|
||||
@@ -38,6 +39,7 @@ part 'channels_provider_request_path_cases.dart';
|
||||
part 'channels_provider_activity_cases.dart';
|
||||
part 'channels_provider_join_cases.dart';
|
||||
part 'channels_provider_saved_directory_cases.dart';
|
||||
part 'channels_provider_thread_interest_cases.dart';
|
||||
|
||||
/// Tests for [ChannelsNotifier] in the pure-Nostr world.
|
||||
///
|
||||
@@ -58,6 +60,7 @@ void main() {
|
||||
_threadActivityCases();
|
||||
_confirmedJoinCases();
|
||||
_savedDirectoryCases();
|
||||
_threadInterestCases();
|
||||
const myPk = 'me';
|
||||
|
||||
test(
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
part of 'channels_provider_test.dart';
|
||||
|
||||
class _CountingPrefsStore extends InMemorySharedPreferencesStore {
|
||||
_CountingPrefsStore() : super.empty();
|
||||
|
||||
final writesByKey = <String, int>{};
|
||||
|
||||
@override
|
||||
Future<bool> setValue(String valueType, String key, Object value) {
|
||||
writesByKey.update(key, (n) => n + 1, ifAbsent: () => 1);
|
||||
return super.setValue(valueType, key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void _threadInterestCases() {
|
||||
test('unread catch-up saves own thread interest once per batch', () async {
|
||||
const self = 'me';
|
||||
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
String hexId(String prefix, int i) =>
|
||||
'$prefix${i.toRadixString(16).padLeft(63, '0')}';
|
||||
final replyRoots = [for (var i = 0; i < 40; i++) hexId('a', i)];
|
||||
final ownRoots = [for (var i = 0; i < 10; i++) hexId('b', i)];
|
||||
final session = _FakeRelaySession(
|
||||
memberships: [_membership(_channelA, self)],
|
||||
metadata: [_meta(id: _channelA, name: 'general')],
|
||||
recentMessages: [
|
||||
for (final (i, root) in replyRoots.indexed)
|
||||
NostrEvent(
|
||||
id: hexId('c', i),
|
||||
pubkey: self,
|
||||
createdAt: now - 600 + i,
|
||||
kind: 9,
|
||||
tags: [
|
||||
['h', _channelA],
|
||||
['e', root, '', 'root'],
|
||||
['e', root, '', 'reply'],
|
||||
],
|
||||
content: 'reply $i',
|
||||
sig: '',
|
||||
),
|
||||
for (final (i, root) in ownRoots.indexed)
|
||||
NostrEvent(
|
||||
id: root,
|
||||
pubkey: self,
|
||||
createdAt: now - 300 + i,
|
||||
kind: 9,
|
||||
tags: const [
|
||||
['h', _channelA],
|
||||
],
|
||||
content: 'root $i',
|
||||
sig: '',
|
||||
),
|
||||
],
|
||||
);
|
||||
final store = _CountingPrefsStore();
|
||||
SharedPreferencesStorePlatform.instance = store;
|
||||
SharedPreferences.resetStatic();
|
||||
addTearDown(() {
|
||||
SharedPreferences.resetStatic();
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
});
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final container = _buildContainer(
|
||||
session: session,
|
||||
overrides: [savedPrefsProvider.overrideWithValue(prefs)],
|
||||
);
|
||||
addTearDown(container.dispose);
|
||||
|
||||
await container.read(channelsProvider.future);
|
||||
await _settle();
|
||||
|
||||
const participated = 'buzz-thread-participation.v1:me';
|
||||
const authored = 'buzz-thread-authored.v1:me';
|
||||
Set<String> saved(String key) => {
|
||||
...(jsonDecode(prefs.getString(key)!) as List).cast<String>(),
|
||||
};
|
||||
expect(saved(participated), replyRoots.toSet());
|
||||
expect(saved(authored), ownRoots.toSet());
|
||||
expect(store.writesByKey['flutter.$participated'], 1);
|
||||
expect(store.writesByKey['flutter.$authored'], 1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user