Skip to content

Commit f9f6265

Browse files
committed
perf: run backtick code block in worker_threads
1 parent 7ff1a70 commit f9f6265

File tree

6 files changed

+37
-13
lines changed

6 files changed

+37
-13
lines changed
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
'use strict';
22

33
const Promise = require('bluebird');
4+
const { WorkerPool } = require('hexo-util');
5+
const { join, dirname } = require('path');
6+
let pool;
47

58
function renderPostFilter(data) {
9+
pool = new WorkerPool(join(dirname(dirname(dirname(__dirname))), 'workers', 'backtick_codeblock_worker.js'));
10+
611
const renderPosts = model => {
712
const posts = model.toArray().filter(post => post.content == null);
813

914
return Promise.map(posts, post => {
10-
post.content = post._content;
11-
post.site = {data};
15+
return Promise.resolve(post._content).then(_content => {
16+
return pool.run({ input: _content, siteCfg: this.config });
17+
}).then(content => {
18+
post.content = content;
19+
post.site = { data };
1220

13-
return this.post.render(post.full_source, post).then(() => post.save());
21+
return this.post.render(post.full_source, post).then(() => post.save());
22+
});
1423
});
1524
};
1625

1726
return Promise.all([
1827
renderPosts(this.model('Post')),
1928
renderPosts(this.model('Page'))
20-
]);
29+
]).then(() => {
30+
pool.destroy();
31+
});
2132
}
2233

2334
module.exports = renderPostFilter;

lib/plugins/filter/before_post_render/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
module.exports = ctx => {
44
const { filter } = ctx.extend;
55

6-
filter.register('before_post_render', require('./backtick_code_block'));
6+
// filter.register('before_post_render', require('./backtick_code_block'));
77
filter.register('before_post_render', require('./titlecase'));
88
};

lib/plugins/filter/before_post_render/backtick_code_block.js renamed to lib/workers/backtick_codeblock.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ const rLangCaption = /([^\s]+)\s*(.+)?/;
88

99
const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}');
1010

11-
function backtickCodeBlock(data) {
12-
const dataContent = data.content;
11+
function backtickCodeBlock(input, siteCfg) {
12+
if (!input.includes('```') && !input.includes('~~~')) return input;
1313

14-
if (!dataContent.includes('```') && !dataContent.includes('~~~')) return;
14+
const hljsCfg = siteCfg.highlight || {};
15+
const prismCfg = siteCfg.prismjs || {};
1516

16-
const hljsCfg = this.config.highlight || {};
17-
const prismCfg = this.config.prismjs || {};
17+
return input.replace(rBacktick, ($0, start, $2, _args, _content, end) => {
1818

19-
data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => {
2019
let content = _content.replace(/\n$/, '');
2120

2221
// neither highlight or prismjs is enabled, return escaped content directly.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const backtickCodeBlock = require('./backtick_codeblock');
4+
5+
// eslint-disable-next-line node/no-unsupported-features/node-builtins
6+
const { isMainThread, parentPort } = require('worker_threads');
7+
8+
if (isMainThread) throw new Error('It is not a worker, it is now at Main Thread.');
9+
10+
parentPort.on('message', ({ input, siteCfg }) => {
11+
const result = backtickCodeBlock(input, siteCfg);
12+
13+
parentPort.postMessage(result);
14+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"hexo-i18n": "^1.0.0",
5050
"hexo-log": "^1.0.0",
5151
"hexo-renderer-nunjucks": "^2.0.0",
52-
"hexo-util": "^2.0.0",
52+
"hexo-util": "github:sukkaw/hexo-util#worker-pool",
5353
"js-yaml": "^3.12.0",
5454
"micromatch": "^4.0.2",
5555
"moment": "^2.22.2",

test/scripts/filters/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
describe('Filters', () => {
4-
require('./backtick_code_block');
4+
// require('./backtick_code_block');
55
require('./excerpt');
66
require('./external_link');
77
require('./i18n_locals');

0 commit comments

Comments
 (0)