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
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions core/src/banking_stage/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ impl Consumer {
// were not included in the block should have their cost removed, the rest
// should update with their actually consumed units.
QosService::remove_or_update_costs(
txs.iter(),
transaction_qos_cost_results.iter(),
commit_transactions_result.as_ref().ok(),
bank,
Expand Down
9 changes: 5 additions & 4 deletions core/src/banking_stage/forward_packet_batches_by_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ impl ForwardPacketBatchesByAccounts {
) -> bool {
let tx_cost = CostModel::calculate_cost(sanitized_transaction, feature_set);

if let Ok(updated_costs) = self.cost_tracker.try_add(&tx_cost) {
if let Ok(updated_costs) = self.cost_tracker.try_add(
CostModel::writable_accounts_iter(sanitized_transaction),
&tx_cost,
) {
let batch_index = self.get_batch_index_by_updated_costs(&tx_cost, &updated_costs);

if let Some(forward_batch) = self.forward_batches.get_mut(batch_index) {
Expand Down Expand Up @@ -349,9 +352,7 @@ mod tests {
ForwardPacketBatchesByAccounts::new_with_default_batch_limits();
forward_packet_batches_by_accounts.batch_vote_limit = test_cost + 1;

let transaction_cost = TransactionCost::SimpleVote {
writable_accounts: vec![],
};
let transaction_cost = TransactionCost::SimpleVote;
assert_eq!(
0,
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
Expand Down
43 changes: 29 additions & 14 deletions core/src/banking_stage/qos_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl QosService {
.map(|(tx, cost)| {
match cost {
Ok(cost) => {
match cost_tracker.try_add(&cost) {
match cost_tracker.try_add(CostModel::writable_accounts_iter(tx),&cost) {
Ok(UpdatedCosts{updated_block_cost, updated_costliest_account_cost}) => {
debug!("slot {:?}, transaction {:?}, cost {:?}, fit into current block, current block cost {}, updated costliest account cost {}", bank.slot(), tx, cost, updated_block_cost, updated_costliest_account_cost);
self.metrics.stats.selected_txs_count.fetch_add(1, Ordering::Relaxed);
Expand Down Expand Up @@ -135,34 +135,43 @@ impl QosService {
/// Removes transaction costs from the cost tracker if not committed or recorded, or
/// updates the transaction costs for committed transactions.
pub fn remove_or_update_costs<'a>(
transactions: impl Iterator<Item = &'a SanitizedTransaction>,
transaction_cost_results: impl Iterator<Item = &'a transaction::Result<TransactionCost>>,
transaction_committed_status: Option<&Vec<CommitTransactionDetails>>,
bank: &Bank,
) {
match transaction_committed_status {
Some(transaction_committed_status) => {
Self::remove_or_update_recorded_transaction_costs(
transactions,
transaction_cost_results,
transaction_committed_status,
bank,
)
}
None => Self::remove_unrecorded_transaction_costs(transaction_cost_results, bank),
None => Self::remove_unrecorded_transaction_costs(
transactions,
transaction_cost_results,
bank,
),
}
}

/// For recorded transactions, remove units reserved by uncommitted transaction, or update
/// units for committed transactions.
fn remove_or_update_recorded_transaction_costs<'a>(
transactions: impl Iterator<Item = &'a SanitizedTransaction>,
transaction_cost_results: impl Iterator<Item = &'a transaction::Result<TransactionCost>>,
transaction_committed_status: &Vec<CommitTransactionDetails>,
bank: &Bank,
) {
let mut cost_tracker = bank.write_cost_tracker().unwrap();
let mut num_included = 0;
transaction_cost_results
transactions
.into_iter()
.zip(transaction_cost_results)
.zip(transaction_committed_status)
.for_each(|(tx_cost, transaction_committed_details)| {
.for_each(|((tx, tx_cost), transaction_committed_details)| {
// Only transactions that the qos service included have to be
// checked for update
if let Ok(tx_cost) = tx_cost {
Expand All @@ -173,6 +182,7 @@ impl QosService {
loaded_accounts_data_size,
} => {
cost_tracker.update_execution_cost(
CostModel::writable_accounts_iter(tx),
tx_cost,
*compute_units,
CostModel::calculate_loaded_accounts_data_size_cost(
Expand All @@ -182,7 +192,7 @@ impl QosService {
);
}
CommitTransactionDetails::NotCommitted => {
cost_tracker.remove(tx_cost);
cost_tracker.remove(CostModel::writable_accounts_iter(tx), tx_cost);
}
}
}
Expand All @@ -192,19 +202,22 @@ impl QosService {

/// Remove reserved units for transaction batch that unsuccessfully recorded.
fn remove_unrecorded_transaction_costs<'a>(
transactions: impl Iterator<Item = &'a SanitizedTransaction>,
transaction_cost_results: impl Iterator<Item = &'a transaction::Result<TransactionCost>>,
bank: &Bank,
) {
let mut cost_tracker = bank.write_cost_tracker().unwrap();
let mut num_included = 0;
transaction_cost_results.for_each(|tx_cost| {
// Only transactions that the qos service included have to be
// removed
if let Ok(tx_cost) = tx_cost {
num_included += 1;
cost_tracker.remove(tx_cost);
}
});
transactions
.zip(transaction_cost_results)
.for_each(|(tx, tx_cost)| {
// Only transactions that the qos service included have to be
// removed
if let Ok(tx_cost) = tx_cost {
num_included += 1;
cost_tracker.remove(CostModel::writable_accounts_iter(tx), tx_cost);
}
});
cost_tracker.sub_transactions_in_flight(num_included);
}

Expand Down Expand Up @@ -760,6 +773,7 @@ mod tests {
* transaction_count;

QosService::remove_or_update_costs(
txs.iter(),
qos_cost_results.iter(),
Some(&committed_status),
&bank,
Expand Down Expand Up @@ -811,7 +825,7 @@ mod tests {
bank.read_cost_tracker().unwrap().block_cost()
);

QosService::remove_or_update_costs(qos_cost_results.iter(), None, &bank);
QosService::remove_or_update_costs(txs.iter(), qos_cost_results.iter(), None, &bank);
assert_eq!(0, bank.read_cost_tracker().unwrap().block_cost());
assert_eq!(0, bank.read_cost_tracker().unwrap().transaction_count());
}
Expand Down Expand Up @@ -884,6 +898,7 @@ mod tests {
.collect();

QosService::remove_or_update_costs(
txs.iter(),
qos_cost_results.iter(),
Some(&committed_status),
&bank,
Expand Down
46 changes: 30 additions & 16 deletions cost-model/benches/cost_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {

struct BenchSetup {
cost_tracker: CostTracker,
writable_accounts: Vec<Vec<Pubkey>>,
tx_costs: Vec<TransactionCost>,
}

Expand All @@ -22,26 +23,29 @@ fn setup(num_transactions: usize, contentious_transactions: bool) -> BenchSetup

let max_accounts_per_tx = 128;
let pubkey = Pubkey::new_unique();
let mut writable_accounts = Vec::with_capacity(num_transactions);
let tx_costs = (0..num_transactions)
.map(|_| {
let mut usage_cost_details = UsageCostDetails::default();
(0..max_accounts_per_tx).for_each(|_| {
let writable_account_key = if contentious_transactions {
pubkey
} else {
Pubkey::new_unique()
};
usage_cost_details
.writable_accounts
.push(writable_account_key)
});
writable_accounts.push(
(0..max_accounts_per_tx)
.map(|_| {
if contentious_transactions {
pubkey
} else {
Pubkey::new_unique()
}
})
.collect(),
);
usage_cost_details.programs_execution_cost = 9999;
TransactionCost::Transaction(usage_cost_details)
})
.collect_vec();

BenchSetup {
cost_tracker,
writable_accounts,
tx_costs,
}
}
Expand All @@ -50,15 +54,20 @@ fn setup(num_transactions: usize, contentious_transactions: bool) -> BenchSetup
fn bench_cost_tracker_non_contentious_transaction(bencher: &mut Bencher) {
let BenchSetup {
mut cost_tracker,
writable_accounts,
tx_costs,
} = setup(1024, false);

bencher.iter(|| {
for tx_cost in tx_costs.iter() {
if cost_tracker.try_add(tx_cost).is_err() {
for (writable_accounts, tx_cost) in writable_accounts.iter().zip(tx_costs.iter()) {
if cost_tracker
.try_add(writable_accounts.iter(), tx_cost)
.is_err()
{
break;
} // stop when hit limits
cost_tracker.update_execution_cost(tx_cost, 0, 0); // update execution cost down to zero
cost_tracker.update_execution_cost(writable_accounts.iter(), tx_cost, 0, 0);
// update execution cost down to zero
}
});
}
Expand All @@ -67,15 +76,20 @@ fn bench_cost_tracker_non_contentious_transaction(bencher: &mut Bencher) {
fn bench_cost_tracker_contentious_transaction(bencher: &mut Bencher) {
let BenchSetup {
mut cost_tracker,
writable_accounts,
tx_costs,
} = setup(1024, true);

bencher.iter(|| {
for tx_cost in tx_costs.iter() {
if cost_tracker.try_add(tx_cost).is_err() {
for (writable_accounts, tx_cost) in writable_accounts.iter().zip(tx_costs.iter()) {
if cost_tracker
.try_add(writable_accounts.iter(), tx_cost)
.is_err()
{
break;
} // stop when hit limits
cost_tracker.update_execution_cost(tx_cost, 0, 0); // update execution cost down to zero
cost_tracker.update_execution_cost(writable_accounts.iter(), tx_cost, 0, 0);
// update execution cost down to zero
}
});
}
Loading