Skip to content

Commit 93bbd45

Browse files
browsquare-tombstudiostudio
authored
perf(mobile): keep verified-name batches open as people are added (#9450)
Verified names are read in subscriptions of up to 128 people. Each time someone new was tracked, the batches were rebuilt from the whole tracked list, so the last partial batch got a new identity, was closed, and was reopened with everyone in it plus the newcomer. Profiles show up a few at a time (search results, channel members, mentions), so the same records were read again and again. Past 1,024 tracked people, each newcomer evicts the oldest, which shifts every batch boundary, so every batch was closed and all 1,024 records were read again. Now an open batch stays open as long as all its people are still tracked. Newly tracked people get a batch of their own. A batch that loses someone to eviction, or that the relay retried, is replaced as before, and so is a batch whose person was evicted and tracked again before the batch was re-checked, so their check mark and cached name are re-read. Once four batches are smaller than 128, the next addition merges the smallest of them with the newcomers until they fill a batch (at least one, so no fifth small batch appears). The subscription count stays within four of the minimum, and a merge re-reads at most 128 plus the largest small batch. People already checked also stay checked while a newcomer's batch opens; before, the reopened batch briefly unchecked them. ### Validation New tests: tracking one more person opens a batch with only that person, and the people already checked stay checked throughout; with four small batches of 64 open, one more person merges two of them into a full batch and leaves the other two open; a large addition past four small batches still retires one; tracking one more person at the 1,024 limit re-reads only the batch that lost the oldest person; a person evicted and tracked again before the sync is re-read and checked; and a re-tracked person keeps their verified name when the cached-name limit trims it. Existing batch, eviction, retry and reconnect tests pass unchanged (one comment updated). Full mobile test suite and `flutter analyze` pass. On an iPhone 12 mini signed in to Block's Builderlab (instrumented profile build of e2fb02c), opening search and typing "presence" after opening a channel and a thread: verified-name reads went from 21 subscriptions and 328 KB before this change to 11 and 88 KB with it (counted per subscription opened; the first revision measured the same way gave 11 and 90 KB). The same automated run covered cold start, opening the-squad and one of its threads, search with the People filter, opening buzz-mobile's details and scrolling its full 86-person member list, and a 60 s background then resume (the resync re-read 2 batches, 65 KB). Names rendered throughout. ### Regressions Up to four more open subscriptions while small batches accumulate, each held open by the relay like the existing ones. A community with under 128 tracked people now has up to five instead of one. No merge policy reads less than the old slicing on every sequence of additions. In a model of 3,000 random tracking sequences, this one read more records than the old code in about 1 sequence in 500, by at most a quarter. --------- Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
1 parent 9fc2b33 commit 93bbd45

2 files changed

Lines changed: 191 additions & 21 deletions

File tree

‎mobile/lib/shared/verified_profiles/verified_profile_directory.dart‎

Lines changed: 74 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ const _discoveryRefresh = Duration(minutes: 5);
2121
/// tolerated, a flood before EOSE fails the batch instead of buffering it.
2222
const _maxEarlyPerSubject = 4;
2323

24+
/// Batches smaller than [verifiedProfileBatchSize] allowed before the smallest
25+
/// are merged into the next new one until it fills a batch.
26+
const _maxPartialBatches = 4;
27+
2428
class _Batch {
25-
_Batch(this.subjects);
29+
_Batch(this.subjects, this.interest);
2630
final List<String> subjects;
31+
32+
/// The newest interest when the batch opened. A subject whose interest is
33+
/// newer was evicted and tracked again since, which dropped its `_checked`
34+
/// mark and maybe its cached head, so the batch must be read again.
35+
final int interest;
2736
final List<NostrEvent> early = [];
2837

2938
/// Live events awaiting proof, newest per subject, so a fast stream cannot
@@ -280,38 +289,84 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
280289
_publish();
281290
return;
282291
}
283-
final subjects = _wanted.keys.toList();
284-
final next = <String, List<String>>{};
285-
for (var i = 0; i < subjects.length; i += verifiedProfileBatchSize) {
286-
final end = i + verifiedProfileBatchSize;
287-
final chunk = subjects.sublist(
288-
i,
289-
end < subjects.length ? end : subjects.length,
290-
);
291-
next[chunk.join(':')] = chunk;
292-
}
292+
// Keep every open batch whose subjects are all still tracked since it
293+
// opened, so newly tracked people get a batch of their own instead of
294+
// re-reading everyone already checked. A retried batch is replaced so a
295+
// fresh snapshot can prove absence, and a batch that lost a subject to
296+
// eviction is replaced too, even if the subject was tracked again.
293297
for (final id in _batches.keys.toList()) {
294-
// A retried batch is replaced so a fresh snapshot can prove absence.
295-
if (next.containsKey(id) && !_batches[id]!.stale) continue;
296-
final batch = _batches.remove(id)!..stop();
297-
_checked.removeAll(batch.subjects);
298+
final batch = _batches[id]!;
299+
if (!batch.stale &&
300+
batch.subjects.every(
301+
(subject) => (_wanted[subject] ?? 1 << 62) <= batch.interest,
302+
)) {
303+
continue;
304+
}
305+
_close(id);
306+
}
307+
var uncovered = _uncoveredSubjects();
308+
if (uncovered.isEmpty) {
309+
if (_refreshTimer == null) _armRefresh(generation);
310+
_publish();
311+
return;
298312
}
299-
for (final MapEntry(key: id, value: chunk) in next.entries) {
300-
if (!_batches.containsKey(id)) {
301-
unawaited(_open(id, chunk, authority, generation));
313+
// Small additions each open a small batch. Past a few of those, merge the
314+
// smallest into the new subjects until they fill a batch, so the
315+
// subscription count stays near the minimum while re-reading as few
316+
// checked subjects as possible.
317+
final partial = [
318+
for (final batch in _batches.entries)
319+
if (batch.value.subjects.length < verifiedProfileBatchSize) batch,
320+
];
321+
if (partial.length >= _maxPartialBatches) {
322+
partial.sort(
323+
(a, b) => a.value.subjects.length.compareTo(b.value.subjects.length),
324+
);
325+
// Retire at least one unless the new subjects split evenly, since
326+
// they can add a partial batch of their own.
327+
var size = uncovered.length;
328+
var retired = size % verifiedProfileBatchSize == 0;
329+
for (final MapEntry(:key, :value) in partial) {
330+
if (retired && size >= verifiedProfileBatchSize) break;
331+
size += value.subjects.length;
332+
retired = true;
333+
_close(key);
302334
}
335+
uncovered = _uncoveredSubjects();
336+
}
337+
for (var i = 0; i < uncovered.length; i += verifiedProfileBatchSize) {
338+
final end = i + verifiedProfileBatchSize;
339+
final chunk = uncovered.sublist(
340+
i,
341+
end < uncovered.length ? end : uncovered.length,
342+
);
343+
unawaited(_open(chunk.join(':'), chunk, authority, generation));
303344
}
304345
if (_refreshTimer == null) _armRefresh(generation);
305346
_publish();
306347
}
307348

349+
void _close(String id) {
350+
final batch = _batches.remove(id)!..stop();
351+
_checked.removeAll(batch.subjects);
352+
}
353+
354+
/// Tracked subjects no open batch covers, oldest interest first.
355+
List<String> _uncoveredSubjects() {
356+
final covered = {for (final batch in _batches.values) ...batch.subjects};
357+
return [
358+
for (final key in _wanted.keys)
359+
if (!covered.contains(key)) key,
360+
];
361+
}
362+
308363
Future<void> _open(
309364
String id,
310365
List<String> chunk,
311366
VerifiedProfileAuthority authority,
312367
int generation,
313368
) async {
314-
final batch = _Batch(chunk);
369+
final batch = _Batch(chunk, _interest);
315370
_batches[id] = batch;
316371
void fail() {
317372
if (!batch.active) return;

‎mobile/test/shared/verified_profiles/verified_profile_directory_test.dart‎

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ void main() {
158158
expect(h.profiles[subjectA]?.checked, isTrue);
159159
h.container.read(verifiedProfilesProvider.notifier).track([subjectB]);
160160
h.settle();
161-
// Rebatching unchecks and rechecks A; every publish rebuilds the map,
162-
// but each head is validated once.
161+
// Every publish rebuilds the map, but each head is validated once.
163162
expect(h.profiles[subjectA]?.checked, isTrue);
164163
expect(h.profiles[subjectB]?.checked, isTrue);
165164
expect(parses..sort(), [subjectA, subjectB]);
@@ -202,6 +201,122 @@ void main() {
202201
expect(sizes..sort(), [2, 128]);
203202
});
204203

204+
journey('a newly tracked subject is read alone; checked ones stay checked', (
205+
h,
206+
) {
207+
h.relay.seed([
208+
signedProjection(profileTags(subjectA)),
209+
signedProjection(profileTags(subjectB, name: 'Bob', username: 'bob')),
210+
]);
211+
h.start([subjectA]);
212+
expect(h.profiles[subjectA]?.checked, isTrue);
213+
final before = h.dFilters.length;
214+
final notifier = h.container.read(verifiedProfilesProvider.notifier);
215+
notifier.track([subjectB]);
216+
// A is never unchecked while B's batch opens.
217+
expect(h.profiles[subjectA]?.checked, isTrue);
218+
h.settle();
219+
expect(
220+
[for (final f in h.dFilters.skip(before)) f['#d']],
221+
[
222+
[subjectB],
223+
],
224+
);
225+
expect(h.profiles[subjectA]?.checked, isTrue);
226+
expect(h.profiles[subjectB]?.checked, isTrue);
227+
});
228+
229+
journey('past four small batches, the smallest merge until one is full', (h) {
230+
String key(int i) => i.toRadixString(16).padLeft(64, '0');
231+
final notifier = h.container.read(verifiedProfilesProvider.notifier);
232+
h.start([for (var i = 0; i < 64; i++) key(i)]);
233+
for (var group = 1; group < 4; group++) {
234+
notifier.track([for (var i = 0; i < 64; i++) key(group * 64 + i)]);
235+
h.settle();
236+
}
237+
final before = h.dFilters.length;
238+
expect(before, 4, reason: 'four partial batches of 64');
239+
notifier.track([key(256)]);
240+
h.settle();
241+
// Two of the 64s fill one batch with the newcomer; the other two stay.
242+
final reads = [
243+
for (final f in h.dFilters.skip(before)) (f['#d'] as List).cast<String>(),
244+
];
245+
expect([for (final r in reads) r.length], [128, 1]);
246+
expect(reads.expand((r) => r), contains(key(256)));
247+
});
248+
249+
journey('a large addition past four small batches still retires one', (h) {
250+
String key(int i) => i.toRadixString(16).padLeft(64, '0');
251+
final notifier = h.container.read(verifiedProfilesProvider.notifier);
252+
h.start([key(0)]);
253+
for (var i = 1; i < 4; i++) {
254+
notifier.track([key(i)]);
255+
h.settle();
256+
}
257+
final before = h.dFilters.length;
258+
notifier.track([for (var i = 4; i < 133; i++) key(i)]);
259+
h.settle();
260+
// 129 newcomers would leave a fifth partial; one single folds in.
261+
expect(
262+
[for (final f in h.dFilters.skip(before)) (f['#d'] as List).length],
263+
[128, 2],
264+
);
265+
});
266+
267+
journey('eviction re-reads only the batch that lost a subject', (h) {
268+
String key(int i) => i.toRadixString(16).padLeft(64, '0');
269+
h.start([for (var i = 0; i < 1024; i++) key(i)]);
270+
final before = h.dFilters.length;
271+
expect(before, 8);
272+
h.container.read(verifiedProfilesProvider.notifier).track([key(1024)]);
273+
h.settle();
274+
expect(
275+
[for (final f in h.dFilters.skip(before)) f['#d']],
276+
[
277+
[for (var i = 1; i < 128; i++) key(i), key(1024)],
278+
],
279+
);
280+
});
281+
282+
journey('a subject evicted and tracked again before the sync is re-read', (
283+
h,
284+
) {
285+
String key(int i) => i.toRadixString(16).padLeft(64, '0');
286+
h.relay.seed([signedProjection(profileTags(subjectA))]);
287+
h.start([subjectA]);
288+
final notifier = h.container.read(verifiedProfilesProvider.notifier);
289+
notifier.track([for (var i = 0; i < 1023; i++) key(i)]);
290+
h.settle();
291+
expect(h.profiles[subjectA]?.checked, isTrue);
292+
// A is evicted, then tracked again, which evicts key(0) in another batch.
293+
notifier
294+
..track([key(1023)])
295+
..track([subjectA]);
296+
h.settle();
297+
expect(h.profiles[subjectA]?.checked, isTrue);
298+
});
299+
300+
journey('a re-tracked subject keeps its name when the head cache trims', (h) {
301+
String key(int i) => i.toRadixString(16).padLeft(64, '0');
302+
h.relay.seed([signedProjection(profileTags(subjectA))]);
303+
h.start([subjectA]);
304+
final notifier = h.container.read(verifiedProfilesProvider.notifier);
305+
// Each round evicts A, may trim its cached head, then tracks it again
306+
// before the sync; past 4,096 cached heads only a re-read restores it.
307+
for (var round = 0; round < 6; round++) {
308+
final subjects = [for (var i = 0; i < 1023; i++) key(round * 1023 + i)];
309+
h.relay.seed([
310+
for (final s in subjects) signedProjection(profileTags(s)),
311+
]);
312+
notifier
313+
..track(subjects)
314+
..track([subjectA]);
315+
h.settle();
316+
expect(h.profiles[subjectA]?.displayName, 'Ada Lovelace');
317+
}
318+
});
319+
205320
journey('a large roster keeps the 1,024 most recently tracked subjects', (h) {
206321
String key(int i) => i.toRadixString(16).padLeft(64, '0');
207322
h.start([for (var i = 0; i < 10; i++) key(i)]);

0 commit comments

Comments
 (0)