Skip to content
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

refactor(fragment_cache): reuse result #3985

Merged
merged 1 commit into from
Dec 18, 2019

Conversation

SukkaW
Copy link
Member

@SukkaW SukkaW commented Dec 18, 2019

What does it do?

Fix a potential performance issue introduced by #3744: After #3744, the function passed to fragment_cache might be called twice instead of once.

A PoC can be viewed here: https://runkit.com/sukkaw/5df9e053e7c3d50019810e89

const cache = {};
let called1 = 0;
let called2 = 0;

// This PR
const cacheFunc1 = (k, fn) => {
  if (cache[k] != null) return cache[k];
  
  const result = fn();
  cache[k] = result;
  
  return result;
}

// PR #3744
const cacheFunc2 = (k, fn) => {
  if (cache[k] != null) return cache[k];

  cache[k] = fn();
  const result = fn();
  return result;
}

cacheFunc1('1', () => { return called1++; });
cacheFunc1('1', () => { return called1++; });
cacheFunc1('1', () => { return called1++; });
cacheFunc2('2', () => { return called2++; });
cacheFunc2('2', () => { return called2++; });
cacheFunc2('2', () => { return called2++; });
console.log(called1, called2);
// 1 2

How to test

git clone -b fragment_cache_resue https://github.com/sukkaw/hexo.git
cd hexo
npm install
npm test

Screenshots

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@SukkaW SukkaW requested a review from curbengh December 18, 2019 08:27
@coveralls
Copy link

Coverage Status

Coverage remained the same at 97.117% when pulling 3a761d7 on SukkaW:fragment_cache_reuse into 5179a6a on hexojs:master.

Copy link
Contributor

@curbengh curbengh left a comment

Choose a reason for hiding this comment

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

In light of this, I'm re-visiting #3744.

@SukkaW
Copy link
Member Author

SukkaW commented Dec 18, 2019

@curbengh I have found some similar cases introduced in #3744 as well. Should I include them in this PR?

@curbengh curbengh mentioned this pull request Dec 18, 2019
2 tasks
@curbengh
Copy link
Contributor

curbengh commented Dec 18, 2019

I just finished working on other cases introduced by #3744, in #3986

@SukkaW SukkaW merged commit 9322cb6 into hexojs:master Dec 18, 2019
@SukkaW SukkaW mentioned this pull request Dec 21, 2019
1 task
thom4parisot pushed a commit to thom4parisot/hexo that referenced this pull request Jan 17, 2020
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.

3 participants