From 3347e00d2a8319b3b61edd7a0ce3229af49893fa Mon Sep 17 00:00:00 2001 From: Toomas Vahter Date: Thu, 25 Jul 2024 12:57:52 +0300 Subject: [PATCH] Improve performance of ChatChannel and ChatMessage equality which caused unnecessary copies of structs --- CHANGELOG.md | 2 + Sources/StreamChat/Models/Channel.swift | 43 ++++++++++--------- Sources/StreamChat/Models/ChatMessage.swift | 47 +++++++++++---------- 3 files changed, 48 insertions(+), 44 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index de37d81a919..97b7a2326a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming ## StreamChat +### ⚡ Performance +- Improve performance of `ChatChannel` and `ChatMessage` equality checks [#3335](https://github.com/GetStream/stream-chat-swift/pull/3335) ### ✅ Added - Expose `MissingConnectionId` + `InvalidURL` + `InvalidJSON` Errors [#3332](https://github.com/GetStream/stream-chat-swift/pull/3332) diff --git a/Sources/StreamChat/Models/Channel.swift b/Sources/StreamChat/Models/Channel.swift index 9b797b2bac0..a89243367a2 100644 --- a/Sources/StreamChat/Models/Channel.swift +++ b/Sources/StreamChat/Models/Channel.swift @@ -237,27 +237,28 @@ extension ChatChannel: AnyChannel {} extension ChatChannel: Hashable { public static func == (lhs: ChatChannel, rhs: ChatChannel) -> Bool { - lhs.cid == rhs.cid && - lhs.updatedAt == rhs.updatedAt && - lhs.cooldownDuration == rhs.cooldownDuration && - lhs.createdAt == rhs.createdAt && - lhs.createdBy == rhs.createdBy && - lhs.deletedAt == rhs.deletedAt && - lhs.extraData == rhs.extraData && - lhs.imageURL == rhs.imageURL && - lhs.isFrozen == rhs.isFrozen && - lhs.isHidden == rhs.isHidden && - lhs.lastMessageAt == rhs.lastMessageAt && - lhs.memberCount == rhs.memberCount && - lhs.membership == rhs.membership && - lhs.muteDetails == rhs.muteDetails && - lhs.name == rhs.name && - lhs.ownCapabilities == rhs.ownCapabilities && - lhs.previewMessage == rhs.previewMessage && - lhs.reads == rhs.reads && - lhs.team == rhs.team && - lhs.truncatedAt == rhs.truncatedAt && - lhs.watcherCount == rhs.watcherCount + guard lhs.cid == rhs.cid else { return false } + guard lhs.updatedAt == rhs.updatedAt else { return false } + guard lhs.lastMessageAt == rhs.lastMessageAt else { return false } + guard lhs.muteDetails == rhs.muteDetails else { return false } + guard lhs.reads == rhs.reads else { return false } + guard lhs.previewMessage == rhs.previewMessage else { return false } + guard lhs.name == rhs.name else { return false } + guard lhs.watcherCount == rhs.watcherCount else { return false } + guard lhs.createdAt == rhs.createdAt else { return false } + guard lhs.cooldownDuration == rhs.cooldownDuration else { return false } + guard lhs.createdBy == rhs.createdBy else { return false } + guard lhs.deletedAt == rhs.deletedAt else { return false } + guard lhs.extraData == rhs.extraData else { return false } + guard lhs.imageURL == rhs.imageURL else { return false } + guard lhs.isFrozen == rhs.isFrozen else { return false } + guard lhs.isHidden == rhs.isHidden else { return false } + guard lhs.memberCount == rhs.memberCount else { return false } + guard lhs.membership == rhs.membership else { return false } + guard lhs.team == rhs.team else { return false } + guard lhs.truncatedAt == rhs.truncatedAt else { return false } + guard lhs.ownCapabilities == rhs.ownCapabilities else { return false } + return true } public func hash(into hasher: inout Hasher) { diff --git a/Sources/StreamChat/Models/ChatMessage.swift b/Sources/StreamChat/Models/ChatMessage.swift index 6b86bf28227..948155d79a9 100644 --- a/Sources/StreamChat/Models/ChatMessage.swift +++ b/Sources/StreamChat/Models/ChatMessage.swift @@ -359,29 +359,30 @@ public extension ChatMessage { extension ChatMessage: Hashable { public static func == (lhs: Self, rhs: Self) -> Bool { - lhs.id == rhs.id && - lhs.updatedAt == rhs.updatedAt && - lhs.allAttachments == rhs.allAttachments && - lhs.arguments == rhs.arguments && - lhs.author == rhs.author && - lhs.command == rhs.command && - lhs.currentUserReactionsCount == rhs.currentUserReactionsCount && - lhs.extraData == rhs.extraData && - lhs.isFlaggedByCurrentUser == rhs.isFlaggedByCurrentUser && - lhs.isShadowed == rhs.isShadowed && - lhs.localState == rhs.localState && - lhs.parentMessageId == rhs.parentMessageId && - lhs.quotedMessage == rhs.quotedMessage && - lhs.reactionCounts == rhs.reactionCounts && - lhs.reactionGroups == rhs.reactionGroups && - lhs.reactionScores == rhs.reactionScores && - lhs.readByCount == rhs.readByCount && - lhs.replyCount == rhs.replyCount && - lhs.showReplyInChannel == rhs.showReplyInChannel && - lhs.text == rhs.text && - lhs.threadParticipantsCount == rhs.threadParticipantsCount && - lhs.translations == rhs.translations && - lhs.type == rhs.type + guard lhs.id == rhs.id else { return false } + guard lhs.localState == rhs.localState else { return false } + guard lhs.updatedAt == rhs.updatedAt else { return false } + guard lhs.allAttachments == rhs.allAttachments else { return false } + guard lhs.author == rhs.author else { return false } + guard lhs.currentUserReactionsCount == rhs.currentUserReactionsCount else { return false } + guard lhs.text == rhs.text else { return false } + guard lhs.parentMessageId == rhs.parentMessageId else { return false } + guard lhs.reactionCounts == rhs.reactionCounts else { return false } + guard lhs.reactionGroups == rhs.reactionGroups else { return false } + guard lhs.reactionScores == rhs.reactionScores else { return false } + guard lhs.readByCount == rhs.readByCount else { return false } + guard lhs.replyCount == rhs.replyCount else { return false } + guard lhs.showReplyInChannel == rhs.showReplyInChannel else { return false } + guard lhs.threadParticipantsCount == rhs.threadParticipantsCount else { return false } + guard lhs.arguments == rhs.arguments else { return false } + guard lhs.command == rhs.command else { return false } + guard lhs.extraData == rhs.extraData else { return false } + guard lhs.isFlaggedByCurrentUser == rhs.isFlaggedByCurrentUser else { return false } + guard lhs.isShadowed == rhs.isShadowed else { return false } + guard lhs.quotedMessage == rhs.quotedMessage else { return false } + guard lhs.translations == rhs.translations else { return false } + guard lhs.type == rhs.type else { return false } + return true } public func hash(into hasher: inout Hasher) {