@@ -21,13 +21,18 @@ const _discoveryRefresh = Duration(minutes: 5);
2121/// tolerated, a flood before EOSE fails the batch instead of buffering it.
2222const _maxEarlyPerSubject = 4 ;
2323
24- /// Batches smaller than [verifiedProfileBatchSize] allowed before they are
25- /// merged into full ones .
24+ /// Batches smaller than [verifiedProfileBatchSize] allowed before the smallest
25+ /// are merged into the next new one until it fills a batch .
2626const _maxPartialBatches = 4 ;
2727
2828class _Batch {
29- _Batch (this .subjects);
29+ _Batch (this .subjects, this .interest );
3030 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;
3136 final List <NostrEvent > early = [];
3237
3338 /// Live events awaiting proof, newest per subject, so a fast stream cannot
@@ -284,14 +289,19 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
284289 _publish ();
285290 return ;
286291 }
287- // Keep every open batch whose subjects are all still tracked, so newly
288- // tracked people get a batch of their own instead of re-reading everyone
289- // already checked. A retried batch is replaced so a fresh snapshot can
290- // prove absence, and a batch that lost a subject to eviction is replaced
291- // too.
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 .
292297 for (final id in _batches.keys.toList ()) {
293298 final batch = _batches[id]! ;
294- if (! batch.stale && batch.subjects.every (_wanted.containsKey)) continue ;
299+ if (! batch.stale &&
300+ batch.subjects.every (
301+ (subject) => (_wanted[subject] ?? 1 << 62 ) <= batch.interest,
302+ )) {
303+ continue ;
304+ }
295305 _close (id);
296306 }
297307 var uncovered = _uncoveredSubjects ();
@@ -300,14 +310,28 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
300310 _publish ();
301311 return ;
302312 }
303- // Small additions each open a small batch. Past a few of those, merge
304- // them into full ones, so the subscription count stays near the minimum.
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.
305317 final partial = [
306- for (final MapEntry ( : key, : value) in _batches.entries)
307- if (value.subjects.length < verifiedProfileBatchSize) key ,
318+ for (final batch in _batches.entries)
319+ if (batch. value.subjects.length < verifiedProfileBatchSize) batch ,
308320 ];
309321 if (partial.length >= _maxPartialBatches) {
310- partial.forEach (_close);
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);
334+ }
311335 uncovered = _uncoveredSubjects ();
312336 }
313337 for (var i = 0 ; i < uncovered.length; i += verifiedProfileBatchSize) {
@@ -342,7 +366,7 @@ class VerifiedProfilesNotifier extends Notifier<Map<String, VerifiedProfile>> {
342366 VerifiedProfileAuthority authority,
343367 int generation,
344368 ) async {
345- final batch = _Batch (chunk);
369+ final batch = _Batch (chunk, _interest );
346370 _batches[id] = batch;
347371 void fail () {
348372 if (! batch.active) return ;
0 commit comments