mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: Move mention lookups out of the V8 context. (#6640)
We were looking up each mention one by one without any form of caching and that results in a problem somewhat similar to an N+1. When we have to do alot of DB lookups, it also increased the time spent in the V8 context which may eventually lead to a timeout. The change here makes it such that mention lookups only does a single DB query per post that happens outside of the V8 context.
This commit is contained in:
@@ -39,12 +39,6 @@ module PrettyText
|
||||
username
|
||||
end
|
||||
|
||||
def mention_lookup(name)
|
||||
return false if name.blank?
|
||||
return "user" if User.exists?(username_lower: name.downcase)
|
||||
return "group" if Group.exists?(name: name)
|
||||
end
|
||||
|
||||
def category_hashtag_lookup(category_slug)
|
||||
if category = Category.query_from_hashtag_slug(category_slug)
|
||||
[category.url_with_id, category_slug]
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
__PrettyText = require('pretty-text/pretty-text').default;
|
||||
__buildOptions = require('pretty-text/pretty-text').buildOptions;
|
||||
__performEmojiUnescape = require('pretty-text/emoji').performEmojiUnescape;
|
||||
__PrettyText = require("pretty-text/pretty-text").default;
|
||||
__buildOptions = require("pretty-text/pretty-text").buildOptions;
|
||||
__performEmojiUnescape = require("pretty-text/emoji").performEmojiUnescape;
|
||||
|
||||
__utils = require('discourse/lib/utilities');
|
||||
__utils = require("discourse/lib/utilities");
|
||||
|
||||
__emojiUnicodeReplacer = null;
|
||||
|
||||
__setUnicode = function(replacements) {
|
||||
let unicodeRegexp = new RegExp(Object.keys(replacements).sort().reverse().join("|"), "g");
|
||||
let unicodeRegexp = new RegExp(
|
||||
Object.keys(replacements)
|
||||
.sort()
|
||||
.reverse()
|
||||
.join("|"),
|
||||
"g"
|
||||
);
|
||||
|
||||
__emojiUnicodeReplacer = function(text) {
|
||||
unicodeRegexp.lastIndex = 0;
|
||||
let m;
|
||||
while ((m = unicodeRegexp.exec(text)) !== null) {
|
||||
let replacement = ":" + replacements[m[0]] + ":";
|
||||
const before = text.charAt(m.index-1);
|
||||
const before = text.charAt(m.index - 1);
|
||||
if (!/\B/.test(before)) {
|
||||
replacement = "\u200b" + replacement;
|
||||
}
|
||||
@@ -23,7 +29,7 @@ __setUnicode = function(replacements) {
|
||||
|
||||
// fixes Safari VARIATION SELECTOR-16 issue with some emojis
|
||||
// https://meta.discourse.org/t/emojis-selected-on-ios-displaying-additional-rectangles/86132
|
||||
text = text.replace(/\ufe0f/g, '');
|
||||
text = text.replace(/\ufe0f/g, "");
|
||||
|
||||
return text;
|
||||
};
|
||||
@@ -35,9 +41,13 @@ function __getURLNoCDN(url) {
|
||||
if (!url) return url;
|
||||
|
||||
// if it's a non relative URL, return it.
|
||||
if (url !== '/' && !/^\/[^\/]/.test(url)) { return url; }
|
||||
if (url !== "/" && !/^\/[^\/]/.test(url)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
if (url.indexOf(__paths.baseUri) !== -1) { return url; }
|
||||
if (url.indexOf(__paths.baseUri) !== -1) {
|
||||
return url;
|
||||
}
|
||||
if (url[0] !== "/") url = "/" + url;
|
||||
|
||||
return __paths.baseUri + url;
|
||||
@@ -76,12 +86,11 @@ function __categoryLookup(c) {
|
||||
return __helpers.category_tag_hashtag_lookup(c);
|
||||
}
|
||||
|
||||
function __mentionLookup(u) {
|
||||
return __helpers.mention_lookup(u);
|
||||
}
|
||||
|
||||
function __lookupAvatar(p) {
|
||||
return __utils.avatarImg({size: "tiny", avatarTemplate: __helpers.avatar_template(p) }, __getURL);
|
||||
return __utils.avatarImg(
|
||||
{ size: "tiny", avatarTemplate: __helpers.avatar_template(p) },
|
||||
__getURL
|
||||
);
|
||||
}
|
||||
|
||||
function __formatUsername(username) {
|
||||
@@ -97,5 +106,7 @@ function __getCurrentUser(userId) {
|
||||
}
|
||||
|
||||
I18n = {
|
||||
t: function(a,b) { return __helpers.t(a,b); }
|
||||
t: function(a, b) {
|
||||
return __helpers.t(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user