Skip to content

Commit

Permalink
unreads [nfc]: Enhance performance in _removeAllInStreams
Browse files Browse the repository at this point in the history
  • Loading branch information
Khader-1 committed Aug 9, 2024
1 parent 895ba4d commit 32e37ca
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions lib/model/unreads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class Unreads extends ChangeNotifier {
} else {
final messageIdsSet = Set.of(event.messages);
mentions.removeAll(messageIdsSet);
_slowRemoveAllInStreams(messageIdsSet);
_removeAllInStreams(messageIdsSet);
_slowRemoveAllInDms(messageIdsSet);
}
case UpdateMessageFlagsRemoveEvent():
Expand Down Expand Up @@ -458,29 +458,18 @@ class Unreads extends ChangeNotifier {
messageIds.map((messageId) => MapEntry(messageId, messageInfo)));
}

// TODO use efficient model lookups
void _slowRemoveAllInStreams(Set<int> idsToRemove) {
final newlyEmptyStreams = <int>[];
for (final MapEntry(key: streamId, value: topics) in streams.entries) {
final newlyEmptyTopics = <String>[];
for (final MapEntry(key: topic, value: messageIds) in topics.entries) {
messageIds.removeWhere((id) => idsToRemove.contains(id));
if (messageIds.isEmpty) {
newlyEmptyTopics.add(topic);
void _removeAllInStreams(Set<int> idsToRemove) {
for (var messageId in idsToRemove) {
final info = _reverseStreamsLookup.remove(messageId);
if (info != null && streams[info.streamId]?[info.topic] != null) {
streams[info.streamId]![info.topic]!.remove(messageId);
if (streams[info.streamId]![info.topic]!.isEmpty) {
streams[info.streamId]!.remove(info.topic);
}
if (streams[info.streamId]!.isEmpty) {
streams.remove(info.streamId);
}
}
for (final topic in newlyEmptyTopics) {
topics.remove(topic);
}
if (topics.isEmpty) {
newlyEmptyStreams.add(streamId);
}
}
for (final streamId in newlyEmptyStreams) {
streams.remove(streamId);
}
for (var messageId in idsToRemove) {
_reverseStreamsLookup.remove(messageId);
}
}

Expand Down

0 comments on commit 32e37ca

Please sign in to comment.