hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.3k stars 4.83k forks source link

Improvement toc helper performance #4331

Closed jiangtj closed 4 years ago

jiangtj commented 4 years ago

Check List

Please check followings before submitting a new feature request.

Feature Request

At present, the way we get toc is to parse the entire article content, this operation is no problem. But if it work with markdown or other renderers together, there will be redundant parsing operations. During the markdown parse, the toc has already been parsed. If we cache them, this will bring better performance

Others

This is an example https://github.com/hexojs/hexo/compare/master...jiangtj:toc https://github.com/hexojs/hexo-renderer-marked/compare/master...jiangtj-lab:toc

Result image

SukkaW commented 4 years ago

FYI, due to Hexo rendering process (which I still have little idea about it), the helper will be executed twice.

jiangtj commented 4 years ago

the helper will be executed twice.

Helper execution times are related to the number of rendering times in the template engine, I think if the location of the toc call is reasonable, it will only be executed once.

I mentioned this issue because if markown renders can cache the toc, then the toc helper only needs to get the cache instead of parsing the article.

However, this will also make hexo more replicated, and the coupling with the plugin increases

So I don’t know if it should be done this way

SukkaW commented 4 years ago

@jiangtj

We might need a prop or sorts of things to make plugins contact with each other.

For example, if renderer could store ASTs in a global object. Then toc helper could first read it from the object, then decide if the parse is necessary.

SukkaW commented 4 years ago

The idea was originally made in https://github.com/hexojs/hexo/pull/4070, which is now discarded.

jiangtj commented 4 years ago

@SukkaW I tested it, the performance did not seem to improve much, but made the code more complicated. Closed.