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

TransactionCost - remove allocation #1987

Closed
wants to merge 3 commits into from

Conversation

apfitzge
Copy link

@apfitzge apfitzge commented Jul 3, 2024

Problem

  • TransactionCost holds an allocation to track writable accounts. This is generally not needed since we can always figure out which accounts the transaction writes since the transaction stays in scope longer than TransactionCost
  • Doing the allocation and free is also bad for performance

Summary of Changes

  • Remove writable_accounts from TransactionCost

Fixes #

@@ -104,33 +100,35 @@ impl CostModel {
);
}

fn get_writable_accounts(transaction: &SanitizedTransaction) -> Vec<Pubkey> {
pub fn writable_accounts_iter(
Copy link
Author

Choose a reason for hiding this comment

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

not ideal place for this function to live. Will, hopefully, put this somewhere shared after this is resolved: #1918 (comment)

While this iteration is not as fast as simply iterating over a Vec; I do not think it will be so much slower that we reduce overall performance.

@apfitzge
Copy link
Author

apfitzge commented Jul 3, 2024

cost_tracker benches:

master:
test bench_cost_tracker_contentious_transaction     ... bench:   4,341,232 ns/iter (+/- 96,382)
test bench_cost_tracker_non_contentious_transaction ... bench:   6,784,462 ns/iter (+/- 142,771)

PR:
test bench_cost_tracker_contentious_transaction     ... bench:   3,542,738 ns/iter (+/- 52,597)
test bench_cost_tracker_non_contentious_transaction ... bench:   5,616,565 ns/iter (+/- 99,960)

@@ -426,9 +455,10 @@ mod tests {
let simple_transaction = SanitizedTransaction::from_transaction_for_tests(
system_transaction::transfer(mint_keypair, &keypair.pubkey(), 2, *start_hash),
);
let mut tx_cost = UsageCostDetails::new_with_capacity(1);
tx_cost.programs_execution_cost = 5;
tx_cost.writable_accounts.push(mint_keypair.pubkey());
Copy link
Author

Choose a reason for hiding this comment

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

This was incorrect. Lead to test_cost_tracker_ok_add_two_same_accounts failing when we used proper writable accounts count.

@apfitzge apfitzge mentioned this pull request Jul 17, 2024
@apfitzge apfitzge self-assigned this Jul 17, 2024
@apfitzge
Copy link
Author

cost model benches (from #2168):

master:

test bench_cost_model                       ... bench:     988,185 ns/iter (+/- 33,633)
test bench_cost_model_requested_write_locks ... bench:   1,077,616 ns/iter (+/- 12,925)

PR:

test bench_cost_model                       ... bench:     818,381 ns/iter (+/- 20,589)
test bench_cost_model_requested_write_locks ... bench:     814,116 ns/iter (+/- 48,559)

More room for improvement here, this is slower than I'd like. But this PR is a step in the right direction.

@apfitzge apfitzge marked this pull request as ready for review July 17, 2024 20:37
@apfitzge apfitzge requested a review from tao-stones July 17, 2024 20:37
@apfitzge apfitzge added the enhancement New feature or request label Jul 17, 2024
Copy link

@tao-stones tao-stones left a comment

Choose a reason for hiding this comment

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

overall the changes look great, removing the vec from TransactionCost is a plus. Going to look more closely; after first glance, feel having to passing writable_accounts: impl Iterator<Item = &'a Pubkey> to every cost-model functions is a bit of repetitive. Possible to to make it a member of TransactionCost? If not, maybe cost-model functions take f(&sanitized_transaction, &tx_cost) make more sense than f(write_lock_account_iter, &tx_cost). Just a quick thought tho.

@apfitzge
Copy link
Author

Yeah I like that more than the iterator interface

@apfitzge
Copy link
Author

Closing this PR for now. Other work going on in area, want to avoid causing rebase issues for this relatively minor improvement.

@apfitzge apfitzge closed this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants