Various performance tweaks (#474)

* Store and reuse room list placeholder avatars and last messages

* Cache and reuse HomeScreenRooms

* Reduce RoomSummaryProvider diff collection time

* Promote more logs to info

* Slighty tweak RustTracing to make it easier to configure

* Move TimelineProvider and RoomTimelineController item processing to background queues

* Prevent the timeline from stopping an ogoing decelerating scroll when starting backpaginating
This commit is contained in:
Stefan Ceriu
2023-01-20 16:40:23 +02:00
committed by GitHub
parent 70920550e7
commit ae8009d040
15 changed files with 167 additions and 95 deletions
@@ -95,6 +95,9 @@ struct HomeScreenViewStateBindings {
}
struct HomeScreenRoom: Identifiable, Equatable {
private static let placeholderLastMessage = AttributedString("Last message")
private static let placeholderAvatar = UIImage(systemName: "photo")
/// The list item identifier can be a real room identifier, a custom one for invalidated entries
/// or a completely unique one for empty items and skeletons
let id: String
@@ -102,11 +105,11 @@ struct HomeScreenRoom: Identifiable, Equatable {
/// The real room identifier this item points to
let roomId: String?
let name: String
var name = ""
let hasUnreads: Bool
var hasUnreads = false
let timestamp: String?
var timestamp: String?
var lastMessage: AttributedString?
@@ -122,8 +125,8 @@ struct HomeScreenRoom: Identifiable, Equatable {
name: "Placeholder room name",
hasUnreads: false,
timestamp: "Now",
lastMessage: AttributedString("Last message"),
avatar: UIImage(systemName: "photo"),
lastMessage: Self.placeholderLastMessage,
avatar: Self.placeholderAvatar,
isPlaceholder: true)
}
}
@@ -31,6 +31,8 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
private let visibleItemRangePublisher = CurrentValueSubject<Range<Int>, Never>(0..<0)
private var roomsForIdentifiers = [String: HomeScreenRoom]()
var callback: ((HomeScreenViewModelAction) -> Void)?
// MARK: - Setup
@@ -178,7 +180,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
// MARK: - Private
private func loadDataForRoomIdentifier(_ identifier: String) {
guard let room = state.rooms.first(where: { $0.roomId == identifier }),
guard let room = roomsForIdentifiers[identifier],
room.avatar == nil,
let avatarURL = room.avatarURL else {
return
@@ -206,6 +208,8 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
return
}
MXLog.info("Updating rooms")
var rooms = [HomeScreenRoom]()
// Try merging together results from both the visibleRoomsSummaryProvider and the allRoomsSummaryProvider
@@ -235,24 +239,34 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
}
state.rooms = rooms
MXLog.info("Finished updating rooms")
}
private func buildRoom(with details: RoomSummaryDetails) -> HomeScreenRoom {
let avatarImage = details.avatarURL.flatMap { userSession.mediaProvider.imageFromURL($0, avatarSize: .room(on: .home)) }
var room: HomeScreenRoom! = roomsForIdentifiers[details.id]
var timestamp: String?
if let lastMessageTimestamp = details.lastMessageTimestamp {
timestamp = lastMessageTimestamp.formatted(date: .omitted, time: .shortened)
if room == nil {
room = HomeScreenRoom(id: details.id,
roomId: details.id,
avatarURL: details.avatarURL)
}
return HomeScreenRoom(id: details.id,
roomId: details.id,
name: details.name,
hasUnreads: details.unreadNotificationCount > 0,
timestamp: timestamp,
lastMessage: details.lastMessage,
avatarURL: details.avatarURL,
avatar: avatarImage)
room.name = details.name
room.hasUnreads = details.unreadNotificationCount > 0
room.lastMessage = details.lastMessage
if let avatarURL = details.avatarURL {
room.avatar = userSession.mediaProvider.imageFromURL(avatarURL, avatarSize: .room(on: .home))
}
if let lastMessageTimestamp = details.lastMessageTimestamp {
room.timestamp = lastMessageTimestamp.formatted(date: .omitted, time: .shortened)
}
roomsForIdentifiers[details.id] = room
return room
}
private func updateVisibleRange(_ range: Range<Int>) {
@@ -69,7 +69,6 @@ class TimelineTableViewController: UIViewController {
didSet {
// Paginate again if the threshold hasn't been satisfied.
paginateBackwardsPublisher.send(())
applySnapshot()
}
}