Skip to content

Commit 34e62d6

Browse files
v2.1: simplify forward packet batches (backport of #3642) (#3649)
* simplify forward packet batches (#3642) simplify forward packet batches - nonzero, no branching (cherry picked from commit 2cb11a7) # Conflicts: # core/src/banking_stage/forward_packet_batches_by_accounts.rs * resolve conflicts --------- Co-authored-by: Andrew Fitzgerald <[email protected]>
1 parent 2d03f7e commit 34e62d6

File tree

1 file changed

+37
-97
lines changed

1 file changed

+37
-97
lines changed

core/src/banking_stage/forward_packet_batches_by_accounts.rs

Lines changed: 37 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use {
22
super::immutable_deserialized_packet::ImmutableDeserializedPacket,
3+
core::num::NonZeroU64,
34
solana_cost_model::{
45
block_cost_limits,
56
cost_model::CostModel,
67
cost_tracker::{CostTracker, UpdatedCosts},
7-
transaction_cost::TransactionCost,
88
},
99
solana_feature_set::FeatureSet,
1010
solana_perf::packet::Packet,
1111
solana_sdk::transaction::SanitizedTransaction,
12-
solana_svm_transaction::svm_message::SVMMessage,
1312
std::sync::Arc,
1413
};
1514

@@ -68,9 +67,8 @@ pub struct ForwardPacketBatchesByAccounts {
6867
cost_tracker: CostTracker,
6968

7069
// Compute Unit limits for each batch
71-
batch_vote_limit: u64,
72-
batch_block_limit: u64,
73-
batch_account_limit: u64,
70+
batch_block_limit: NonZeroU64,
71+
batch_account_limit: NonZeroU64,
7472
}
7573

7674
impl ForwardPacketBatchesByAccounts {
@@ -83,22 +81,32 @@ impl ForwardPacketBatchesByAccounts {
8381
.map(|_| ForwardBatch::default())
8482
.collect();
8583

86-
let batch_vote_limit = block_cost_limits::MAX_VOTE_UNITS.saturating_div(limit_ratio as u64);
84+
let batch_vote_limit =
85+
NonZeroU64::new(block_cost_limits::MAX_VOTE_UNITS.saturating_div(limit_ratio as u64))
86+
.expect("batch vote limit must not be zero");
8787
let batch_block_limit =
88-
block_cost_limits::MAX_BLOCK_UNITS.saturating_div(limit_ratio as u64);
89-
let batch_account_limit =
90-
block_cost_limits::MAX_WRITABLE_ACCOUNT_UNITS.saturating_div(limit_ratio as u64);
88+
NonZeroU64::new(block_cost_limits::MAX_BLOCK_UNITS.saturating_div(limit_ratio as u64))
89+
.expect("batch block limit must not be zero");
90+
let batch_account_limit = NonZeroU64::new(
91+
block_cost_limits::MAX_WRITABLE_ACCOUNT_UNITS.saturating_div(limit_ratio as u64),
92+
)
93+
.expect("batch account limit must not be zero");
9194

9295
let mut cost_tracker = CostTracker::default();
9396
cost_tracker.set_limits(
94-
batch_account_limit.saturating_mul(number_of_batches as u64),
95-
batch_block_limit.saturating_mul(number_of_batches as u64),
96-
batch_vote_limit.saturating_mul(number_of_batches as u64),
97+
batch_account_limit
98+
.get()
99+
.saturating_mul(number_of_batches as u64),
100+
batch_block_limit
101+
.get()
102+
.saturating_mul(number_of_batches as u64),
103+
batch_vote_limit
104+
.get()
105+
.saturating_mul(number_of_batches as u64),
97106
);
98107
Self {
99108
forward_batches,
100109
cost_tracker,
101-
batch_vote_limit,
102110
batch_block_limit,
103111
batch_account_limit,
104112
}
@@ -113,7 +121,7 @@ impl ForwardPacketBatchesByAccounts {
113121
let tx_cost = CostModel::calculate_cost(sanitized_transaction, feature_set);
114122

115123
if let Ok(updated_costs) = self.cost_tracker.try_add(&tx_cost) {
116-
let batch_index = self.get_batch_index_by_updated_costs(&tx_cost, &updated_costs);
124+
let batch_index = self.get_batch_index_by_updated_costs(&updated_costs);
117125

118126
if let Some(forward_batch) = self.forward_batches.get_mut(batch_index) {
119127
forward_batch.forwardable_packets.push(immutable_packet);
@@ -145,24 +153,12 @@ impl ForwardPacketBatchesByAccounts {
145153
// would be exceeded. Eg, if by block limit, it can be put into batch #1; by vote limit, it can
146154
// be put into batch #2; and by account limit, it can be put into batch #3; then it should be
147155
// put into batch #3 to satisfy all batch limits.
148-
fn get_batch_index_by_updated_costs(
149-
&self,
150-
tx_cost: &TransactionCost<impl SVMMessage>,
151-
updated_costs: &UpdatedCosts,
152-
) -> usize {
153-
let Some(batch_index_by_block_limit) =
154-
updated_costs.updated_block_cost.checked_div(match tx_cost {
155-
TransactionCost::SimpleVote { .. } => self.batch_vote_limit,
156-
TransactionCost::Transaction(_) => self.batch_block_limit,
157-
})
158-
else {
159-
unreachable!("batch vote limit or block limit must not be zero")
160-
};
161-
156+
fn get_batch_index_by_updated_costs(&self, updated_costs: &UpdatedCosts) -> usize {
157+
let batch_index_by_block_cost =
158+
updated_costs.updated_block_cost / self.batch_block_limit.get();
162159
let batch_index_by_account_limit =
163-
updated_costs.updated_costliest_account_cost / self.batch_account_limit;
164-
165-
batch_index_by_block_limit.max(batch_index_by_account_limit) as usize
160+
updated_costs.updated_costliest_account_cost / self.batch_account_limit.get();
161+
batch_index_by_block_cost.max(batch_index_by_account_limit) as usize
166162
}
167163
}
168164

@@ -171,14 +167,10 @@ mod tests {
171167
use {
172168
super::*,
173169
crate::banking_stage::unprocessed_packet_batches::DeserializedPacket,
174-
solana_cost_model::transaction_cost::{UsageCostDetails, WritableKeysTransaction},
175170
solana_feature_set::FeatureSet,
176171
solana_sdk::{
177-
compute_budget::ComputeBudgetInstruction,
178-
message::{Message, TransactionSignatureDetails},
179-
pubkey::Pubkey,
180-
system_instruction,
181-
transaction::Transaction,
172+
compute_budget::ComputeBudgetInstruction, message::Message, pubkey::Pubkey,
173+
system_instruction, transaction::Transaction,
182174
},
183175
};
184176

@@ -210,21 +202,6 @@ mod tests {
210202
(sanitized_transaction, deserialized_packet, limit_ratio)
211203
}
212204

213-
fn zero_transaction_cost() -> TransactionCost<'static, WritableKeysTransaction> {
214-
static DUMMY_TRANSACTION: WritableKeysTransaction = WritableKeysTransaction(vec![]);
215-
216-
TransactionCost::Transaction(UsageCostDetails {
217-
transaction: &DUMMY_TRANSACTION,
218-
signature_cost: 0,
219-
write_lock_cost: 0,
220-
data_bytes_cost: 0,
221-
programs_execution_cost: 0,
222-
loaded_accounts_data_size_cost: 0,
223-
allocated_accounts_data_size: 0,
224-
signature_details: TransactionSignatureDetails::new(0, 0, 0),
225-
})
226-
}
227-
228205
#[test]
229206
fn test_try_add_packet_to_multiple_batches() {
230207
// setup two transactions, one has high priority that writes to hot account, the
@@ -364,49 +341,16 @@ mod tests {
364341
fn test_get_batch_index_by_updated_costs() {
365342
let test_cost = 99;
366343

367-
// check against vote limit only
368-
{
369-
let mut forward_packet_batches_by_accounts =
370-
ForwardPacketBatchesByAccounts::new_with_default_batch_limits();
371-
forward_packet_batches_by_accounts.batch_vote_limit = test_cost + 1;
372-
373-
let dummy_transaction = WritableKeysTransaction(vec![]);
374-
let transaction_cost = TransactionCost::SimpleVote {
375-
transaction: &dummy_transaction,
376-
};
377-
assert_eq!(
378-
0,
379-
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
380-
&transaction_cost,
381-
&UpdatedCosts {
382-
updated_block_cost: test_cost,
383-
updated_costliest_account_cost: 0
384-
}
385-
)
386-
);
387-
assert_eq!(
388-
1,
389-
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
390-
&transaction_cost,
391-
&UpdatedCosts {
392-
updated_block_cost: test_cost + 1,
393-
updated_costliest_account_cost: 0
394-
}
395-
)
396-
);
397-
}
398-
399344
// check against block limit only
400345
{
401346
let mut forward_packet_batches_by_accounts =
402347
ForwardPacketBatchesByAccounts::new_with_default_batch_limits();
403-
forward_packet_batches_by_accounts.batch_block_limit = test_cost + 1;
348+
forward_packet_batches_by_accounts.batch_block_limit =
349+
NonZeroU64::new(test_cost + 1).unwrap();
404350

405-
let transaction_cost = zero_transaction_cost();
406351
assert_eq!(
407352
0,
408353
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
409-
&transaction_cost,
410354
&UpdatedCosts {
411355
updated_block_cost: test_cost,
412356
updated_costliest_account_cost: 0
@@ -416,7 +360,6 @@ mod tests {
416360
assert_eq!(
417361
1,
418362
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
419-
&transaction_cost,
420363
&UpdatedCosts {
421364
updated_block_cost: test_cost + 1,
422365
updated_costliest_account_cost: 0
@@ -429,13 +372,12 @@ mod tests {
429372
{
430373
let mut forward_packet_batches_by_accounts =
431374
ForwardPacketBatchesByAccounts::new_with_default_batch_limits();
432-
forward_packet_batches_by_accounts.batch_account_limit = test_cost + 1;
375+
forward_packet_batches_by_accounts.batch_account_limit =
376+
NonZeroU64::new(test_cost + 1).unwrap();
433377

434-
let transaction_cost = zero_transaction_cost();
435378
assert_eq!(
436379
0,
437380
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
438-
&transaction_cost,
439381
&UpdatedCosts {
440382
updated_block_cost: 0,
441383
updated_costliest_account_cost: test_cost
@@ -445,7 +387,6 @@ mod tests {
445387
assert_eq!(
446388
1,
447389
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
448-
&transaction_cost,
449390
&UpdatedCosts {
450391
updated_block_cost: 0,
451392
updated_costliest_account_cost: test_cost + 1
@@ -461,15 +402,14 @@ mod tests {
461402
{
462403
let mut forward_packet_batches_by_accounts =
463404
ForwardPacketBatchesByAccounts::new_with_default_batch_limits();
464-
forward_packet_batches_by_accounts.batch_block_limit = test_cost + 1;
465-
forward_packet_batches_by_accounts.batch_vote_limit = test_cost / 2 + 1;
466-
forward_packet_batches_by_accounts.batch_account_limit = test_cost / 3 + 1;
405+
forward_packet_batches_by_accounts.batch_block_limit =
406+
NonZeroU64::new(test_cost + 1).unwrap();
407+
forward_packet_batches_by_accounts.batch_account_limit =
408+
NonZeroU64::new(test_cost / 3 + 1).unwrap();
467409

468-
let transaction_cost = zero_transaction_cost();
469410
assert_eq!(
470411
2,
471412
forward_packet_batches_by_accounts.get_batch_index_by_updated_costs(
472-
&transaction_cost,
473413
&UpdatedCosts {
474414
updated_block_cost: test_cost,
475415
updated_costliest_account_cost: test_cost

0 commit comments

Comments
 (0)