Skip to content

Commit 537938e

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Optimize makeGetStatus (mastodon#11211)
* Optimize makeGetStatus Because `ImmutableList.filter` always returns a new object and `createSelector` memoizes based on object identity, the selector returned by `makeGetStatus` would *always* execute. To avoid that, we wrap `getFilters` into a new memoizer that memoizes based on deep equality, thus returning the same object as long as the filters haven't changed, allowing the memoization of `makeGetStatus` to work. Furthermore, we memoize the compiled regexs instead of recomputing them each time the selector is called. * Fix memoized result being cleared too often * Make notifications use memoized getFiltersRegex
1 parent bca5ba0 commit 537938e

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

app/javascript/mastodon/actions/notifications.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { saveSettings } from './settings';
1111
import { defineMessages } from 'react-intl';
1212
import { List as ImmutableList } from 'immutable';
1313
import { unescapeHTML } from '../utils/html';
14-
import { getFilters, regexFromFilters } from '../selectors';
14+
import { getFiltersRegex } from '../selectors';
1515

1616
export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
1717
export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
@@ -43,13 +43,13 @@ export function updateNotifications(notification, intlMessages, intlLocale) {
4343
const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
4444
const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
4545
const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
46-
const filters = getFilters(getState(), { contextType: 'notifications' });
46+
const filters = getFiltersRegex(getState(), { contextType: 'notifications' });
4747

4848
let filtered = false;
4949

5050
if (notification.type === 'mention') {
51-
const dropRegex = regexFromFilters(filters.filter(filter => filter.get('irreversible')));
52-
const regex = regexFromFilters(filters);
51+
const dropRegex = filters[0];
52+
const regex = filters[1];
5353
const searchIndex = notification.status.spoiler_text + '\n' + unescapeHTML(notification.status.content);
5454

5555
if (dropRegex && dropRegex.test(searchIndex)) {

app/javascript/mastodon/selectors/index.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSelector } from 'reselect';
2-
import { List as ImmutableList } from 'immutable';
2+
import { List as ImmutableList, is } from 'immutable';
33
import { me } from '../initial_state';
44

55
const getAccountBase = (state, id) => state.getIn(['accounts', id], null);
@@ -36,12 +36,10 @@ const toServerSideType = columnType => {
3636
}
3737
};
3838

39-
export const getFilters = (state, { contextType }) => state.get('filters', ImmutableList()).filter(filter => contextType && filter.get('context').includes(toServerSideType(contextType)) && (filter.get('expires_at') === null || Date.parse(filter.get('expires_at')) > (new Date())));
40-
4139
const escapeRegExp = string =>
4240
string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
4341

44-
export const regexFromFilters = filters => {
42+
const regexFromFilters = filters => {
4543
if (filters.size === 0) {
4644
return null;
4745
}
@@ -63,17 +61,38 @@ export const regexFromFilters = filters => {
6361
}).join('|'), 'i');
6462
};
6563

64+
// Memoize the filter regexps for each valid server contextType
65+
const makeGetFiltersRegex = () => {
66+
let memo = {};
67+
68+
return (state, { contextType }) => {
69+
if (!contextType) return ImmutableList();
70+
71+
const serverSideType = toServerSideType(contextType);
72+
const filters = state.get('filters', ImmutableList()).filter(filter => filter.get('context').includes(serverSideType) && (filter.get('expires_at') === null || Date.parse(filter.get('expires_at')) > (new Date())));
73+
74+
if (!memo[serverSideType] || !is(memo[serverSideType].filters, filters)) {
75+
const dropRegex = regexFromFilters(filters.filter(filter => filter.get('irreversible')));
76+
const regex = regexFromFilters(filters);
77+
memo[serverSideType] = { filters: filters, results: [dropRegex, regex] };
78+
}
79+
return memo[serverSideType].results;
80+
};
81+
};
82+
83+
export const getFiltersRegex = makeGetFiltersRegex();
84+
6685
export const makeGetStatus = () => {
6786
return createSelector(
6887
[
6988
(state, { id }) => state.getIn(['statuses', id]),
7089
(state, { id }) => state.getIn(['statuses', state.getIn(['statuses', id, 'reblog'])]),
7190
(state, { id }) => state.getIn(['accounts', state.getIn(['statuses', id, 'account'])]),
7291
(state, { id }) => state.getIn(['accounts', state.getIn(['statuses', state.getIn(['statuses', id, 'reblog']), 'account'])]),
73-
getFilters,
92+
getFiltersRegex,
7493
],
7594

76-
(statusBase, statusReblog, accountBase, accountReblog, filters) => {
95+
(statusBase, statusReblog, accountBase, accountReblog, filtersRegex) => {
7796
if (!statusBase) {
7897
return null;
7998
}
@@ -84,12 +103,12 @@ export const makeGetStatus = () => {
84103
statusReblog = null;
85104
}
86105

87-
const dropRegex = (accountReblog || accountBase).get('id') !== me && regexFromFilters(filters.filter(filter => filter.get('irreversible')));
106+
const dropRegex = (accountReblog || accountBase).get('id') !== me && filtersRegex[0];
88107
if (dropRegex && dropRegex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'))) {
89108
return null;
90109
}
91110

92-
const regex = (accountReblog || accountBase).get('id') !== me && regexFromFilters(filters);
111+
const regex = (accountReblog || accountBase).get('id') !== me && filtersRegex[1];
93112
const filtered = regex && regex.test(statusBase.get('reblog') ? statusReblog.get('search_index') : statusBase.get('search_index'));
94113

95114
return statusBase.withMutations(map => {

0 commit comments

Comments
 (0)