[PERF] mail: faster opening of messaging menu

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) <aku@odoo.com>
This commit is contained in:
Alexandre Kühn
2023-12-14 17:57:00 +00:00
parent 3803406298
commit e5e01b884e
2 changed files with 20 additions and 11 deletions
@@ -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
@@ -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);
}
}
}
});
}
});