Skip to content

Commit 99390d7

Browse files
square-tombstudio
andcommitted
mobile: validate each verified profile once, not on every publish
Every publish rebuilt the verified profile map by validating every known head again. A channel with many members publishes once per batch as subscriptions land, so opening or resuming a big channel spent about 210 ms of UI-thread CPU in parseVerifiedProfile on an iPhone 12 mini. Keep each subject's parse while its head and the authority stand, and apply `checked` to it without validating again. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Signed-off-by: Tom Brow <[email protected]>
1 parent 920b1e6 commit 99390d7

2 files changed

Lines changed: 100 additions & 10 deletions

File tree

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

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,18 +434,47 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
434434
}
435435
}
436436

437+
/// Validates one head. Tests override this to count parses.
438+
@visibleForTesting
439+
VerifiedProfile? parseHead(
440+
NostrEvent event,
441+
VerifiedProfileAuthority authority, {
442+
required bool checked,
443+
}) => parseVerifiedProfile(event, authority, checked: checked);
444+
445+
/// The last parse of each subject's head. Every publish rebuilds the whole
446+
/// map, so without it each batch re-validated every head already known.
447+
Map<String, _ParsedHead> _parsed = const {};
448+
437449
Map<String, VerifiedProfile> _snapshot() {
438450
final authority = _authority;
439-
if (!_enabled || authority == null) return const {};
440-
return {
441-
for (final MapEntry(:key, :value) in _heads.entries)
442-
if (_wanted.containsKey(key))
443-
key: ?parseVerifiedProfile(
444-
value,
445-
authority,
446-
checked: _discoveryFresh && _checked.contains(key),
447-
),
448-
};
451+
if (!_enabled || authority == null) {
452+
_parsed = const {};
453+
return const {};
454+
}
455+
final parsed = <String, _ParsedHead>{};
456+
final profiles = <String, VerifiedProfile>{};
457+
for (final MapEntry(:key, :value) in _heads.entries) {
458+
if (!_wanted.containsKey(key)) continue;
459+
final checked = _discoveryFresh && _checked.contains(key);
460+
final previous = _parsed[key];
461+
final head =
462+
previous != null &&
463+
identical(previous.event, value) &&
464+
previous.authority == authority
465+
? previous
466+
: _ParsedHead(
467+
value,
468+
authority,
469+
parseHead(value, authority, checked: checked),
470+
);
471+
parsed[key] = head;
472+
if (head.withChecked(checked) case final profile?) {
473+
profiles[key] = profile;
474+
}
475+
}
476+
_parsed = parsed;
477+
return profiles;
449478
}
450479

451480
void _publish() {
@@ -455,6 +484,30 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
455484
}
456485
}
457486

487+
/// A head's parse, kept while the head and authority stand. Only `checked`
488+
/// varies between publishes, so it is applied without validating again.
489+
class _ParsedHead {
490+
_ParsedHead(this.event, this.authority, this._profile);
491+
492+
final NostrEvent event;
493+
final VerifiedProfileAuthority authority;
494+
VerifiedProfile? _profile;
495+
496+
VerifiedProfile? withChecked(bool checked) {
497+
final profile = _profile;
498+
if (profile == null || profile.checked == checked) return profile;
499+
return _profile = VerifiedProfile(
500+
displayName: profile.displayName,
501+
username: profile.username,
502+
fullName: profile.fullName,
503+
issuerLabel: profile.issuerLabel,
504+
verifiedAt: profile.verifiedAt,
505+
checked: checked,
506+
multipleIssuers: profile.multipleIssuers,
507+
);
508+
}
509+
}
510+
458511
final verifiedProfilesProvider =
459512
NotifierProvider<VerifiedProfilesNotifier, Map<String, VerifiedProfile>>(
460513
VerifiedProfilesNotifier.new,

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ class _CountingNotifier extends VerifiedProfilesNotifier {
5555
Map<String, int> newInterestMap() => _CountingMap(iterations);
5656
}
5757

58+
class _ParseCountingNotifier extends VerifiedProfilesNotifier {
59+
_ParseCountingNotifier(this.parses);
60+
61+
final List<String> parses;
62+
63+
@override
64+
VerifiedProfile? parseHead(
65+
NostrEvent event,
66+
VerifiedProfileAuthority authority, {
67+
required bool checked,
68+
}) {
69+
parses.add(verifiedProfileSubject(event, authority)!);
70+
return super.parseHead(event, authority, checked: checked);
71+
}
72+
}
73+
5874
class Harness {
5975
Harness(this.async, {VerifiedProfilesNotifier Function()? notifier}) {
6076
container = ProviderContainer(
@@ -129,6 +145,27 @@ void main() {
129145
});
130146
});
131147

148+
test('a later batch reuses profiles already parsed', () {
149+
fakeAsync((async) {
150+
final parses = <String>[];
151+
final h = Harness(async, notifier: () => _ParseCountingNotifier(parses));
152+
addTearDown(h.container.dispose);
153+
h.relay.seed([
154+
signedProjection(profileTags(subjectA)),
155+
signedProjection(profileTags(subjectB)),
156+
]);
157+
h.start([subjectA]);
158+
expect(h.profiles[subjectA]?.checked, isTrue);
159+
h.container.read(verifiedProfilesProvider.notifier).track([subjectB]);
160+
h.settle();
161+
// Rebatching unchecks and rechecks A; every publish rebuilds the map,
162+
// but each head is validated once.
163+
expect(h.profiles[subjectA]?.checked, isTrue);
164+
expect(h.profiles[subjectB]?.checked, isTrue);
165+
expect(parses..sort(), [subjectA, subjectB]);
166+
});
167+
});
168+
132169
journey('no marks until discovery succeeds and advertises public_profile', (
133170
h,
134171
) {

0 commit comments

Comments
 (0)