From d97ff6820ef4f1ee1be100435783c663fdd2090c Mon Sep 17 00:00:00 2001 From: tsm-odoo Date: Fri, 7 Jul 2023 09:53:48 +0000 Subject: [PATCH] [FIX] mail: fix slow follower insertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, follower insertion checked if a follower was already in the thread's follower list by using `Array.prototype.includes`. Since this method must run through every item in the array to tell whether or not a follower is present, it is extremly slow with large arrays (especially since it is called for each follower). This would slow down the tab for threads having lots of follower (issue starts appering at 5k+ followers). This PR solves the issue by replacing thread follower lists by sets. With this change, inserting even 15k+ follower is almost instantaneous since the membership test is done in O(1) instead of O(n). closes odoo/odoo#127833 X-original-commit: 406c91c8cba32e01eaa8df4b7fb9d0e3ad821e9c Signed-off-by: Alexandre Kühn (aku) --- .../mail/static/src/core/common/suggestion_service.js | 5 +++-- addons/mail/static/src/core/common/thread_model.js | 11 ++++++++--- addons/mail/static/src/core/web/chatter.js | 4 ++-- addons/mail/static/src/core/web/chatter.xml | 10 +++++----- addons/mail/static/src/core/web/recipient_list.xml | 2 +- .../mail/static/src/core/web/thread_service_patch.js | 9 ++------- 6 files changed, 21 insertions(+), 20 deletions(-) diff --git a/addons/mail/static/src/core/common/suggestion_service.js b/addons/mail/static/src/core/common/suggestion_service.js index 2a4a8a3e5db..32d2dc2ff3c 100644 --- a/addons/mail/static/src/core/common/suggestion_service.js +++ b/addons/mail/static/src/core/common/suggestion_service.js @@ -208,8 +208,9 @@ export class SuggestionService { } } if (thread) { - const isFollower1 = thread.followers.some((follower) => follower.partner === p1); - const isFollower2 = thread.followers.some((follower) => follower.partner === p2); + const followerList = [...thread.followers]; + const isFollower1 = followerList.some((follower) => follower.partner === p1); + const isFollower2 = followerList.some((follower) => follower.partner === p2); if (isFollower1 && !isFollower2) { return -1; } diff --git a/addons/mail/static/src/core/common/thread_model.js b/addons/mail/static/src/core/common/thread_model.js index b7e2732204a..a4697fcf301 100644 --- a/addons/mail/static/src/core/common/thread_model.js +++ b/addons/mail/static/src/core/common/thread_model.js @@ -51,8 +51,8 @@ export class Thread { customName; /** @type {string} */ description; - /** @type {import("@mail/core/common/follower_model").Follower[]} */ - followers = []; + /** @type {Set} */ + followers = new Set(); isAdmin = false; loadOlder = false; loadNewer = false; @@ -256,7 +256,12 @@ export class Thread { * @returns {import("@mail/core/common/follower_model").Follower} */ get followerOfSelf() { - return this.followers.find((f) => f.partner === this._store.self); + for (const follower of this.followers) { + if (follower.partner === this._store.self) { + return follower; + } + } + return undefined; } get imgUrl() { diff --git a/addons/mail/static/src/core/web/chatter.js b/addons/mail/static/src/core/web/chatter.js index 3b3427f4a2c..7418feddb4b 100644 --- a/addons/mail/static/src/core/web/chatter.js +++ b/addons/mail/static/src/core/web/chatter.js @@ -201,7 +201,7 @@ export class Chatter extends Component { * @returns {string} */ get toRecipientsText() { - const followers = this.state.thread.followers.slice(0, 5).map(({ partner }) => { + const followers = [...this.state.thread.followers].slice(0, 5).map(({ partner }) => { if (partner === this.store.self) { return `me`; } @@ -213,7 +213,7 @@ export class Chatter extends Component { this.store.env.services["user"].lang?.replace("_", "-"), { type: "unit" } ); - if (this.state.thread.followers.length > 5) { + if (this.state.thread.followers.size > 5) { followers.push("…"); } return markup(formatter.format(followers)); diff --git a/addons/mail/static/src/core/web/chatter.xml b/addons/mail/static/src/core/web/chatter.xml index c9403fec8d4..34e2a9ebe30 100644 --- a/addons/mail/static/src/core/web/chatter.xml +++ b/addons/mail/static/src/core/web/chatter.xml @@ -35,17 +35,17 @@ - + Add Followers -