From f3f1f8b99bda7313c31b55f58a2b6c7a80a691d6 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Sat, 16 Apr 2022 20:53:07 -0400 Subject: [PATCH 1/3] Improve performance of switching to rooms with lots of servers and ACLs By not processing the *entire* list of servers and ACLs when determining how to create permalinks, this shaves ~100 ms off of switches into high-traffic rooms. --- src/utils/permalinks/Permalinks.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/utils/permalinks/Permalinks.ts b/src/utils/permalinks/Permalinks.ts index 015b42ea239..87a27e15605 100644 --- a/src/utils/permalinks/Permalinks.ts +++ b/src/utils/permalinks/Permalinks.ts @@ -166,9 +166,6 @@ export class RoomPermalinkCreator { // updates, but they were on member events which can be very numerous, so the incremental // updates ended up being much slower than a full update. We now have the batch state update // event, so we just update in full, but on each batch of updates. - // A full update takes about 120ms for me on Matrix HQ, which still feels like way too long - // to be spending worrying about how we might generate a permalink, but it's better than - // multiple seconds. this.updateAllowedServers(); this.updateHighestPlUser(); this.updatePopulationMap(); @@ -247,16 +244,19 @@ export class RoomPermalinkCreator { } const serversByPopulation = Object.keys(this.populationMap) - .sort((a, b) => this.populationMap[b] - this.populationMap[a]) - .filter(a => { - return !candidates.includes(a) && - !isHostnameIpAddress(a) && - !isHostInRegex(a, this.bannedHostsRegexps) && - isHostInRegex(a, this.allowedHostsRegexps); - }); - - const remainingServers = serversByPopulation.slice(0, MAX_SERVER_CANDIDATES - candidates.length); - candidates = candidates.concat(remainingServers); + .sort((a, b) => this.populationMap[b] - this.populationMap[a]); + + for (let i = 0; i < serversByPopulation.length && candidates.length < MAX_SERVER_CANDIDATES; i++) { + const server = serversByPopulation[i]; + if ( + !candidates.includes(server) && + !isHostnameIpAddress(server) && + !isHostInRegex(server, this.bannedHostsRegexps) && + isHostInRegex(server, this.allowedHostsRegexps) + ) { + candidates.push(server); + } + } this._serverCandidates = candidates; }; @@ -447,12 +447,12 @@ function getHostnameFromMatrixDomain(domain: string): string { return new URL(`https://${domain}`).hostname; } -function isHostInRegex(hostname: string, regexps: RegExp[]) { +function isHostInRegex(hostname: string, regexps: RegExp[]): boolean { hostname = getHostnameFromMatrixDomain(hostname); if (!hostname) return true; // assumed if (regexps.length > 0 && !regexps[0].test) throw new Error(regexps[0].toString()); - return regexps.filter(h => h.test(hostname)).length > 0; + return regexps.some(h => h.test(hostname)); } function isHostnameIpAddress(hostname: string): boolean { From f0e7b644290e109d0885314d880fdafef21f2ec5 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Sat, 16 Apr 2022 21:06:00 -0400 Subject: [PATCH 2/3] Fix lint --- src/utils/permalinks/Permalinks.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/permalinks/Permalinks.ts b/src/utils/permalinks/Permalinks.ts index 87a27e15605..a3c305c2e37 100644 --- a/src/utils/permalinks/Permalinks.ts +++ b/src/utils/permalinks/Permalinks.ts @@ -238,7 +238,7 @@ export class RoomPermalinkCreator { } private updateServerCandidates = () => { - let candidates = []; + const candidates = []; if (this.highestPlUserId) { candidates.push(getServerName(this.highestPlUserId)); } From bd8ff17ef684a59d47ef0ea8cc0fda75bd7fcaa7 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Sun, 17 Apr 2022 12:42:41 -0400 Subject: [PATCH 3/3] Ensure that permalink server candidates can't be duplicates --- src/utils/permalinks/Permalinks.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/permalinks/Permalinks.ts b/src/utils/permalinks/Permalinks.ts index a3c305c2e37..8c213a8cffc 100644 --- a/src/utils/permalinks/Permalinks.ts +++ b/src/utils/permalinks/Permalinks.ts @@ -238,27 +238,27 @@ export class RoomPermalinkCreator { } private updateServerCandidates = () => { - const candidates = []; + const candidates = new Set(); if (this.highestPlUserId) { - candidates.push(getServerName(this.highestPlUserId)); + candidates.add(getServerName(this.highestPlUserId)); } const serversByPopulation = Object.keys(this.populationMap) .sort((a, b) => this.populationMap[b] - this.populationMap[a]); - for (let i = 0; i < serversByPopulation.length && candidates.length < MAX_SERVER_CANDIDATES; i++) { + for (let i = 0; i < serversByPopulation.length && candidates.size < MAX_SERVER_CANDIDATES; i++) { const server = serversByPopulation[i]; if ( - !candidates.includes(server) && + !candidates.has(server) && !isHostnameIpAddress(server) && !isHostInRegex(server, this.bannedHostsRegexps) && isHostInRegex(server, this.allowedHostsRegexps) ) { - candidates.push(server); + candidates.add(server); } } - this._serverCandidates = candidates; + this._serverCandidates = [...candidates]; }; }