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

Cache list account results #111

Merged
merged 3 commits into from
Nov 30, 2023
Merged

Cache list account results #111

merged 3 commits into from
Nov 30, 2023

Conversation

koresar
Copy link
Collaborator

@koresar koresar commented Nov 29, 2023

Hello

We've noticed that these calls are taking too much time. Need to cache.

I'm looking for a quick turnaround for this PR.

@koresar koresar requested a review from Uzlopak November 29, 2023 09:16
@Uzlopak Uzlopak changed the title Ceche list account results Cache list account results Nov 29, 2023
src/Book.ts Outdated
Comment on lines 36 to 49
function fromDistinctToAccounts(distinctResult: string[]) {
const accountsSet: Set<string> = new Set();
for (const result of distinctResult) {
const prev: string[] = [];
const paths: string[] = result.split(":");
for (const acct of paths) {
prev.push(acct);
accountsSet.add(prev.join(":"));
}
}

return Array.from(accountsSet);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

fromDistinctToAccounts seems a little bit computational heavy.
Would like to see a benchmark for this function.

can we reduce the pressure, by reusing prev bytaking it out of the for loop and doing a prev.length = 0?

Also the join call is probably also expensive.

It makes more sense to it like this:

Suggested change
function fromDistinctToAccounts(distinctResult: string[]) {
const accountsSet: Set<string> = new Set();
for (const result of distinctResult) {
const prev: string[] = [];
const paths: string[] = result.split(":");
for (const acct of paths) {
prev.push(acct);
accountsSet.add(prev.join(":"));
}
}
return Array.from(accountsSet);
}
function fromDistinctToAccounts(distinctResult: string[]) {
const accountsSet: Set<string> = new Set();
let i = 0;
for (const result of distinctResult) {
const paths: string[] = result.split(":");
for (i = 0; i < paths.length; ++i) {
switch (i) {
case 0:
accountsSet.add(paths[0]);
break;
case 1:
accountsSet.add(`${paths[0]}:${paths[1]});
break;
case 2:
accountsSet.add(`${paths[0]}:${paths[1]:${paths[2]});
break;
case 3:
accountsSet.add(`${paths[0]}:${paths[1]:${paths[2]:${paths[3]});
break;
default:
// is it till i or does it need to be i -1 or i+1?
accountsSet.add(paths.slice(0, i).join(':'));
}
}
return Array.from(accountsSet);
}

untested. But it should reduce some pressure because we avoid alot of intermediary arrays.

Copy link
Collaborator Author

@koresar koresar Nov 30, 2023

Choose a reason for hiding this comment

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

Suggestion listened. However, I'd implemented even simpler and faster solution:

function fromDistinctToAccounts(distinctResult: string[]) {
  const accountsSet: Set<string> = new Set();
  for (const fullAccountName of distinctResult) {
    const paths = fullAccountName.split(":");
    let path = paths[0];
    accountsSet.add(path);
    for (let i = 1; i < paths.length; ++i) {
      path += ":" + paths[i];
      accountsSet.add(path);
    }
  }
  return Array.from(accountsSet);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, this function will be called very occasionally. There is no point optimising it.

src/Book.ts Outdated
});
} else {
// There is a snapshot already. But let's check if it's too old.
const tooOld = Date.now() > listAccountsSnapshot.createdAt.getTime() + this.balanceSnapshotSec * 1000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe better name. isSnapshotObsolete ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

great suggestion!

src/Book.ts Outdated
session: options.session,
})
.then((results) => {
return snapshotListAccounts({
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm. why is the return here? iIs it here because then the .catch() can handle it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, correct.
I'll remove the return keyword and use the implicit return of arrow functions.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Nov 29, 2023

@koresar

I have reviewed it in regards of potential performance improvements. But it is quiete complicated piece of code. I can not say, that I fully understood it in terms of reliability or avoiding race conditions, which could result in showing wrong account balances or so.

Do you have some lines which you consider being critically checked.

@koresar
Copy link
Collaborator Author

koresar commented Nov 30, 2023

This is not about balances, but about returning the list of all accounts this book has. So, balances won't be affected or touched.

The logic for listAccounts() was copied from the balance() function. The appropriate unit tests were also copied from the balance() method unit tests.

Anyhow, this is a very safe PR for existing users. But addresses a major pain point we have atm.

@koresar koresar merged commit fc5900c into master Nov 30, 2023
31 checks passed
@koresar koresar deleted the ceche-listAccount-results branch November 30, 2023 02:31
@koresar
Copy link
Collaborator Author

koresar commented Nov 30, 2023

Published as v6.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants