Skip to content

Commit

Permalink
msglist: Optimize the _messageVisible check to avoid copying where po…
Browse files Browse the repository at this point in the history
…ssible
  • Loading branch information
gnprice committed Dec 19, 2023
1 parent d56c02e commit 942bea6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lib/model/message_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
///
/// This depends in particular on whether the message is muted in
/// one way or another.
///
/// See also [_allMessagesVisible].
bool _messageVisible(Message message) {
switch (narrow) {
case AllMessagesNarrow():
Expand All @@ -332,6 +334,21 @@ class MessageListView with ChangeNotifier, _MessageSequence {
}
}

/// Whether [_messageVisible] is true for all possible messages.
///
/// This is useful for an optimization.
bool get _allMessagesVisible {
switch (narrow) {
case AllMessagesNarrow():
case StreamNarrow():
return false;

case TopicNarrow():
case DmNarrow():
return true;
}
}

/// Fetch messages, starting from scratch.
Future<void> fetchInitial() async {
// TODO(#80): fetch from anchor firstUnread, instead of newest
Expand Down Expand Up @@ -380,7 +397,11 @@ class MessageListView with ChangeNotifier, _MessageSequence {
result.messages.removeLast();
}

_insertAllMessages(0, result.messages.where(_messageVisible));
final fetchedMessages = _allMessagesVisible
? result.messages // Avoid unnecessarily copying the list.
: result.messages.where(_messageVisible);

_insertAllMessages(0, fetchedMessages);
_haveOldest = result.foundOldest;
} finally {
_fetchingOlder = false;
Expand Down

0 comments on commit 942bea6

Please sign in to comment.