From 94cdf97b0b3ef8f016635b2265799faf561fc69a Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 11 Feb 2022 15:13:12 +0100 Subject: [PATCH] Cache: Save more chats and less users --- src/config.ts | 5 ++- src/global/cache.ts | 65 +++++++++++++++++++++---------- src/modules/actions/api/chats.ts | 19 ++------- src/modules/selectors/messages.ts | 18 +++++++++ 4 files changed, 68 insertions(+), 39 deletions(-) diff --git a/src/config.ts b/src/config.ts index e6bf8047c..ab52e3bd8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -20,8 +20,9 @@ export const LEGACY_SESSION_KEY = 'GramJs:sessionId'; export const GLOBAL_STATE_CACHE_DISABLED = false; export const GLOBAL_STATE_CACHE_KEY = 'tt-global-state'; -export const GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT = 30; -export const GLOBAL_STATE_CACHE_USER_LIST_LIMIT = 5000; +export const GLOBAL_STATE_CACHE_USER_LIST_LIMIT = 500; +export const GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT = 200; +export const GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT = 30; export const MEDIA_CACHE_DISABLED = false; export const MEDIA_CACHE_NAME = 'tt-media'; diff --git a/src/global/cache.ts b/src/global/cache.ts index afe9ee246..223e8ed16 100644 --- a/src/global/cache.ts +++ b/src/global/cache.ts @@ -10,21 +10,29 @@ import { DEBUG, GLOBAL_STATE_CACHE_DISABLED, GLOBAL_STATE_CACHE_KEY, - GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT, - MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN, GLOBAL_STATE_CACHE_USER_LIST_LIMIT, + GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT, + GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT, + MIN_SCREEN_WIDTH_FOR_STATIC_RIGHT_COLUMN, DEFAULT_VOLUME, DEFAULT_PLAYBACK_RATE, + ALL_FOLDER_ID, + ARCHIVED_FOLDER_ID, } from '../config'; import { IS_SINGLE_COLUMN_LAYOUT } from '../util/environment'; import { isHeavyAnimating } from '../hooks/useHeavyAnimationCheck'; -import { pick } from '../util/iteratees'; -import { selectCurrentMessageList } from '../modules/selectors'; +import { pick, unique } from '../util/iteratees'; +import { + selectCurrentChat, + selectCurrentMessageList, + selectVisibleUsers, +} from '../modules/selectors'; import { hasStoredSession } from '../util/sessions'; import { INITIAL_STATE } from './initial'; import { parseLocationHash } from '../util/routing'; import { LOCATION_HASH } from '../hooks/useHistoryBack'; import { isUserId } from '../modules/helpers'; +import { getOrderedIds } from '../util/folderManager'; const UPDATE_THROTTLE = 5000; @@ -263,10 +271,21 @@ function reduceShowChatInfo(global: GlobalState): boolean { } function reduceUsers(global: GlobalState): GlobalState['users'] { - const { users: { byId, statusesById } } = global; - const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT).filter(isUserId); - const userIds = Object.keys(byId); - const idsToSave = chatIds.concat(userIds).slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT); + const { users: { byId, statusesById }, currentUserId } = global; + const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; + const visibleUserIds = selectVisibleUsers(global)?.map(({ id }) => id); + + const idsToSave = unique([ + ...currentUserId ? [currentUserId] : [], + ...currentChatId && isUserId(currentChatId) ? [currentChatId] : [], + ...visibleUserIds || [], + ...global.topPeers.userIds || [], + ...getOrderedIds(ALL_FOLDER_ID)?.filter(isUserId) || [], + ...getOrderedIds(ARCHIVED_FOLDER_ID)?.filter(isUserId) || [], + ...global.contactList?.userIds || [], + ...global.globalSearch.recentlyFoundChatIds?.filter(isUserId) || [], + ...Object.keys(byId), + ]).slice(0, GLOBAL_STATE_CACHE_USER_LIST_LIMIT); return { byId: pick(byId, idsToSave), @@ -275,29 +294,33 @@ function reduceUsers(global: GlobalState): GlobalState['users'] { } function reduceChats(global: GlobalState): GlobalState['chats'] { - const newListIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT); - const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; - const idsToSave = newListIds.concat(currentChatId ? [currentChatId] : []); + const { chats: { byId }, currentUserId } = global; + const currentChat = selectCurrentChat(global); + const idsToSave = unique([ + ...currentUserId ? [currentUserId] : [], + ...currentChat ? [currentChat.id] : [], + ...getOrderedIds(ALL_FOLDER_ID) || [], + ...getOrderedIds(ARCHIVED_FOLDER_ID) || [], + ...global.globalSearch.recentlyFoundChatIds || [], + ...Object.keys(byId), + ]).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT); return { ...global.chats, - byId: pick(global.chats.byId, idsToSave), - listIds: { - active: newListIds, - }, isFullyLoaded: {}, - orderedPinnedIds: { - active: global.chats.orderedPinnedIds.active, - }, + byId: pick(global.chats.byId, idsToSave), }; } function reduceMessages(global: GlobalState): GlobalState['messages'] { + const { currentUserId } = global; const byChatId: GlobalState['messages']['byChatId'] = {}; const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; - - const chatIds = (global.chats.listIds.active || []).slice(0, GLOBAL_STATE_CACHE_CHAT_LIST_LIMIT); - const chatIdsToSave = chatIds.concat(currentChatId ? [currentChatId] : []); + const chatIdsToSave = [ + ...currentChatId ? [currentChatId] : [], + ...currentUserId ? [currentUserId] : [], + ...getOrderedIds(ALL_FOLDER_ID)?.slice(0, GLOBAL_STATE_CACHE_CHATS_WITH_MESSAGES_LIMIT) || [], + ]; chatIdsToSave.forEach((chatId) => { const current = global.messages.byChatId[chatId]; diff --git a/src/modules/actions/api/chats.ts b/src/modules/actions/api/chats.ts index 48ee2dad4..c7d633d5a 100644 --- a/src/modules/actions/api/chats.ts +++ b/src/modules/actions/api/chats.ts @@ -29,7 +29,7 @@ import { selectChat, selectUser, selectChatListType, selectIsChatPinned, selectChatFolder, selectSupportChat, selectChatByUsername, selectThreadTopMessageId, selectCurrentMessageList, selectThreadInfo, selectCurrentChat, selectLastServiceNotification, - selectThreadParam, selectChatMessage, + selectVisibleUsers, } from '../../selectors'; import { buildCollectionByKey, omit } from '../../../util/iteratees'; import { debounce, pause, throttle } from '../../../util/schedulers'; @@ -1011,23 +1011,10 @@ async function loadChats( global = getGlobal(); if (shouldReplace && listType === 'active') { - const visibleChats = []; - const visibleUsers = []; - const currentChat = selectCurrentChat(global); - if (currentChat) { - const { threadId } = selectCurrentMessageList(global)!; - visibleChats.push(currentChat); - const messageIds = selectThreadParam(global, currentChat.id, threadId, 'viewportIds'); - const messageSenders = messageIds ? messageIds - .map((messageId) => { - const { senderId } = selectChatMessage(global, currentChat.id, messageId) || {}; - return senderId ? selectUser(global, senderId) : undefined; - }) - .filter(Boolean) : []; - visibleUsers.push(...messageSenders); - } + const visibleChats = currentChat ? [currentChat] : []; + const visibleUsers = selectVisibleUsers(global) || []; if (global.currentUserId && global.users.byId[global.currentUserId]) { visibleUsers.push(global.users.byId[global.currentUserId]); } diff --git a/src/modules/selectors/messages.ts b/src/modules/selectors/messages.ts index 302586e40..874252cb9 100644 --- a/src/modules/selectors/messages.ts +++ b/src/modules/selectors/messages.ts @@ -904,3 +904,21 @@ export function selectDefaultReaction(global: GlobalState, chatId: string) { return defaultReaction; } + +// Slow, not to be used in `withGlobal` +export function selectVisibleUsers(global: GlobalState) { + const { chatId, threadId } = selectCurrentMessageList(global) || {}; + if (!chatId || !threadId) { + return undefined; + } + + const messageIds = selectThreadParam(global, chatId, threadId, 'viewportIds'); + if (!messageIds) { + return undefined; + } + + return messageIds.map((messageId) => { + const { senderId } = selectChatMessage(global, chatId, messageId) || {}; + return senderId ? selectUser(global, senderId) : undefined; + }).filter(Boolean); +}