Skip to content

Node.js based forum software built for the modern web

late night optimizations

barisusaklic3a9767bf6f5
diff --git a/src/database/mongo/sets.js b/src/database/mongo/sets.js
index 73b4e6428b..e8d4f1a1f0 100644
--- a/src/database/mongo/sets.js
+++ b/src/database/mongo/sets.js
@@ -78,7 +78,7 @@ module.exports = function(db, module) {
});
result = sets.map(function(set) {
- return result.indexOf(set) !== -1 ? 1 : 0;
+ return result.indexOf(set) !== -1;
});
callback(null, result);
diff --git a/src/database/redis/sets.js b/src/database/redis/sets.js
index 32c59a060a..10ef126b06 100644
--- a/src/database/redis/sets.js
+++ b/src/database/redis/sets.js
@@ -40,11 +40,20 @@ module.exports = function(redisClient, module) {
module.isMemberOfSets = function(sets, value, callback) {
var multi = redisClient.multi();
- for (var i = 0, ii = sets.length; i < ii; i++) {
+ for (var i = 0; i < sets.length; ++i) {
multi.sismember(sets[i], value);
}
- multi.exec(callback);
+ multi.exec(function(err, results) {
+ if (err) {
+ return callback(err);
+ }
+
+ for (var i=0; i<results.length; ++i) {
+ results[i] = results[i] === 1;
+ }
+ callback(null, results);
+ });
};
module.getSetMembers = function(key, callback) {
diff --git a/src/favourites.js b/src/favourites.js
index 622606e2fc..69a611248d 100644
--- a/src/favourites.js
+++ b/src/favourites.js
@@ -101,7 +101,7 @@ var async = require('async'),
if (meta.config['reputation:disabled'] === false) {
return callback(false);
}
-
+
toggleVote('upvote', pid, uid, callback);
};
@@ -155,8 +155,21 @@ var async = require('async'),
};
Favourites.getVoteStatusByPostIDs = function(pids, uid, callback) {
- async.map(pids, function(pid, next) {
- Favourites.hasVoted(pid, uid, next);
+ var upvoteSets = [],
+ downvoteSets = [];
+
+ for (var i=0; i<pids.length; ++i) {
+ upvoteSets.push('pid:' + pids[i] + ':upvote');
+ downvoteSets.push('pid:' + pids[i] + ':downvote');
+ }
+
+ async.parallel({
+ upvotes: function(next) {
+ db.isMemberOfSets(upvoteSets, uid, next);
+ },
+ downvotes: function(next) {
+ db.isMemberOfSets(downvoteSets, uid, next);
+ }
}, callback);
};
@@ -225,9 +238,12 @@ var async = require('async'),
};
Favourites.getFavouritesByPostIDs = function(pids, uid, callback) {
- async.map(pids, function(pid, next) {
- Favourites.hasFavourited(pid, uid, next);
- }, callback);
+ var sets = [];
+ for (var i=0; i<pids.length; ++i) {
+ sets.push('pid:' + pids[i] + ':users_favourited');
+ }
+
+ db.isMemberOfSets(sets, uid, callback);
};
Favourites.getFavouritedUidsByPids = function(pids, callback) {
diff --git a/src/groups.js b/src/groups.js
index 5bea503656..708de3cd56 100644
--- a/src/groups.js
+++ b/src/groups.js
@@ -350,7 +350,7 @@
db.isMemberOfSets(groupSets, uid, function(err, isMembers) {
for(var i=isMembers.length - 1; i>=0; --i) {
- if (parseInt(isMembers[i], 10) !== 1) {
+ if (!isMembers[i]) {
groupData.splice(i, 1);
}
}
diff --git a/src/posts.js b/src/posts.js
index 519bb5453a..9c86ce4aec 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -188,43 +188,46 @@ var async = require('async'),
});
};
- Posts.getUserInfoForPost = function(post, callback) {
- user.getUserFields(post.uid, ['username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) {
+ Posts.getUserInfoForPosts = function(uids, callback) {
+ user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned'], function(err, userData) {
if (err) {
return callback(err);
}
- var userInfo = {
- username: userData.username || '[[global:guest]]',
- userslug: userData.userslug || '',
- reputation: userData.reputation || 0,
- postcount: userData.postcount || 0,
- banned: parseInt(userData.banned, 10) === 1,
- picture: userData.picture || user.createGravatarURLFromEmail('')
- };
+ async.map(userData, function(userData, next) {
+ var userInfo = {
+ uid: userData.uid,
+ username: userData.username || '[[global:guest]]',
+ userslug: userData.userslug || '',
+ reputation: userData.reputation || 0,
+ postcount: userData.postcount || 0,
+ banned: parseInt(userData.banned, 10) === 1,
+ picture: userData.picture || user.createGravatarURLFromEmail('')
+ };
- async.parallel({
- signature: function(next) {
- if (parseInt(meta.config.disableSignatures, 10) === 1) {
- return next();
+ async.parallel({
+ signature: function(next) {
+ if (parseInt(meta.config.disableSignatures, 10) === 1) {
+ return next();
+ }
+ postTools.parseSignature(userData.signature, next);
+ },
+ customProfileInfo: function(next) {
+ plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next);
+ },
+ groups: function(next) {
+ groups.getUserGroups(userData.uid, next);
}
- postTools.parseSignature(userData.signature, next);
- },
- customProfileInfo: function(next) {
- plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: post.uid, pid: post.pid}, next);
- },
- groups: function(next) {
- groups.getUserGroups(post.uid, next);
- }
- }, function(err, results) {
- if (err) {
- return callback(err);
- }
- userInfo.signature = results.signature;
- userInfo.custom_profile_info = results.custom_profile_info;
- userInfo.groups = results.groups;
- callback(null, userInfo);
- });
+ }, function(err, results) {
+ if (err) {
+ return next(err);
+ }
+ userInfo.signature = results.signature;
+ userInfo.custom_profile_info = results.custom_profile_info;
+ userInfo.groups = results.groups;
+ next(null, userInfo);
+ });
+ }, callback);
});
};
diff --git a/src/topics.js b/src/topics.js
index 52f84f575f..9157ab8f48 100644
--- a/src/topics.js
+++ b/src/topics.js
@@ -270,6 +270,9 @@ var async = require('async'),
}
async.parallel({
+ mainPost: function(next) {
+ Topics.getMainPost(tid, uid, next);
+ },
posts: function(next) {
Topics.getTopicPosts(tid, set, start, end, uid, reverse, next);
},
@@ -284,26 +287,6 @@ var async = require('async'),
},
tags: function(next) {
Topics.getTopicTagsObjects(tid, next);
- },
- mainPost: function(next) {
- Topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
- if (err) {
- return next(err);
- }
- if (!parseInt(mainPid, 10)) {
- return next(null, []);
- }
- posts.getPostsByPids([mainPid], function(err, postData) {
- if (err) {
- return next(err);
- }
- if (!Array.isArray(postData) || !postData[0]) {
- return next(null, []);
- }
- postData[0].index = 0;
- Topics.addPostData(postData, uid, next);
- });
- });
}
}, function(err, results) {
if (err) {
@@ -325,6 +308,27 @@ var async = require('async'),
});
};
+ Topics.getMainPost = function(tid, uid, callback) {
+ Topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
+ if (err) {
+ return callback(err);
+ }
+ if (!parseInt(mainPid, 10)) {
+ return callback(null, []);
+ }
+ posts.getPostsByPids([mainPid], function(err, postData) {
+ if (err) {
+ return callback(err);
+ }
+ if (!Array.isArray(postData) || !postData[0]) {
+ return callback(null, []);
+ }
+ postData[0].index = 0;
+ Topics.addPostData(postData, uid, callback);
+ });
+ });
+ };
+
Topics.getTeasers = function(tids, callback) {
if(!Array.isArray(tids)) {
diff --git a/src/topics/create.js b/src/topics/create.js
index cbaeec4947..ea3e56933c 100644
--- a/src/topics/create.js
+++ b/src/topics/create.js
@@ -224,10 +224,10 @@ module.exports = function(Topics) {
},
function(result, next) {
Topics.pushUnreadCount();
- posts.getUserInfoForPost(postData, next);
+ posts.getUserInfoForPosts([postData.uid], next);
},
function(userInfo, next) {
- postData.user = userInfo;
+ postData.user = userInfo[0];
Topics.getTopicFields(tid, ['tid', 'title', 'slug'], next);
},
function(topicData, next) {
diff --git a/src/topics/posts.js b/src/topics/posts.js
index b787c3956c..00903373a8 100644
--- a/src/topics/posts.js
+++ b/src/topics/posts.js
@@ -42,6 +42,7 @@ module.exports = function(Topics) {
};
Topics.addPostData = function(postData, uid, callback) {
+ var st = process.hrtime();
var pids = postData.map(function(post) {
return post.pid;
});
@@ -54,27 +55,43 @@ module.exports = function(Topics) {
favourites.getVoteStatusByPostIDs(pids, uid, next);
},
userData: function(next) {
- async.each(postData, function(post, next) {
- async.parallel({
- editor: function(next) {
- if (!post.editor) {
- return next();
- }
- user.getUserFields(post.editor, ['username', 'userslug'], next);
- },
- user: function(next) {
- posts.getUserInfoForPost(post, next);
- }
- }, function(err, results) {
- if (err) {
- return next(err);
- }
-
- post.user = results.user;
- post.editor = results.editor;
- next();
+ var uids = [];
+ for(var i=0; i<postData.length; ++i) {
+ if (uids.indexOf(postData[i].uid) === -1) {
+ uids.push(postData[i].uid);
+ }
+ }
+
+ posts.getUserInfoForPosts(uids, function(err, users) {
+ if (err) {
+ return next(err);
+ }
+
+ var userData = {};
+ users.forEach(function(user) {
+ userData[user.uid] = user;
});
- }, next);
+ next(null, userData);
+ });
+ },
+ editors: function(next) {
+ var editors = [];
+ for(var i=0; i<postData.length; ++i) {
+ if (postData[i].editor && editors.indexOf(postData[i].editor) === -1) {
+ editors.push(postData[i].editor);
+ }
+ }
+
+ user.getMultipleUserFields(editors, ['uid', 'username', 'userslug'], function(err, editors) {
+ if (err) {
+ return next(err);
+ }
+ var editorData = {};
+ editors.forEach(function(editor) {
+ editorData[editor.uid] = editor;
+ })
+ next(null, editorData);
+ });
},
privileges: function(next) {
async.map(pids, function (pid, next) {
@@ -88,9 +105,11 @@ module.exports = function(Topics) {
for (var i = 0; i < postData.length; ++i) {
postData[i].deleted = parseInt(postData[i].deleted, 10) === 1;
+ postData[i].user = results.userData[postData[i].uid];
+ postData[i].editor = postData[i].editor ? results.editors[postData[i].editor] : null;
postData[i].favourited = results.favourites[i];
- postData[i].upvoted = results.voteData[i].upvoted;
- postData[i].downvoted = results.voteData[i].downvoted;
+ postData[i].upvoted = results.voteData.upvotes[i];
+ postData[i].downvoted = results.voteData.downvotes[i];
postData[i].votes = postData[i].votes || 0;
postData[i].display_moderator_tools = results.privileges[i].editable;
postData[i].display_move_tools = results.privileges[i].move && postData[i].index !== 0;
@@ -100,7 +119,7 @@ module.exports = function(Topics) {
postData[i].content = '[[topic:post_is_deleted]]';
}
}
-
+process.profile('derp', st);
callback(null, postData);
});
};
diff --git a/src/topics/unread.js b/src/topics/unread.js
index 7000dfc1a2..f7a3f8a700 100644
--- a/src/topics/unread.js
+++ b/src/topics/unread.js
@@ -45,7 +45,7 @@ module.exports = function(Topics) {
return callback(err);
}
var newtids = tids.filter(function(tid, index, self) {
- return parseInt(read[index], 10) === 0;
+ return !read[index];
});
async.filter(newtids, function(tid, next) {