Skip to content

Node.js based forum software built for the modern web

perf: ~18x speedup of /world route

Julian Lamf6f01ba34550
diff --git a/src/controllers/activitypub/topics.js b/src/controllers/activitypub/topics.js
index 69c682d2cb..2ea0132d0c 100644
--- a/src/controllers/activitypub/topics.js
+++ b/src/controllers/activitypub/topics.js
@@ -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);
diff --git a/src/upgrades/4.0.0/prune_user_inboxes.js b/src/upgrades/4.0.0/prune_user_inboxes.js
new file mode 100644
index 0000000000..7ea27d322e
--- /dev/null
+++ b/src/upgrades/4.0.0/prune_user_inboxes.js
@@ -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 });
+ },
+};