X Tutup
Skip to content

perf: memorize encoded summary article#243

Merged
azu merged 3 commits intohonkit:masterfrom
Yilun-Sun:yilun/perf-summary-article-encoding
Jan 17, 2022
Merged

perf: memorize encoded summary article#243
azu merged 3 commits intohonkit:masterfrom
Yilun-Sun:yilun/perf-summary-article-encoding

Conversation

@Yilun-Sun
Copy link
Contributor

  • memorize encoded summary article

#241 (comment)

const articleMapByLevel = new Map();

function encodeSummaryArticle(article: SummaryArticle, recursive?: boolean) {
const articleLevel = article.getLevel();
Copy link
Member

@azu azu Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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===.
  • encodeSummary and encodeSummaryWithoutCache should return equal object.
    • assert.deepStrictEqual(encodeSummary(article), encodeSummaryWithoutCache(article))

refs: SummaryArticle.create will be used for this test.

https://github.com/honkit/honkit/blob/4a439e5aff23b0722b93ab30186f2a30872a12e4/packages/honkit/src/models/__tests__/summaryArticle.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Thanks for the suggestions, I am going to work on that!

@azu azu mentioned this pull request Dec 29, 2021
@Yilun-Sun
Copy link
Contributor Author

Yilun-Sun commented Dec 31, 2021

@azu I am not sure where should I use the encodeSummaryArticleWithCache for. So I only use in encodeSummaryArticle.ts.
And for the articleLevel. It is unique in all articles.
See here: https://github.com/honkit/honkit/blob/master/packages/honkit/src/models/summaryArticle.ts#:~:text=const%20articles%20%3D%20(def,%7D)%3B
So maybe we could use article level for the key of the map?

@azu
Copy link
Member

azu commented Jan 2, 2022

And for the articleLevel. It is unique in all articles.

Ah, Ok.

{parentLevel}.{childIndex}

  • 1.1
  • 2.1
    • 2.1.1
    • 2.1.2
      • 2.1.2.1
      • 2.1.2.2

Yor're right, articleLevel seems be unique.

Copy link
Member

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

image

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +17 to +34
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,
};
Copy link
Member

@azu azu Jan 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@azu
Copy link
Member

azu commented Jan 2, 2022

  • My commented approach: cache root article includes articles(children)
  • Current PR approach: cache each article wihtout root article

Another approach: always cache all article. (It always use encodeSummaryArticleWithCache and remove encodeSummaryArticle function.)

Copy link
Member

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +17 to +34
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,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@azu azu merged commit 6b2292d into honkit:master Jan 17, 2022
@azu
Copy link
Member

azu commented Jan 17, 2022

Thanks for PR!

This was referenced Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup