Skip to content

make clear_prefix count optional #804

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

Merged
merged 1 commit into from
Sep 21, 2022
Merged
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
5 changes: 5 additions & 0 deletions bencher/src/bencher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl Bencher {
}
}

pub fn count_clear_prefix(&mut self) {
#[cfg(not(feature = "std"))]
crate::bench::count_clear_prefix();
}

pub fn bench<T, F>(&mut self, mut inner: F) -> T
where
F: FnMut() -> T,
Expand Down
31 changes: 25 additions & 6 deletions bencher/src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub enum Warning {
#[error("clear prefix without limit, cannot be tracked")]
ClearPrefixWithoutLimit,
#[error("child storage is not supported")]
ChildStorageNoSupported,
ChildStorageNotSupported,
}

pub struct BenchTracker {
Expand All @@ -99,6 +99,7 @@ pub struct BenchTracker {
clear_prefixes: RwLock<HashMap<StorageKey, u32>>,
warnings: RwLock<Vec<Warning>>,
whitelisted_keys: RwLock<HashMap<StorageKey, (bool, bool)>>,
count_clear_prefix: RwLock<bool>,
}

impl BenchTracker {
Expand All @@ -112,6 +113,7 @@ impl BenchTracker {
clear_prefixes: RwLock::new(HashMap::new()),
warnings: RwLock::new(Vec::new()),
whitelisted_keys: RwLock::new(HashMap::new()),
count_clear_prefix: RwLock::new(false),
}
}

Expand Down Expand Up @@ -152,7 +154,10 @@ impl BenchTracker {
}

pub fn on_read_child_storage(&self, _child_info: &ChildInfo, _key: StorageKey) {
self.warn(Warning::ChildStorageNoSupported);
if self.is_redundant() {
return;
}
self.warn(Warning::ChildStorageNotSupported);
}

pub fn on_update_storage(&self, key: StorageKey) {
Expand All @@ -172,11 +177,14 @@ impl BenchTracker {
}

pub fn on_update_child_storage(&self, _child_info: &ChildInfo, _key: StorageKey) {
self.warn(Warning::ChildStorageNoSupported);
if self.is_redundant() {
return;
}
self.warn(Warning::ChildStorageNotSupported);
}

pub fn on_clear_prefix(&self, prefix: &[u8], limit: Option<u32>) {
if self.is_redundant() {
if self.is_redundant() || !(*self.count_clear_prefix.read()) {
return;
}
if let Some(limit) = limit {
Expand All @@ -196,11 +204,17 @@ impl BenchTracker {
}

pub fn on_clear_child_prefix(&self, _child_info: &ChildInfo, _prefix: &[u8], _limit: Option<u32>) {
self.warn(Warning::ChildStorageNoSupported);
if self.is_redundant() {
return;
}
self.warn(Warning::ChildStorageNotSupported);
}

pub fn on_kill_child_storage(&self, _child_info: &ChildInfo, _limit: Option<u32>) {
self.warn(Warning::ChildStorageNoSupported);
if self.is_redundant() {
return;
}
self.warn(Warning::ChildStorageNotSupported);
}

/// Get the benchmark summary
Expand Down Expand Up @@ -325,6 +339,10 @@ impl BenchTracker {
whitelisted.insert(key, (read, write));
}

pub fn count_clear_prefix(&self) {
*self.count_clear_prefix.write() = true;
}

/// Reset for the next benchmark
pub fn reset(&self) {
*self.depth.write() = 0;
Expand All @@ -334,6 +352,7 @@ impl BenchTracker {
self.clear_prefixes.write().clear();
self.warnings.write().clear();
self.whitelisted_keys.write().clear();
*self.count_clear_prefix.write() = false;
}
}

Expand Down
7 changes: 7 additions & 0 deletions bencher/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,11 @@ pub trait Bench {
.expect("No `bench_tracker` associated for the current context!");
tracker.reset();
}

fn count_clear_prefix(&mut self) {
let tracker = &***self
.extension::<BenchTrackerExt>()
.expect("No `bench_tracker` associated for the current context!");
tracker.count_clear_prefix();
}
}
1 change: 1 addition & 0 deletions bencher/test/src/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn remove_all_bar(b: &mut Bencher) {
}

fn remove_all_bar_with_limit(b: &mut Bencher) {
b.count_clear_prefix();
Bar::<Runtime>::insert(1, 1);
b.bench(|| {
Test::remove_all_bar_with_limit();
Expand Down
4 changes: 2 additions & 2 deletions bencher/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ pub mod pallet {

#[orml_weight_meter::weight(0)]
pub(crate) fn remove_all_bar() {
Bar::<T>::remove_all(None);
_ = Bar::<T>::clear(10, None);
}

#[orml_weight_meter::weight(0)]
pub(crate) fn remove_all_bar_with_limit() {
Bar::<T>::remove_all(Some(10));
_ = Bar::<T>::clear(10, None);
}
}
}