Merge pull request #6063 from matrix-org/t3chguy/fix/17369

Move SettingsStore watchers/monitors over to ES6 maps for performance
pull/21833/head
Michael Telatynski 2021-05-25 10:58:09 +01:00 committed by GitHub
commit 97c6ee39d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 39 deletions

View File

@ -26,7 +26,7 @@ import { _t } from '../languageHandler';
import dis from '../dispatcher/dispatcher';
import { ISetting, SETTINGS } from "./Settings";
import LocalEchoWrapper from "./handlers/LocalEchoWrapper";
import { WatchManager } from "./WatchManager";
import { WatchManager, CallbackFn as WatchCallbackFn } from "./WatchManager";
import { SettingLevel } from "./SettingLevel";
import SettingsHandler from "./handlers/SettingsHandler";
@ -117,8 +117,8 @@ export default class SettingsStore {
// We also maintain a list of monitors which are special watchers: they cause dispatches
// when the setting changes. We track which rooms we're monitoring though to ensure we
// don't duplicate updates on the bus.
private static watchers = {}; // { callbackRef => { callbackFn } }
private static monitors = {}; // { settingName => { roomId => callbackRef } }
private static watchers = new Map<string, WatchCallbackFn>();
private static monitors = new Map<string, Map<string, string>>(); // { settingName => { roomId => callbackRef } }
// Counter used for generation of watcher IDs
private static watcherCount = 1;
@ -163,7 +163,7 @@ export default class SettingsStore {
callbackFn(originalSettingName, changedInRoomId, atLevel, newValAtLevel, newValue);
};
SettingsStore.watchers[watcherId] = localizedCallback;
SettingsStore.watchers.set(watcherId, localizedCallback);
defaultWatchManager.watchSetting(settingName, roomId, localizedCallback);
return watcherId;
@ -176,13 +176,13 @@ export default class SettingsStore {
* to cancel.
*/
public static unwatchSetting(watcherReference: string) {
if (!SettingsStore.watchers[watcherReference]) {
if (!SettingsStore.watchers.has(watcherReference)) {
console.warn(`Ending non-existent watcher ID ${watcherReference}`);
return;
}
defaultWatchManager.unwatchSetting(SettingsStore.watchers[watcherReference]);
delete SettingsStore.watchers[watcherReference];
defaultWatchManager.unwatchSetting(SettingsStore.watchers.get(watcherReference));
SettingsStore.watchers.delete(watcherReference);
}
/**
@ -196,10 +196,10 @@ export default class SettingsStore {
public static monitorSetting(settingName: string, roomId: string) {
roomId = roomId || null; // the thing wants null specifically to work, so appease it.
if (!this.monitors[settingName]) this.monitors[settingName] = {};
if (!this.monitors.has(settingName)) this.monitors.set(settingName, new Map());
const registerWatcher = () => {
this.monitors[settingName][roomId] = SettingsStore.watchSetting(
this.monitors.get(settingName).set(roomId, SettingsStore.watchSetting(
settingName, roomId, (settingName, inRoomId, level, newValueAtLevel, newValue) => {
dis.dispatch({
action: 'setting_updated',
@ -210,19 +210,20 @@ export default class SettingsStore {
newValue,
});
},
);
));
};
const hasRoom = Object.keys(this.monitors[settingName]).find((r) => r === roomId || r === null);
const rooms = Array.from(this.monitors.get(settingName).keys());
const hasRoom = rooms.find((r) => r === roomId || r === null);
if (!hasRoom) {
registerWatcher();
} else {
if (roomId === null) {
// Unregister all existing watchers and register the new one
for (const roomId of Object.keys(this.monitors[settingName])) {
SettingsStore.unwatchSetting(this.monitors[settingName][roomId]);
}
this.monitors[settingName] = {};
rooms.forEach(roomId => {
SettingsStore.unwatchSetting(this.monitors.get(settingName).get(roomId));
});
this.monitors.get(settingName).clear();
registerWatcher();
} // else a watcher is already registered for the room, so don't bother registering it again
}

View File

@ -18,11 +18,7 @@ import { SettingLevel } from "./SettingLevel";
export type CallbackFn = (changedInRoomId: string, atLevel: SettingLevel, newValAtLevel: any) => void;
const IRRELEVANT_ROOM: string = null;
interface RoomWatcherMap {
[roomId: string]: CallbackFn[];
}
const IRRELEVANT_ROOM = Symbol("irrelevant-room");
/**
* Generalized management class for dealing with watchers on a per-handler (per-level)
@ -30,25 +26,25 @@ interface RoomWatcherMap {
* class, which are then proxied outwards to any applicable watchers.
*/
export class WatchManager {
private watchers: {[settingName: string]: RoomWatcherMap} = {};
private watchers = new Map<string, Map<string | symbol, CallbackFn[]>>(); // settingName -> roomId -> CallbackFn[]
// Proxy for handlers to delegate changes to this manager
public watchSetting(settingName: string, roomId: string | null, cb: CallbackFn) {
if (!this.watchers[settingName]) this.watchers[settingName] = {};
if (!this.watchers[settingName][roomId]) this.watchers[settingName][roomId] = [];
this.watchers[settingName][roomId].push(cb);
if (!this.watchers.has(settingName)) this.watchers.set(settingName, new Map());
if (!this.watchers.get(settingName).has(roomId)) this.watchers.get(settingName).set(roomId, []);
this.watchers.get(settingName).get(roomId).push(cb);
}
// Proxy for handlers to delegate changes to this manager
public unwatchSetting(cb: CallbackFn) {
for (const settingName of Object.keys(this.watchers)) {
for (const roomId of Object.keys(this.watchers[settingName])) {
this.watchers.forEach((map) => {
map.forEach((callbacks) => {
let idx;
while ((idx = this.watchers[settingName][roomId].indexOf(cb)) !== -1) {
this.watchers[settingName][roomId].splice(idx, 1);
while ((idx = callbacks.indexOf(cb)) !== -1) {
callbacks.splice(idx, 1);
}
}
}
});
});
}
public notifyUpdate(settingName: string, inRoomId: string | null, atLevel: SettingLevel, newValueAtLevel: any) {
@ -56,21 +52,21 @@ export class WatchManager {
// we also don't have a reliable way to get the old value of a setting. Instead, we'll just
// let it fall through regardless and let the receiver dedupe if they want to.
if (!this.watchers[settingName]) return;
if (!this.watchers.has(settingName)) return;
const roomWatchers = this.watchers[settingName];
const roomWatchers = this.watchers.get(settingName);
const callbacks = [];
if (inRoomId !== null && roomWatchers[inRoomId]) {
callbacks.push(...roomWatchers[inRoomId]);
if (inRoomId !== null && roomWatchers.has(inRoomId)) {
callbacks.push(...roomWatchers.get(inRoomId));
}
if (!inRoomId) {
// Fire updates to all the individual room watchers too, as they probably
// care about the change higher up.
callbacks.push(...Object.values(roomWatchers).flat(1));
} else if (roomWatchers[IRRELEVANT_ROOM]) {
callbacks.push(...roomWatchers[IRRELEVANT_ROOM]);
// Fire updates to all the individual room watchers too, as they probably care about the change higher up.
const callbacks = Array.from(roomWatchers.values()).flat(1);
callbacks.push(...callbacks);
} else if (roomWatchers.has(IRRELEVANT_ROOM)) {
callbacks.push(...roomWatchers.get(IRRELEVANT_ROOM));
}
for (const callback of callbacks) {