perf: memorize encoded summary article#243
Conversation
| const articleMapByLevel = new Map(); | ||
|
|
||
| function encodeSummaryArticle(article: SummaryArticle, recursive?: boolean) { | ||
| const articleLevel = article.getLevel(); |
There was a problem hiding this comment.
Is articleLevelunique in all articles?
Map allow you to use object as key.
articleCacheMap.set(article, encodedArticle); is better.
(lru cache is better for this usecase. to avoid memory leak)
It is better that separate memorize function like memorizeEncodeSummaryArticle.
The memorizeEncodeSummaryArticle(cached) will wrap the encodeSummaryArticle(non cache).
Or, encodeSummaryArticle(cached) and encodeSummaryArticleWithoutCache(non cache).
(It means that we can avoid to rename refererences)
If you can, please add some tests.
encodeSummaryWithoutCache(non cached) should return an object.encodeSummary(cached) should return an object.encodeSummary(cached) should return different object when pass difference SummaryArticle.encodeSummary(cached) should return same object when pass same SummaryArticle.assert.strictEqual(encodeSummary(article), encodeSummary(article)). it is just===.
encodeSummaryandencodeSummaryWithoutCacheshould return equal object.assert.deepStrictEqual(encodeSummary(article), encodeSummaryWithoutCache(article))
refs: SummaryArticle.create will be used for this test.
There was a problem hiding this comment.
Got it! Thanks for the suggestions, I am going to work on that!
|
@azu I am not sure where should I use the encodeSummaryArticleWithCache for. So I only use in |
Ah, Ok.
Yor're right, |
There was a problem hiding this comment.
I am not sure where should I use the encodeSummaryArticleWithCache for. So I only use in encodeSummaryArticle.ts.
"Go to references" on encodeSummaryArticle.
You can see the references of encodeSummaryArticle function.
or search encodeSummaryArticle (but it is not complete because default export can be renamed)
https://github.com/honkit/honkit/search?q=encodeSummaryArticle
| articles = article | ||
| .getArticles() | ||
| .map((article) => encodeSummaryArticle(article)) | ||
| .map((article) => encodeSummaryArticleWithCache(article)) |
There was a problem hiding this comment.
encodeSummaryArticle should not depend on encodeSummaryArticleWithCache.
This dependency prevent correct testing.
(We can not test encodeSummaryArticle(without cacehe) correctly).
Instead of it, encodeSummaryArticleWithCache depended on encodeSummaryArticle.
| let articles = undefined; | ||
| if (recursive !== false) { | ||
| articles = article | ||
| .getArticles() | ||
| .map((article) => encodeSummaryArticleWithCache(article)) | ||
| .toJS(); | ||
| } | ||
|
|
||
| const encodedArticle = { | ||
| title: article.getTitle(), | ||
| level: article.getLevel(), | ||
| depth: article.getDepth(), | ||
| anchor: article.getAnchor(), | ||
| url: article.getUrl(), | ||
| path: article.getPath(), | ||
| ref: article.getRef(), | ||
| articles: articles, | ||
| }; |
There was a problem hiding this comment.
Remove this lines and just use encodeSummaryArticle.
const encodedArticle = encodeSummaryArticle(article, recursive);
articleCacheMap.set(article, encodedArticle);As as result, we can create cached version of encodeSummaryArticle.
encoding article one time.
(encodeSummaryArticleWithCache depended on encodeSummaryArticle.)
There was a problem hiding this comment.
@azu Sorry I don't understand. If we replace all current encodeSummaryArticle with encodeSummaryArticleWithCache and call encodeSummaryArticle inside encodeSummaryArticleWithCache. How could we cache articles? The first call of encodeSummaryArticleWithCache will go direct into encodeSummaryArticle and parse all articles recursively and only store the root one. So one article will still be encoded may times.
Or maybe I got lost, could you please tell me more about your thoughts? Thanks!
There was a problem hiding this comment.
Ah, You're correct.
I think It is a bit strange depdency, but Let's move on.
https://github.com/honkit/honkit/pull/243/files#r777172419
Another approach: always cache all article. (It always use |
| let articles = undefined; | ||
| if (recursive !== false) { | ||
| articles = article | ||
| .getArticles() | ||
| .map((article) => encodeSummaryArticleWithCache(article)) | ||
| .toJS(); | ||
| } | ||
|
|
||
| const encodedArticle = { | ||
| title: article.getTitle(), | ||
| level: article.getLevel(), | ||
| depth: article.getDepth(), | ||
| anchor: article.getAnchor(), | ||
| url: article.getUrl(), | ||
| path: article.getPath(), | ||
| ref: article.getRef(), | ||
| articles: articles, | ||
| }; |
There was a problem hiding this comment.
Ah, You're correct.
I think It is a bit strange depdency, but Let's move on.
https://github.com/honkit/honkit/pull/243/files#r777172419
|
Thanks for PR! |

#241 (comment)