HardwareCounterCell::disposable() Performance Improvement (#6511)

* Improve performance of HardwareCounterCell::disposable() by 72x

* Fix ci
This commit is contained in:
Jojii
2025-05-09 11:11:22 +02:00
committed by GitHub
parent e12de1472d
commit fa9d0ec406
5 changed files with 43 additions and 11 deletions
+4
View File
@@ -61,3 +61,7 @@ harness = false
[[bench]]
name = "mmap_hashmap"
harness = false
[[bench]]
name = "hw_counter"
harness = false
+20
View File
@@ -0,0 +1,20 @@
use common::counter::hardware_accumulator::HwMeasurementAcc;
use common::counter::hardware_counter::HardwareCounterCell;
use criterion::{Criterion, criterion_group, criterion_main};
fn bench_hw_counter(c: &mut Criterion) {
c.bench_function("Disposable Hw Cell", |b| {
b.iter(|| {
let _ = HardwareCounterCell::disposable();
});
});
c.bench_function("Disposable Hw Acc", |b| {
b.iter(|| {
let _ = HwMeasurementAcc::disposable();
});
});
}
criterion_group!(hw_counter, bench_hw_counter);
criterion_main!(hw_counter);
@@ -97,7 +97,7 @@ mod test {
}
assert_eq!(parent.cpu_counter().get(), 0);
assert_eq!(parent.accumulator.get_cpu(), 0);
assert_eq!(parent.accumulator.as_ref().unwrap().get_cpu(), 0);
}
#[test]
@@ -111,7 +111,7 @@ mod test {
assert_eq!(parent.cpu_counter().get(), 5);
let parent_acc = parent.accumulator.clone();
let parent_acc = parent.new_accumulator();
drop(parent); // Parents accumulator gets written after `parent` drops.
assert_eq!(parent_acc.get_cpu(), 5);
}
@@ -131,7 +131,7 @@ mod test {
}
assert_eq!(parent.cpu_counter().get(), 0); // Parents accumulator gets written, not the counter cell!
assert_eq!(parent.accumulator.get_cpu(), 5);
assert_eq!(parent.accumulator.as_ref().unwrap().get_cpu(), 5);
}
#[test]
@@ -18,7 +18,7 @@ pub struct HardwareCounterCell {
pub(super) payload_index_io_write_counter: CounterCell,
pub(super) vector_io_read_counter: CounterCell,
pub(super) vector_io_write_counter: CounterCell,
pub(super) accumulator: HwMeasurementAcc,
pub(super) accumulator: Option<HwMeasurementAcc>,
}
#[cfg(feature = "testing")]
@@ -50,7 +50,7 @@ impl HardwareCounterCell {
payload_index_io_write_counter: CounterCell::new(),
vector_io_read_counter: CounterCell::new(),
vector_io_write_counter: CounterCell::new(),
accumulator: HwMeasurementAcc::new(),
accumulator: Some(HwMeasurementAcc::new()),
}
}
@@ -68,7 +68,7 @@ impl HardwareCounterCell {
payload_index_io_write_counter: CounterCell::new(),
vector_io_read_counter: CounterCell::new(),
vector_io_write_counter: CounterCell::new(),
accumulator: HwMeasurementAcc::disposable(),
accumulator: None,
}
}
@@ -83,12 +83,14 @@ impl HardwareCounterCell {
payload_index_io_write_counter: CounterCell::new(),
vector_io_read_counter: CounterCell::new(),
vector_io_write_counter: CounterCell::new(),
accumulator,
accumulator: Some(accumulator),
}
}
pub fn new_accumulator(&self) -> HwMeasurementAcc {
self.accumulator.clone()
self.accumulator
.clone()
.unwrap_or_else(HwMeasurementAcc::disposable)
}
/// Create a copy of the current counter cell with the same accumulator and config,
@@ -181,7 +183,9 @@ impl HardwareCounterCell {
}
fn merge_to_accumulator(&self) {
self.accumulator.accumulate(self.get_hw_data());
if let Some(accumulator) = &self.accumulator {
accumulator.accumulate(self.get_hw_data());
}
}
}
@@ -201,7 +205,11 @@ impl Drop for HardwareCounterCell {
impl From<&HardwareCounterCell> for HardwareData {
fn from(value: &HardwareCounterCell) -> Self {
let counter_values = value.get_hw_data();
let acc_values = value.accumulator.hw_data();
let acc_values = value
.accumulator
.as_ref()
.map(|i| i.hw_data())
.unwrap_or_default();
counter_values + acc_values
}
}
@@ -1,7 +1,7 @@
use std::ops::Add;
/// Contains all hardware metrics. Only serves as value holding structure without any semantics.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Default)]
pub struct HardwareData {
pub cpu: usize,
pub payload_io_read: usize,