perf: ~18x speedup of /world route

- upgrade script to remove pruned tids that did not get removed from inbox
- switch from db intersect to manual intersection of subset.
This commit is contained in:
Julian Lam
2024-07-12 14:38:46 -04:00
parent c01f154937
commit f6f01ba345
2 changed files with 35 additions and 8 deletions

View File

@@ -46,13 +46,11 @@ controller.list = async function (req, res) {
]);
const sort = validSorts.includes(req.query.sort) ? req.query.sort : userSettings.categoryTopicSort;
const sets = [sortToSet[sort], `uid:${req.uid}:inbox`];
const tids = await db.getSortedSetRevIntersect({
sets,
start,
stop,
weights: sets.map((s, index) => (index ? 0 : 1)),
});
let tids = await db.getSortedSetRevRange(sortToSet[sort], 0, 499);
const isMembers = await db.isSortedSetMembers(`uid:${req.uid}:inbox`, tids);
tids = tids.filter((tid, idx) => isMembers[idx]);
const count = tids.length;
tids = tids.slice(start, stop);
const targetUid = await user.getUidByUserslug(req.query.author);
@@ -71,7 +69,7 @@ controller.list = async function (req, res) {
data.name = '[[activitypub:world.name]]';
delete data.children;
data.topicCount = await db.sortedSetIntersectCard(sets);
data.topicCount = count;
data.topics = await topics.getTopicsByTids(tids, { uid: req.uid });
topics.calculateTopicIndices(data.topics, start);

View File

@@ -0,0 +1,29 @@
'use strict';
const db = require('../../database');
const batch = require('../../batch');
const topics = require('../../topics');
module.exports = {
name: 'Prune deleted topics out of user inboxes',
timestamp: Date.UTC(2024, 6, 12),
method: async function () {
const { progress } = this;
await batch.processSortedSet('users:joindate', async (uids) => {
const exists = await db.exists(uids.map(uid => `uid:${uid}:inbox`));
const count = uids.length;
uids = uids.filter((uid, idx) => exists[idx]);
await Promise.all(uids.map(async (uid) => {
const key = `uid:${uid}:inbox`;
const tids = await db.getSortedSetMembers(key);
const exists = await topics.exists(tids);
const toRemove = tids.filter((tid, idx) => !exists[idx]);
await db.sortedSetRemove(key, toRemove);
}));
progress.incr(count);
}, { progress });
},
};