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

fix: serializing transactions; sort that takes less time and memory #25642

Merged
merged 1 commit into from
May 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions web3.js/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,13 +395,16 @@ export class Transaction {

// Sort. Prioritizing first by signer, then by writable
uniqueMetas.sort(function (x, y) {
const pubkeySorting = x.pubkey
.toBase58()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One string created here.

.localeCompare(y.pubkey.toBase58());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another string created here.

const checkSigner = x.isSigner === y.isSigner ? 0 : x.isSigner ? -1 : 1;
const checkWritable =
x.isWritable === y.isWritable ? pubkeySorting : x.isWritable ? -1 : 1;
Comment on lines -401 to -403
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The CPU and memory that we consumed to produce base58 strings gets thrown out here if the signedness or writability of the accounts differ.

return checkSigner || checkWritable;
if (x.isSigner !== y.isSigner) {
// Signers always come before non-signers
return x.isSigner ? -1 : 1;
}
if (x.isWritable !== y.isWritable) {
// Writable accounts always come before read-only accounts
return x.isWritable ? -1 : 1;
}
// Otherwise, sort by pubkey.
return x.pubkey._bn.cmp(y.pubkey._bn);
});

// Move fee payer to the front
Expand Down