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
Merged

Conversation

@SukkaW
Copy link
Member

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 Dec 18, 2019
@coveralls

This comment has been minimized.

Copy link

coveralls commented Dec 18, 2019

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 left a comment

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

@SukkaW

This comment has been minimized.

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
1 of 2 tasks complete
@curbengh

This comment has been minimized.

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
4 checks passed
4 checks passed
Travis CI - Pull Request Build Passed
Details
codeclimate All good!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
coverage/coveralls Coverage remained the same at 97.117%
Details
@SukkaW SukkaW mentioned this pull request Dec 21, 2019
1 of 1 task complete
oncletom added a commit to oncletom/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
Projects
None yet
3 participants
You can’t perform that action at this time.