replace function with const var for better readability (#19285)

This commit is contained in:
Tao Zhu 2021-08-19 14:59:53 -05:00 committed by GitHub
parent e1fb45bd3f
commit 4982dc20f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 31 deletions

View File

@ -68,7 +68,7 @@ pub struct CostModel {
impl Default for CostModel { impl Default for CostModel {
fn default() -> Self { fn default() -> Self {
CostModel::new(account_cost_max(), block_cost_max()) CostModel::new(ACCOUNT_COST_MAX, BLOCK_COST_MAX)
} }
} }
@ -126,9 +126,9 @@ impl CostModel {
if is_writable { if is_writable {
self.transaction_cost.writable_accounts.push(*k); self.transaction_cost.writable_accounts.push(*k);
self.transaction_cost.account_access_cost += account_write_cost(); self.transaction_cost.account_access_cost += ACCOUNT_WRITE_COST;
} else { } else {
self.transaction_cost.account_access_cost += account_read_cost(); self.transaction_cost.account_access_cost += ACCOUNT_READ_COST;
} }
}); });
debug!( debug!(
@ -388,8 +388,7 @@ mod tests {
.try_into() .try_into()
.unwrap(); .unwrap();
let expected_account_cost = let expected_account_cost = ACCOUNT_WRITE_COST + ACCOUNT_WRITE_COST + ACCOUNT_READ_COST;
account_write_cost() + account_write_cost() + account_read_cost();
let expected_execution_cost = 8; let expected_execution_cost = 8;
let mut cost_model = CostModel::default(); let mut cost_model = CostModel::default();
@ -445,7 +444,7 @@ mod tests {
let number_threads = 10; let number_threads = 10;
let expected_account_cost = let expected_account_cost =
account_write_cost() + account_write_cost() * 2 + account_read_cost() * 2; ACCOUNT_WRITE_COST + ACCOUNT_WRITE_COST * 2 + ACCOUNT_READ_COST * 2;
let cost1 = 100; let cost1 = 100;
let cost2 = 200; let cost2 = 200;
// execution cost can be either 2 * Default (before write) or cost1+cost2 (after write) // execution cost can be either 2 * Default (before write) or cost1+cost2 (after write)

View File

@ -10,33 +10,25 @@ pub const SYSTEM_PARALLELISM: u64 = 10;
pub const MAX_INSTRUCTION_COST: u64 = 200_000; pub const MAX_INSTRUCTION_COST: u64 = 200_000;
pub const MAX_NUMBER_BPF_INSTRUCTIONS_PER_ACCOUNT: u64 = 200; pub const MAX_NUMBER_BPF_INSTRUCTIONS_PER_ACCOUNT: u64 = 200;
pub const fn max_instructions_per_block() -> u64 { // 4_000
(MAX_BLOCK_TIME_US / AVG_INSTRUCTION_TIME_US) * SYSTEM_PARALLELISM pub const MAX_INSTRUCTIONS_PER_BLOCK: u64 =
} (MAX_BLOCK_TIME_US / AVG_INSTRUCTION_TIME_US) * SYSTEM_PARALLELISM;
pub const fn block_cost_max() -> u64 { // 8_000_000_000
MAX_INSTRUCTION_COST * max_instructions_per_block() * 10 pub const BLOCK_COST_MAX: u64 = MAX_INSTRUCTION_COST * MAX_INSTRUCTIONS_PER_BLOCK * 10;
}
pub const fn account_cost_max() -> u64 { // 800_000_000
MAX_INSTRUCTION_COST * max_instructions_per_block() pub const ACCOUNT_COST_MAX: u64 = MAX_INSTRUCTION_COST * MAX_INSTRUCTIONS_PER_BLOCK;
}
pub const fn compute_unit_to_us_ratio() -> u64 { // 2_000
(MAX_INSTRUCTION_COST / AVG_INSTRUCTION_TIME_US) * SYSTEM_PARALLELISM pub const COMPUTE_UNIT_TO_US_RATIO: u64 =
} (MAX_INSTRUCTION_COST / AVG_INSTRUCTION_TIME_US) * SYSTEM_PARALLELISM;
pub const fn signature_cost() -> u64 { // signature takes average 10us, or 20K CU
// signature takes average 10us pub const SIGNATURE_COST: u64 = COMPUTE_UNIT_TO_US_RATIO * 10;
compute_unit_to_us_ratio() * 10
}
pub const fn account_read_cost() -> u64 { // read account averages 5us, or 10K CU
// read account averages 5us pub const ACCOUNT_READ_COST: u64 = COMPUTE_UNIT_TO_US_RATIO * 5;
compute_unit_to_us_ratio() * 5
}
pub const fn account_write_cost() -> u64 { // write account averages 25us, or 50K CU
// write account averages 25us pub const ACCOUNT_WRITE_COST: u64 = COMPUTE_UNIT_TO_US_RATIO * 25;
compute_unit_to_us_ratio() * 25
}

View File

@ -64,7 +64,7 @@ pub struct BlockCostCapacityMeter {
impl Default for BlockCostCapacityMeter { impl Default for BlockCostCapacityMeter {
fn default() -> Self { fn default() -> Self {
BlockCostCapacityMeter::new(block_cost_max()) BlockCostCapacityMeter::new(BLOCK_COST_MAX)
} }
} }