cost model nits (#18528)

This commit is contained in:
Jack May
2021-07-09 12:55:31 -07:00
committed by GitHub
parent 761de8b1a3
commit e9ace3a0d5
6 changed files with 75 additions and 61 deletions

View File

@@ -135,26 +135,27 @@ impl CostUpdateService {
fn update_cost_model(cost_model: &RwLock<CostModel>, execute_timings: &ExecuteTimings) -> bool {
let mut dirty = false;
let mut cost_model_mutable = cost_model.write().unwrap();
for (program_id, stats) in &execute_timings.details.per_program_timings {
let cost = stats.0 / stats.1 as u64;
match cost_model_mutable.upsert_instruction_cost(program_id, &cost) {
Ok(c) => {
debug!(
"after replayed into bank, instruction {:?} has averaged cost {}",
program_id, c
);
dirty = true;
}
Err(err) => {
debug!(
{
let mut cost_model_mutable = cost_model.write().unwrap();
for (program_id, tining) in &execute_timings.details.per_program_timings {
let cost = tining.accumulated_us / tining.count as u64;
match cost_model_mutable.upsert_instruction_cost(program_id, cost) {
Ok(c) => {
debug!(
"after replayed into bank, instruction {:?} has averaged cost {}",
program_id, c
);
dirty = true;
}
Err(err) => {
debug!(
"after replayed into bank, instruction {:?} failed to update cost, err: {}",
program_id, err
);
}
}
}
}
drop(cost_model_mutable);
debug!(
"after replayed into bank, updated cost model instruction cost table, current values: {:?}",
cost_model.read().unwrap().get_instruction_cost_table()
@@ -187,6 +188,7 @@ impl CostUpdateService {
#[cfg(test)]
mod tests {
use super::*;
use solana_runtime::message_processor::ProgramTiming;
use solana_sdk::pubkey::Pubkey;
#[test]
@@ -219,10 +221,13 @@ mod tests {
let count: u32 = 10;
expected_cost = accumulated_us / count as u64;
execute_timings
.details
.per_program_timings
.insert(program_key_1, (accumulated_us, count));
execute_timings.details.per_program_timings.insert(
program_key_1,
ProgramTiming {
accumulated_us,
count,
},
);
CostUpdateService::update_cost_model(&cost_model, &execute_timings);
assert_eq!(
1,
@@ -249,10 +254,13 @@ mod tests {
// to expect new cost is Average(new_value, existing_value)
expected_cost = ((accumulated_us / count as u64) + expected_cost) / 2;
execute_timings
.details
.per_program_timings
.insert(program_key_1, (accumulated_us, count));
execute_timings.details.per_program_timings.insert(
program_key_1,
ProgramTiming {
accumulated_us,
count,
},
);
CostUpdateService::update_cost_model(&cost_model, &execute_timings);
assert_eq!(
1,