-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The proposal of using Multi Threads #4355
Comments
cc @hexojs/core @curbengh @stevenjoezhang @jiangtj @segayuu @yoshinorin @JLHwung Should we update minimum required Node.js version to 12? Although Hexo 5.0.0 might not require such a high Node.js version, but we could bring up more features during Hexo 5.x development. |
I'm ok with bumping to Node 12, as long as only filters are affected to minimize the delay 5.0.0. Perhaps only change 1-2 filters for now, then other filters can be updated during 5.x. |
@curbengh We could even release 5.0.0 first, then add multi core support from 5.1.0. |
It would better to have at least one filter that utilize this API to justify bumping to Node 12 (and demonstrate the benefit of that bump) in 5.0.0. |
We could start with Take a look at the flamegraph: https://29e28e2d8f6f8fdb247ad2c47788857d003fd894-12-hexo.surge.sh/flamegraph.html It seems to be a long task. |
This is nice. I have a very good experience with piscina. it's a nice wrapper (and more) around |
@tuananh LGTM! It seems definitely better than my WorkerPool: hexojs/hexo-util#212 |
I gave it a try to optimize haven't gotten around fixing it yet. Not sure if it has anything to do with the way hexo calls all the filter return Promise.each(filters, filter => Reflect.apply(Promise.method(filter), ctx, args).then(result => {
args[0] = result == null ? args[0] : result;
return args[0];
})).then(() => args[0]); |
@tuananh The entire |
Here's what we can learn #4368 According to the documents of the worker_threads:
Which means:
|
@SukkaW that's probably it. in order to change that, we need to change the way we pass |
Instead of
The only problem is |
From the perspective of 2024, the support for multithreading in Node.js has not improved. The rendering process of posts heavily relies on Hexo's |
So this basically leaves us with 2 options:
|
option 2 sounds better to me |
Since #550, the original creator of Hexo, @tommy351 want to speed up Hexo with multi core rendering. However, the #550 is never continued due to the difficulties of managing multiple Hexo instance.
Recently I have brought up Node.js
worker_threads
for a project (OI-wiki/OI-wiki#2288) and learned something aboutworker_threads
. With Node.js add support forworker_threads
, it is now possible to bring up multi core rendering for Hexo again.Limit
Workers Thread is designed to run CPU intensive tasks with simple algorism:
Thus we cannot run many difficult functions inside workers.
Design
As creating workers and destroy workers is still expensive (worker_threads are required to contact with main_thread), we should only create limited number of
worker_threads
(In OI-wiki/OI-wiki#2288 I use the length of CPU Threads). Thus, aWorkerPool
util should be made.The
WorkPool
is designed to queue the task, manage task and make sure next task would run in an idle worker, thus it should have those method:init()
: Init a worker pool with the queue (the queue could be an array). This will be called inconstructor
.run(input)
: add a task to the queue, withinput
passed to the workers. APromise
will be returned (the result could be retrieved byconst output = await workerPool.run(input)
).destroy()
: after all tasks is finished, destroy all the worker_threads created.And here is an example about how to use
WorkPool
:As you can see, the example I given is suitable for some of filters (likes
meta_generator
,backtick_code_filter
) that we pass input to the filter and get output from it. But for more complicated job (like post rendering & template rendering) workers_thread still can't help.cc @hexojs/core @tommy351
The text was updated successfully, but these errors were encountered: