Improve complexity of CompactAssignments::unique_targets (#8314)

* Improve complexity of CompactAssignments::unique_targets

Original implementation was O(n**2). Current impl is O(n log n).

Avoided the original proposed mitigation because it does not retain
the de-duplicating property present in the original implementation.
This implementation does a little more work, but retains that property.

* Explicitly choose sp_std Vec and BTreeSet

Ensures that the macro still works if someone uses it in a context
in which sp_std is not imported or is renamed.

* explicitly use sp_std vectors throughout compact macro
This commit is contained in:
Peter Goodspeed-Niklaus
2021-03-17 09:13:33 +01:00
committed by GitHub
parent 2586d55754
commit 23b32e7543
4 changed files with 25 additions and 24 deletions
@@ -49,14 +49,14 @@ fn decode_impl(
quote! {
let #name =
<
Vec<(_npos::codec::Compact<#voter_type>, _npos::codec::Compact<#target_type>)>
_npos::sp_std::prelude::Vec<(_npos::codec::Compact<#voter_type>, _npos::codec::Compact<#target_type>)>
as
_npos::codec::Decode
>::decode(value)?;
let #name = #name
.into_iter()
.map(|(v, t)| (v.0, t.0))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
}
};
@@ -65,7 +65,7 @@ fn decode_impl(
quote! {
let #name =
<
Vec<(
_npos::sp_std::prelude::Vec<(
_npos::codec::Compact<#voter_type>,
(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>),
_npos::codec::Compact<#target_type>,
@@ -76,7 +76,7 @@ fn decode_impl(
let #name = #name
.into_iter()
.map(|(v, (t1, w), t2)| (v.0, (t1.0, w.0), t2.0))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
}
};
@@ -90,7 +90,7 @@ fn decode_impl(
quote! {
let #name =
<
Vec<(
_npos::sp_std::prelude::Vec<(
_npos::codec::Compact<#voter_type>,
[(_npos::codec::Compact<#target_type>, _npos::codec::Compact<#weight_type>); #c-1],
_npos::codec::Compact<#target_type>,
@@ -104,7 +104,7 @@ fn decode_impl(
[ #inner_impl ],
t_last.0,
))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
}
}).collect::<TokenStream2>();
@@ -142,7 +142,7 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
_npos::codec::Compact(v.clone()),
_npos::codec::Compact(t.clone()),
))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
#name.encode_to(&mut r);
}
};
@@ -160,7 +160,7 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
),
_npos::codec::Compact(t2.clone()),
))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
#name.encode_to(&mut r);
}
};
@@ -184,14 +184,14 @@ fn encode_impl(ident: syn::Ident, count: usize) -> TokenStream2 {
[ #inners_compact_array ],
_npos::codec::Compact(t_last.clone()),
))
.collect::<Vec<_>>();
.collect::<_npos::sp_std::prelude::Vec<_>>();
#name.encode_to(&mut r);
}
}).collect::<TokenStream2>();
quote!(
impl _npos::codec::Encode for #ident {
fn encode(&self) -> Vec<u8> {
fn encode(&self) -> _npos::sp_std::prelude::Vec<u8> {
let mut r = vec![];
#encode_impl_single
#encode_impl_double