From e5e01b884e12fdea3cf3e33846f179919e0f4a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Tue, 12 Dec 2023 16:12:29 +0100 Subject: [PATCH] [PERF] mail: faster opening of messaging menu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, when many channels were pinned in Discuss, the messaging menu took a while to open and render all items. This happens because `fetchPreviews()` and `inbox.fetchNewMessages()` were inserting data for each thread and message. This meant computed and sorted fields were called with that many objects. This commit improves the performances by wrapping all of it in an update cycle transaction, so that computed and sorted fields are invoked only once at the end of the update cycle. With `contacts` installed, populate `medium`: - Before this commit: 1min. - With this commit: 2sec. (30x faster) closes odoo/odoo#145980 Signed-off-by: Alexandre Kühn (aku) --- addons/mail/static/src/core/common/record.js | 1 + .../static/src/core/common/thread_service.js | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/addons/mail/static/src/core/common/record.js b/addons/mail/static/src/core/common/record.js index 7acabf05219..ad311c81103 100644 --- a/addons/mail/static/src/core/common/record.js +++ b/addons/mail/static/src/core/common/record.js @@ -29,6 +29,7 @@ export function OR(...args) { } export function makeStore(env) { + Record.UPDATE = 0; let storeReady = false; const res = { // fake store for now, until it becomes a model diff --git a/addons/mail/static/src/core/common/thread_service.js b/addons/mail/static/src/core/common/thread_service.js index ee1dce06e4a..bc214be2b3e 100644 --- a/addons/mail/static/src/core/common/thread_service.js +++ b/addons/mail/static/src/core/common/thread_service.js @@ -2,6 +2,7 @@ import { loadEmoji } from "@web/core/emoji_picker/emoji_picker"; import { DEFAULT_AVATAR } from "@mail/core/common/persona_service"; +import { Record } from "@mail/core/common/record"; import { prettifyMessageContent } from "@mail/utils/common/format"; import { browser } from "@web/core/browser/browser"; @@ -255,12 +256,14 @@ export class ThreadService { // same for needaction messages, special case for mailbox: // kinda "fetch new/more" with needactions on many origin threads at once if (thread.eq(this.store.discuss.inbox)) { - for (const message of fetched) { - const thread = message.originThread; - if (message.notIn(thread.needactionMessages)) { - thread.needactionMessages.unshift(message); + Record.MAKE_UPDATE(() => { + for (const message of fetched) { + const thread = message.originThread; + if (thread && message.notIn(thread.needactionMessages)) { + thread.needactionMessages.unshift(message); + } } - } + }); } else { const startNeedactionIndex = after === undefined @@ -329,13 +332,18 @@ export class ThreadService { } if (ids.length) { const previews = await this.orm.call("discuss.channel", "channel_fetch_preview", [ids]); - for (const preview of previews) { - const thread = this.store.Thread.get({ model: "discuss.channel", id: preview.id }); - const message = this.store.Message.insert(preview.last_message, { html: true }); - if (message.isNeedaction) { - thread.needactionMessages.add(message); + Record.MAKE_UPDATE(() => { + for (const preview of previews) { + const thread = this.store.Thread.get({ + model: "discuss.channel", + id: preview.id, + }); + const message = this.store.Message.insert(preview.last_message, { html: true }); + if (message.isNeedaction) { + thread.needactionMessages.add(message); + } } - } + }); } });