-
-
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
perf(processor/post): improve processing speed when config.post_asset_folder
is enabled
#5473
Conversation
Pull Request Test Coverage Report for Build 8753005942Details
💛 - Coveralls |
config.post_asset_folder
is enabledconfig.post_asset_folder
is enabled
…t_folder` is enabled
131d295
to
9385c81
Compare
2790445
to
44a14de
Compare
@@ -268,29 +269,41 @@ function processAsset(ctx: Hexo, file: _File) { | |||
const PostAsset = ctx.model('PostAsset'); | |||
const Post = ctx.model('Post'); | |||
const id = file.source.substring(ctx.base_dir.length); | |||
const doc = PostAsset.findById(id); | |||
const postAsset = PostAsset.findById(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed unclear naming.
} | ||
|
||
if (doc) { | ||
return doc.remove(); | ||
// NOTE: Probably, unreachable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, will not be reached here unless the user intentionally changes the values in db.json
. But, I'm not sure about this, so I haven't removed it.
config.post_asset_folder
is enabledconfig.post_asset_folder
is enabled
@hexojs/core |
…t_folder` is enabled (hexojs#5473)
…t_folder` is enabled (hexojs#5473)
I think other issues also related this PR
When slow?
config.post_asset_folder
is enabled.Why?
When
config.post_asset_folder
is enabled, Hexo calls thePost.toArray()
function for each file. The time complexity ofPost.toArray()
isO(n)
. Additionally, Hexo uses thePost.find
method to search for the post, and the average time complexity offind
is alsoO(n)
.So, if there are 100 articles and 100 files, the time complexity would be
O(100 * 100 * 100) = O(n^3)
.How to solve?
Avoid calling
Post.toArray()
. Instead, we will search based on whether the directory where the files are stored (assetDir
) matches. Also, if the file found withPostAsset
, usePost.findById
. The time complexity ofPost.findById
isO(1)
.Benchmark Environment
Below is bench env.
Machine
Hexo and Node.js
package.json
_config.yml
Theme
I'm using a theme that I've delete many features from https://github.com/LouisBarranqueiro/hexo-theme-tranquilpeak.
Benchmark Time (Before, After)
Processed data
Time
Command & Details
Cold Processing (run hexo clean before generate)
Hot Processing (run hexo clean before generate)
After
Cold Processing (run hexo clean before generate)
Hot Processing (run hexo clean before generate)
Gramegraph (Hot Processing only)
Before (Hot Processing)
After (Hot Processing)