From 91acf6cde2f1954191ea5c65b1da7e1523ae861b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org> Date: Wed, 15 Jan 2025 14:17:54 +0100 Subject: [PATCH 1/2] Optimize do_remove_all_certs_received_by --- pallets/certification/src/benchmarking.rs | 23 +++++++++++++++++ pallets/certification/src/lib.rs | 25 ++++++++++++++++--- pallets/certification/src/weights.rs | 17 +++++++++++++ .../g1/src/weights/pallet_certification.rs | 22 ++++++++++++++++ .../gdev/src/weights/pallet_certification.rs | 24 ++++++++++++++++++ .../gtest/src/weights/pallet_certification.rs | 22 ++++++++++++++++ 6 files changed, 129 insertions(+), 4 deletions(-) diff --git a/pallets/certification/src/benchmarking.rs b/pallets/certification/src/benchmarking.rs index 1fa4b0bfb..6f9d2ee32 100644 --- a/pallets/certification/src/benchmarking.rs +++ b/pallets/certification/src/benchmarking.rs @@ -157,6 +157,29 @@ mod benchmarks { Ok(()) } + #[benchmark] + fn do_remove_all_certs_received_by(i: Linear<2, 100>) -> Result<(), BenchmarkError> { + let receiver: T::IdtyIndex = 0.into(); + add_certs::<T>(i, receiver)?; + + #[block] + { + Pallet::<T>::do_remove_all_certs_received_by(receiver); + } + + for issuer in 1..i { + assert_has_event::<T>( + Event::<T>::CertRemoved { + issuer: issuer.into(), + receiver, + expiration: false, + } + .into(), + ); + } + Ok(()) + } + impl_benchmark_test_suite!( Pallet, crate::mock::new_test_ext(crate::mock::DefaultCertificationConfig { diff --git a/pallets/certification/src/lib.rs b/pallets/certification/src/lib.rs index 08aa992f7..204c8cb08 100644 --- a/pallets/certification/src/lib.rs +++ b/pallets/certification/src/lib.rs @@ -362,11 +362,28 @@ pub mod pallet { impl<T: Config> Pallet<T> { /// Perform removal of all certifications received by an identity. pub fn do_remove_all_certs_received_by(idty_index: T::IdtyIndex) -> Weight { - let mut weight = T::DbWeight::get().reads_writes(1, 0); - for (issuer, _) in CertsByReceiver::<T>::get(idty_index) { - weight = weight.saturating_add(Self::do_remove_cert(issuer, idty_index, None)); + let received_certs = CertsByReceiver::<T>::take(idty_index); + for (receiver_received_count, (issuer, _)) in received_certs.iter().enumerate().rev() { + let issuer_issued_count = + <StorageIdtyCertMeta<T>>::mutate_exists(issuer, |cert_meta_opt| { + let cert_meta = cert_meta_opt.get_or_insert(IdtyCertMeta::default()); + cert_meta.issued_count = cert_meta.issued_count.saturating_sub(1); + cert_meta.issued_count + }); + T::OnRemovedCert::on_removed_cert( + *issuer, + issuer_issued_count, + idty_index, + receiver_received_count as u32, + false, + ); + Self::deposit_event(Event::CertRemoved { + issuer: *issuer, + receiver: idty_index, + expiration: false, + }); } - weight + T::WeightInfo::do_remove_all_certs_received_by(received_certs.len() as u32) } /// Get the issuer index from the origin. diff --git a/pallets/certification/src/weights.rs b/pallets/certification/src/weights.rs index e58c33831..49280e9f2 100644 --- a/pallets/certification/src/weights.rs +++ b/pallets/certification/src/weights.rs @@ -27,6 +27,7 @@ pub trait WeightInfo { fn on_initialize() -> Weight; fn do_remove_cert_noop() -> Weight; fn do_remove_cert() -> Weight; + fn do_remove_all_certs_received_by(i: u32) -> Weight; } // Insecure weights implementation, use it for tests only! @@ -102,4 +103,20 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(7 as u64)) .saturating_add(RocksDbWeight::get().writes(4 as u64)) } + + // Storage: Cert CertsByReceiver (r:1 w:1) + // Storage: Cert StorageIdtyCertMeta (r:2 w:2) + // Storage: Parameters ParametersStorage (r:1 w:0) + // Storage: Membership Membership (r:1 w:0) + /// The range of component `i` is `[2, 1000]`. + fn do_remove_all_certs_received_by(i: u32) -> Weight { + // Minimum execution time: 223_292 nanoseconds. + Weight::from_parts(233_586_000 as u64, 0) + // Standard Error: 598_929 + .saturating_add(Weight::from_parts(53_659_501 as u64, 0).saturating_mul(i as u64)) + .saturating_add(RocksDbWeight::get().reads(3 as u64)) + .saturating_add(RocksDbWeight::get().reads((1 as u64).saturating_mul(i as u64))) + .saturating_add(RocksDbWeight::get().writes(1 as u64)) + .saturating_add(RocksDbWeight::get().writes((1 as u64).saturating_mul(i as u64))) + } } diff --git a/runtime/g1/src/weights/pallet_certification.rs b/runtime/g1/src/weights/pallet_certification.rs index 9b40f1ab3..3b52d4e48 100644 --- a/runtime/g1/src/weights/pallet_certification.rs +++ b/runtime/g1/src/weights/pallet_certification.rs @@ -188,4 +188,26 @@ impl<T: frame_system::Config> pallet_certification::WeightInfo for WeightInfo<T> .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(3)) } + /// Storage: `Certification::CertsByReceiver` (r:1 w:1) + /// Proof: `Certification::CertsByReceiver` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Certification::StorageIdtyCertMeta` (r:1000 w:1000) + /// Proof: `Certification::StorageIdtyCertMeta` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Membership::Membership` (r:1 w:0) + /// Proof: `Membership::Membership` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `i` is `[2, 1000]`. + fn do_remove_all_certs_received_by(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `554 + i * (35 ±0)` + // Estimated: `4018 + i * (2511 ±0)` + // Minimum execution time: 30_026_000 picoseconds. + Weight::from_parts(30_298_000, 0) + .saturating_add(Weight::from_parts(0, 4018)) + // Standard Error: 24_742 + .saturating_add(Weight::from_parts(8_970_118, 0).saturating_mul(i.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(i.into()))) + .saturating_add(T::DbWeight::get().writes(1)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) + .saturating_add(Weight::from_parts(0, 2511).saturating_mul(i.into())) + } } diff --git a/runtime/gdev/src/weights/pallet_certification.rs b/runtime/gdev/src/weights/pallet_certification.rs index e3accd5ec..73df17f4b 100644 --- a/runtime/gdev/src/weights/pallet_certification.rs +++ b/runtime/gdev/src/weights/pallet_certification.rs @@ -172,4 +172,28 @@ impl<T: frame_system::Config> pallet_certification::WeightInfo for WeightInfo<T> .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(3)) } + /// Storage: `Certification::CertsByReceiver` (r:1 w:1) + /// Proof: `Certification::CertsByReceiver` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Certification::StorageIdtyCertMeta` (r:1000 w:1000) + /// Proof: `Certification::StorageIdtyCertMeta` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::ParametersStorage` (r:1 w:0) + /// Proof: `Parameters::ParametersStorage` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Membership::Membership` (r:1 w:0) + /// Proof: `Membership::Membership` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `i` is `[2, 1000]`. + fn do_remove_all_certs_received_by(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `665 + i * (35 ±0)` + // Estimated: `4129 + i * (2511 ±0)` + // Minimum execution time: 33_392_000 picoseconds. + Weight::from_parts(34_295_000, 0) + .saturating_add(Weight::from_parts(0, 4129)) + // Standard Error: 24_307 + .saturating_add(Weight::from_parts(9_192_872, 0).saturating_mul(i.into())) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(i.into()))) + .saturating_add(T::DbWeight::get().writes(1)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) + .saturating_add(Weight::from_parts(0, 2511).saturating_mul(i.into())) + } } diff --git a/runtime/gtest/src/weights/pallet_certification.rs b/runtime/gtest/src/weights/pallet_certification.rs index df9b6216d..6c9f58a2c 100644 --- a/runtime/gtest/src/weights/pallet_certification.rs +++ b/runtime/gtest/src/weights/pallet_certification.rs @@ -188,4 +188,26 @@ impl<T: frame_system::Config> pallet_certification::WeightInfo for WeightInfo<T> .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(3)) } + /// Storage: `Certification::CertsByReceiver` (r:1 w:1) + /// Proof: `Certification::CertsByReceiver` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Certification::StorageIdtyCertMeta` (r:1000 w:1000) + /// Proof: `Certification::StorageIdtyCertMeta` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// Storage: `Membership::Membership` (r:1 w:0) + /// Proof: `Membership::Membership` (`max_values`: None, `max_size`: None, mode: `Measured`) + /// The range of component `i` is `[2, 1000]`. + fn do_remove_all_certs_received_by(i: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `554 + i * (35 ±0)` + // Estimated: `4018 + i * (2511 ±0)` + // Minimum execution time: 29_810_000 picoseconds. + Weight::from_parts(30_875_000, 0) + .saturating_add(Weight::from_parts(0, 4018)) + // Standard Error: 25_367 + .saturating_add(Weight::from_parts(8_966_191, 0).saturating_mul(i.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(i.into()))) + .saturating_add(T::DbWeight::get().writes(1)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into()))) + .saturating_add(Weight::from_parts(0, 2511).saturating_mul(i.into())) + } } -- GitLab From fb6f3d78ec7039c51f0fa4dc2d3d9170d6936ba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pascal=20Eng=C3=A9libert?= <tuxmain@zettascript.org> Date: Wed, 15 Jan 2025 14:21:04 +0100 Subject: [PATCH 2/2] certification/benchmark: fix add_certs number --- pallets/certification/src/benchmarking.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pallets/certification/src/benchmarking.rs b/pallets/certification/src/benchmarking.rs index 6f9d2ee32..668d5cdfa 100644 --- a/pallets/certification/src/benchmarking.rs +++ b/pallets/certification/src/benchmarking.rs @@ -37,11 +37,11 @@ mod benchmarks { fn add_certs<T: Config>(i: u32, receiver: T::IdtyIndex) -> Result<(), &'static str> { Pallet::<T>::remove_all_certs_received_by(RawOrigin::Root.into(), receiver)?; - for j in 1..i { + for j in 1..i + 1 { Pallet::<T>::do_add_cert_checked(j.into(), receiver, false)?; } assert!( - CertsByReceiver::<T>::get(receiver).len() as u32 == i - 1, + CertsByReceiver::<T>::get(receiver).len() as u32 == i, "Certs not added", ); Ok(()) @@ -167,7 +167,7 @@ mod benchmarks { Pallet::<T>::do_remove_all_certs_received_by(receiver); } - for issuer in 1..i { + for issuer in 1..i + 1 { assert_has_event::<T>( Event::<T>::CertRemoved { issuer: issuer.into(), -- GitLab