From c34ef129ecbb70ab11066ee0add8f48afecfaa07 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 22 Jan 2023 18:12:29 +0100 Subject: [PATCH] [Perf] Avoid redundant updates of chat and user `usernames` field --- src/global/reducers/chats.ts | 22 ++++++++++++++++------ src/global/reducers/users.ts | 12 +++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/global/reducers/chats.ts b/src/global/reducers/chats.ts index 7dfb3601f..601c2ad81 100644 --- a/src/global/reducers/chats.ts +++ b/src/global/reducers/chats.ts @@ -9,6 +9,7 @@ import { } from '../../util/iteratees'; import { selectChat, selectChatListType } from '../selectors'; import { updateThread, updateThreadInfo } from './messages'; +import { areDeepEqual } from '../../util/areDeepEqual'; export function replaceChatListIds( global: GlobalState, @@ -125,15 +126,24 @@ function getUpdatedChat( ) { const { byId } = global.chats; const chat = byId[chatId]; - const shouldOmitMinInfo = chatUpdate.isMin && chat && !chat.isMin; + const omitProps: (keyof ApiChat)[] = []; + + const shouldOmitMinInfo = chatUpdate.isMin && chat && !chat.isMin; + if (shouldOmitMinInfo) { + omitProps.push('isMin', 'accessHash'); + } + + if (!noOmitUnreadReactionCount) { + omitProps.push('unreadReactionsCount'); + } + + if (areDeepEqual(chat?.usernames, chatUpdate.usernames)) { + omitProps.push('usernames'); + } - chatUpdate = noOmitUnreadReactionCount - ? chatUpdate : omit(chatUpdate, ['unreadReactionsCount']); const updatedChat: ApiChat = { ...chat, - ...(shouldOmitMinInfo - ? omit(chatUpdate, ['isMin', 'accessHash']) - : chatUpdate), + ...omit(chatUpdate, omitProps), ...(photo && { photos: [photo, ...(chat.photos || [])] }), }; diff --git a/src/global/reducers/users.ts b/src/global/reducers/users.ts index 9f71af591..cac6bd5ec 100644 --- a/src/global/reducers/users.ts +++ b/src/global/reducers/users.ts @@ -4,6 +4,7 @@ import type { ApiUser, ApiUserStatus } from '../../api/types'; import { omit, pick } from '../../util/iteratees'; import { MEMO_EMPTY_ARRAY } from '../../util/memo'; import { updateChat } from './chats'; +import { areDeepEqual } from '../../util/areDeepEqual'; export function replaceUsers(global: GlobalState, newById: Record): GlobalState { return { @@ -109,11 +110,20 @@ export function addUsers(global: GlobalState, newById: Record): function getUpdatedUser(global: GlobalState, userId: string, userUpdate: Partial) { const { byId } = global.users; const user = byId[userId]; + const omitProps: (keyof ApiUser)[] = []; + const shouldOmitMinInfo = userUpdate.isMin && user && !user.isMin; + if (shouldOmitMinInfo) { + omitProps.push('isMin', 'accessHash'); + } + + if (areDeepEqual(user?.usernames, userUpdate.usernames)) { + omitProps.push('usernames'); + } const updatedUser = { ...user, - ...(shouldOmitMinInfo ? omit(userUpdate, ['isMin', 'accessHash']) : userUpdate), + ...omit(userUpdate, omitProps), }; if (!updatedUser.id || !updatedUser.type) {