Skip to content

Commit 5c0a508

Browse files
committed
unreads [nfc]: Introduce _reverseStreamsLookup data structure
This one is needed for enhancing performance in _removeAllInStreams, _isPresentInStreams which will be done in the upcoming commits. Additionally this will be helpful for efficiently checking for @-mention in subscription_list
1 parent 0ca1423 commit 5c0a508

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/model/unreads.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,17 @@ class Unreads extends ChangeNotifier {
4040
required ChannelStore channelStore,
4141
}) {
4242
final streams = <int, Map<String, QueueList<int>>>{};
43+
final reverseStreamsLookup = <int, ({int streamId, String topic})>{};
4344
final dms = <DmNarrow, QueueList<int>>{};
4445
final mentions = Set.of(initial.mentions);
4546

4647
for (final unreadChannelSnapshot in initial.channels) {
4748
final streamId = unreadChannelSnapshot.streamId;
4849
final topic = unreadChannelSnapshot.topic;
4950
(streams[streamId] ??= {})[topic] = QueueList.from(unreadChannelSnapshot.unreadMessageIds);
51+
final messageInfo = (streamId: streamId, topic: topic);
52+
reverseStreamsLookup.addEntries(
53+
unreadChannelSnapshot.unreadMessageIds.map((messageId) => MapEntry(messageId, messageInfo)));
5054
}
5155

5256
for (final unreadDmSnapshot in initial.dms) {
@@ -63,6 +67,7 @@ class Unreads extends ChangeNotifier {
6367
return Unreads._(
6468
channelStore: channelStore,
6569
streams: streams,
70+
reverseStreamsLookup: reverseStreamsLookup,
6671
dms: dms,
6772
mentions: mentions,
6873
oldUnreadsMissing: initial.oldUnreadsMissing,
@@ -73,11 +78,12 @@ class Unreads extends ChangeNotifier {
7378
Unreads._({
7479
required this.channelStore,
7580
required this.streams,
81+
required Map<int, ({int streamId, String topic})> reverseStreamsLookup,
7682
required this.dms,
7783
required this.mentions,
7884
required this.oldUnreadsMissing,
7985
required this.selfUserId,
80-
});
86+
}) : _reverseStreamsLookup = reverseStreamsLookup;
8187

8288
final ChannelStore channelStore;
8389

@@ -87,6 +93,9 @@ class Unreads extends ChangeNotifier {
8793
/// Unread stream messages, as: stream ID → topic → message IDs (sorted).
8894
final Map<int, Map<String, QueueList<int>>> streams;
8995

96+
/// Maps the message id to the (stream ID, topic) we stored it under in streams.
97+
final Map<int, ({int streamId, String topic})> _reverseStreamsLookup;
98+
9099
/// Unread DM messages, as: DM narrow → message IDs (sorted).
91100
final Map<DmNarrow, QueueList<int>> dms;
92101

@@ -339,6 +348,7 @@ class Unreads extends ChangeNotifier {
339348
streams.clear();
340349
dms.clear();
341350
mentions.clear();
351+
_reverseStreamsLookup.clear();
342352
oldUnreadsMissing = false;
343353
} else {
344354
final messageIdsSet = Set.of(event.messages);
@@ -433,6 +443,7 @@ class Unreads extends ChangeNotifier {
433443

434444
void _addLastInStreamTopic(int messageId, int streamId, String topic) {
435445
((streams[streamId] ??= {})[topic] ??= QueueList()).addLast(messageId);
446+
_reverseStreamsLookup[messageId] = (streamId: streamId, topic: topic);
436447
}
437448

438449
// [messageIds] must be sorted ascending and without duplicates.
@@ -445,6 +456,9 @@ class Unreads extends ChangeNotifier {
445456
// TODO(server-6) remove 6.0 comment
446457
(existing) => setUnion(existing, messageIds),
447458
);
459+
final messageInfo = (streamId: streamId, topic: topic);
460+
_reverseStreamsLookup.addEntries(
461+
messageIds.map((messageId) => MapEntry(messageId, messageInfo)));
448462
}
449463

450464
// TODO use efficient model lookups
@@ -468,6 +482,9 @@ class Unreads extends ChangeNotifier {
468482
for (final streamId in newlyEmptyStreams) {
469483
streams.remove(streamId);
470484
}
485+
for (var messageId in idsToRemove) {
486+
_reverseStreamsLookup.remove(messageId);
487+
}
471488
}
472489

473490
void _removeAllInStreamTopic(Set<int> incomingMessageIds, int streamId, String topic) {
@@ -484,6 +501,9 @@ class Unreads extends ChangeNotifier {
484501
streams.remove(streamId);
485502
}
486503
}
504+
for (var messageId in incomingMessageIds) {
505+
_reverseStreamsLookup.remove(messageId);
506+
}
487507
}
488508

489509
// TODO use efficient model lookups

0 commit comments

Comments
 (0)