From 942bea65d9b4a4e97dd765c93748d3ffee2076dd Mon Sep 17 00:00:00 2001 From: Greg Price Date: Mon, 27 Nov 2023 19:29:59 -0800 Subject: [PATCH] msglist: Optimize the _messageVisible check to avoid copying where possible --- lib/model/message_list.dart | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/model/message_list.dart b/lib/model/message_list.dart index 2ef95771e4f..f9f65e0aab3 100644 --- a/lib/model/message_list.dart +++ b/lib/model/message_list.dart @@ -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(): @@ -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 fetchInitial() async { // TODO(#80): fetch from anchor firstUnread, instead of newest @@ -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;