Skip to content

Commit

Permalink
perf: run backtick code block in worker_threads
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jun 25, 2020
1 parent 7ff1a70 commit 999169a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
19 changes: 15 additions & 4 deletions lib/plugins/filter/before_generate/render_post.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
'use strict';

const Promise = require('bluebird');
const { WorkerPool } = require('hexo-util');
const { join, dirname } = require('path');
let pool;

function renderPostFilter(data) {
pool = new WorkerPool(join(dirname(dirname(dirname(__dirname))), 'workers', 'backtick_codeblock_worker.js'));

const renderPosts = model => {
const posts = model.toArray().filter(post => post.content == null);

return Promise.map(posts, post => {
post.content = post._content;
post.site = {data};
return Promise.resolve(post._content).then(_content => {
return pool.run({ input: _content, siteCfg: this.config });
}).then(content => {
post.content = content;
post.site = { data };

return this.post.render(post.full_source, post).then(() => post.save());
return this.post.render(post.full_source, post).then(() => post.save());
});
});
};

return Promise.all([
renderPosts(this.model('Post')),
renderPosts(this.model('Page'))
]);
]).then(() => {
pool.destroy();
});
}

module.exports = renderPostFilter;
2 changes: 1 addition & 1 deletion lib/plugins/filter/before_post_render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module.exports = ctx => {
const { filter } = ctx.extend;

filter.register('before_post_render', require('./backtick_code_block'));
// filter.register('before_post_render', require('./backtick_code_block'));
filter.register('before_post_render', require('./titlecase'));
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ const rLangCaption = /([^\s]+)\s*(.+)?/;

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

function backtickCodeBlock(data) {
const dataContent = data.content;
function backtickCodeBlock({ input, siteCfg }) {
if (!input.includes('```') && !input.includes('~~~')) return input;

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

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

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

// neither highlight or prismjs is enabled, return escaped content directly.
Expand Down
14 changes: 14 additions & 0 deletions lib/workers/backtick_codeblock_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const backtickCodeBlock = require('./backtick_codeblock');

// eslint-disable-next-line node/no-unsupported-features/node-builtins
const { isMainThread, parentPort } = require('worker_threads');

if (isMainThread) throw new Error('It is not a worker, it is now at Main Thread.');

parentPort.on('message', ({ input, siteCfg }) => {
const result = backtickCodeBlock(input, siteCfg);

parentPort.postMessage(result);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"hexo-i18n": "^1.0.0",
"hexo-log": "^1.0.0",
"hexo-renderer-nunjucks": "^2.0.0",
"hexo-util": "^2.0.0",
"hexo-util": "github:sukkaw/hexo-util#worker-pool",
"js-yaml": "^3.12.0",
"micromatch": "^4.0.2",
"moment": "^2.22.2",
Expand Down
2 changes: 1 addition & 1 deletion test/scripts/filters/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

describe('Filters', () => {
require('./backtick_code_block');
// require('./backtick_code_block');
require('./excerpt');
require('./external_link');
require('./i18n_locals');
Expand Down

0 comments on commit 999169a

Please sign in to comment.