From df8484e833f70edb0dfa76356d6aa7e4d6bb5fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Wed, 21 Feb 2024 16:41:16 +0100 Subject: [PATCH] [FIX] mail: fix performance issue with im status tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move compute of `imStatusTrackedPersonas` to its inverse field. Doing the compute on the "One" side of the relation prevents from looping and computing all values whenever there is a change in any of them. In practice, this reduces the "compute" time of the message fetch (which also fetch persona as authors of messages), reducing by approximately half the time it takes to insert these messages (depending on the number of persona it had to loop through). For example from 60ms to 30ms on my machine for message insert in "partner_1_125" (populate medium). closes odoo/odoo#154918 Signed-off-by: Alexandre Kühn (aku) --- addons/mail/static/src/core/common/persona_model.js | 10 ++++++++++ addons/mail/static/src/core/common/store_service.js | 10 +--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/addons/mail/static/src/core/common/persona_model.js b/addons/mail/static/src/core/common/persona_model.js index a94de05380b..8d57107b0d9 100644 --- a/addons/mail/static/src/core/common/persona_model.js +++ b/addons/mail/static/src/core/common/persona_model.js @@ -34,6 +34,16 @@ export class Persona extends Record { landlineNumber; /** @type {string} */ mobileNumber; + storeAsTrackedImStatus = Record.one("Store", { + /** @this {import("models").Persona} */ + compute() { + if (this.type === "partner" && this.im_status !== "im_partner" && !this.is_public) { + return this._store; + } + }, + eager: true, + inverse: "imStatusTrackedPersonas", + }); /** @type {'partner' | 'guest'} */ type; /** @type {string} */ diff --git a/addons/mail/static/src/core/common/store_service.js b/addons/mail/static/src/core/common/store_service.js index b2b0588279e..4ca136df4cb 100644 --- a/addons/mail/static/src/core/common/store_service.js +++ b/addons/mail/static/src/core/common/store_service.js @@ -74,18 +74,10 @@ export class Store extends BaseStore { users = {}; internalUserGroupId = null; imStatusTrackedPersonas = Record.many("Persona", { - compute() { - return Object.values(this.Persona?.records ?? []).filter( - (persona) => - persona.type === "partner" && - persona.im_status !== "im_partner" && - !persona.is_public - ); - }, + inverse: "storeAsTrackedImStatus", onUpdate() { this.updateImStatusRegistration(); }, - eager: true, }); hasLinkPreviewFeature = true; // messaging menu