Skip to content

Commit 7e1123e

Browse files
authored
make clear_prefix count optional (#804)
1 parent 31c6db7 commit 7e1123e

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

bencher/src/bencher.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ impl Bencher {
4545
}
4646
}
4747

48+
pub fn count_clear_prefix(&mut self) {
49+
#[cfg(not(feature = "std"))]
50+
crate::bench::count_clear_prefix();
51+
}
52+
4853
pub fn bench<T, F>(&mut self, mut inner: F) -> T
4954
where
5055
F: FnMut() -> T,

bencher/src/tracker.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub enum Warning {
8787
#[error("clear prefix without limit, cannot be tracked")]
8888
ClearPrefixWithoutLimit,
8989
#[error("child storage is not supported")]
90-
ChildStorageNoSupported,
90+
ChildStorageNotSupported,
9191
}
9292

9393
pub struct BenchTracker {
@@ -99,6 +99,7 @@ pub struct BenchTracker {
9999
clear_prefixes: RwLock<HashMap<StorageKey, u32>>,
100100
warnings: RwLock<Vec<Warning>>,
101101
whitelisted_keys: RwLock<HashMap<StorageKey, (bool, bool)>>,
102+
count_clear_prefix: RwLock<bool>,
102103
}
103104

104105
impl BenchTracker {
@@ -112,6 +113,7 @@ impl BenchTracker {
112113
clear_prefixes: RwLock::new(HashMap::new()),
113114
warnings: RwLock::new(Vec::new()),
114115
whitelisted_keys: RwLock::new(HashMap::new()),
116+
count_clear_prefix: RwLock::new(false),
115117
}
116118
}
117119

@@ -152,7 +154,10 @@ impl BenchTracker {
152154
}
153155

154156
pub fn on_read_child_storage(&self, _child_info: &ChildInfo, _key: StorageKey) {
155-
self.warn(Warning::ChildStorageNoSupported);
157+
if self.is_redundant() {
158+
return;
159+
}
160+
self.warn(Warning::ChildStorageNotSupported);
156161
}
157162

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

174179
pub fn on_update_child_storage(&self, _child_info: &ChildInfo, _key: StorageKey) {
175-
self.warn(Warning::ChildStorageNoSupported);
180+
if self.is_redundant() {
181+
return;
182+
}
183+
self.warn(Warning::ChildStorageNotSupported);
176184
}
177185

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

198206
pub fn on_clear_child_prefix(&self, _child_info: &ChildInfo, _prefix: &[u8], _limit: Option<u32>) {
199-
self.warn(Warning::ChildStorageNoSupported);
207+
if self.is_redundant() {
208+
return;
209+
}
210+
self.warn(Warning::ChildStorageNotSupported);
200211
}
201212

202213
pub fn on_kill_child_storage(&self, _child_info: &ChildInfo, _limit: Option<u32>) {
203-
self.warn(Warning::ChildStorageNoSupported);
214+
if self.is_redundant() {
215+
return;
216+
}
217+
self.warn(Warning::ChildStorageNotSupported);
204218
}
205219

206220
/// Get the benchmark summary
@@ -325,6 +339,10 @@ impl BenchTracker {
325339
whitelisted.insert(key, (read, write));
326340
}
327341

342+
pub fn count_clear_prefix(&self) {
343+
*self.count_clear_prefix.write() = true;
344+
}
345+
328346
/// Reset for the next benchmark
329347
pub fn reset(&self) {
330348
*self.depth.write() = 0;
@@ -334,6 +352,7 @@ impl BenchTracker {
334352
self.clear_prefixes.write().clear();
335353
self.warnings.write().clear();
336354
self.whitelisted_keys.write().clear();
355+
*self.count_clear_prefix.write() = false;
337356
}
338357
}
339358

bencher/src/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,11 @@ pub trait Bench {
9292
.expect("No `bench_tracker` associated for the current context!");
9393
tracker.reset();
9494
}
95+
96+
fn count_clear_prefix(&mut self) {
97+
let tracker = &***self
98+
.extension::<BenchTrackerExt>()
99+
.expect("No `bench_tracker` associated for the current context!");
100+
tracker.count_clear_prefix();
101+
}
95102
}

bencher/test/src/benches.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn remove_all_bar(b: &mut Bencher) {
2525
}
2626

2727
fn remove_all_bar_with_limit(b: &mut Bencher) {
28+
b.count_clear_prefix();
2829
Bar::<Runtime>::insert(1, 1);
2930
b.bench(|| {
3031
Test::remove_all_bar_with_limit();

bencher/test/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ pub mod pallet {
7777

7878
#[orml_weight_meter::weight(0)]
7979
pub(crate) fn remove_all_bar() {
80-
Bar::<T>::remove_all(None);
80+
_ = Bar::<T>::clear(10, None);
8181
}
8282

8383
#[orml_weight_meter::weight(0)]
8484
pub(crate) fn remove_all_bar_with_limit() {
85-
Bar::<T>::remove_all(Some(10));
85+
_ = Bar::<T>::clear(10, None);
8686
}
8787
}
8888
}

0 commit comments

Comments
 (0)