Skip to content

Commit 942bea6

Browse files
committed
msglist: Optimize the _messageVisible check to avoid copying where possible
1 parent d56c02e commit 942bea6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

lib/model/message_list.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ class MessageListView with ChangeNotifier, _MessageSequence {
312312
///
313313
/// This depends in particular on whether the message is muted in
314314
/// one way or another.
315+
///
316+
/// See also [_allMessagesVisible].
315317
bool _messageVisible(Message message) {
316318
switch (narrow) {
317319
case AllMessagesNarrow():
@@ -332,6 +334,21 @@ class MessageListView with ChangeNotifier, _MessageSequence {
332334
}
333335
}
334336

337+
/// Whether [_messageVisible] is true for all possible messages.
338+
///
339+
/// This is useful for an optimization.
340+
bool get _allMessagesVisible {
341+
switch (narrow) {
342+
case AllMessagesNarrow():
343+
case StreamNarrow():
344+
return false;
345+
346+
case TopicNarrow():
347+
case DmNarrow():
348+
return true;
349+
}
350+
}
351+
335352
/// Fetch messages, starting from scratch.
336353
Future<void> fetchInitial() async {
337354
// TODO(#80): fetch from anchor firstUnread, instead of newest
@@ -380,7 +397,11 @@ class MessageListView with ChangeNotifier, _MessageSequence {
380397
result.messages.removeLast();
381398
}
382399

383-
_insertAllMessages(0, result.messages.where(_messageVisible));
400+
final fetchedMessages = _allMessagesVisible
401+
? result.messages // Avoid unnecessarily copying the list.
402+
: result.messages.where(_messageVisible);
403+
404+
_insertAllMessages(0, fetchedMessages);
384405
_haveOldest = result.foundOldest;
385406
} finally {
386407
_fetchingOlder = false;

0 commit comments

Comments
 (0)