Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Improve performance of switching to rooms with lots of servers and ACLs #8347

Merged
merged 3 commits into from
Apr 17, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/utils/permalinks/Permalinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -241,22 +238,25 @@ export class RoomPermalinkCreator {
}

private updateServerCandidates = () => {
let candidates = [];
const candidates = [];
if (this.highestPlUserId) {
candidates.push(getServerName(this.highestPlUserId));
robintown marked this conversation as resolved.
Show resolved Hide resolved
}

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;
};
Expand Down Expand Up @@ -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 {
Expand Down