Skip to content

Commit f06c4be

Browse files
apoelstratcharding
authored andcommitted
sortedmulti: eliminate allocation in constructor_check
1 parent f1ab0eb commit f06c4be

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/descriptor/sortedmulti.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ pub struct SortedMultiVec<Pk: MiniscriptKey, Ctx: ScriptContext> {
3232
}
3333

3434
impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
35-
fn constructor_check(&self) -> Result<(), Error> {
35+
fn constructor_check(mut self) -> Result<Self, Error> {
3636
// Check the limits before creating a new SortedMultiVec
3737
// For example, under p2sh context the scriptlen can only be
3838
// upto 520 bytes.
39-
let term: Terminal<Pk, Ctx> = Terminal::Multi(self.inner.clone());
39+
let term: Terminal<Pk, Ctx> = Terminal::Multi(self.inner);
4040
let ms = Miniscript::from_ast(term)?;
4141
// This would check all the consensus rules for p2sh/p2wsh and
4242
// even tapscript in future
43-
Ctx::check_local_validity(&ms).map_err(From::from)
43+
Ctx::check_local_validity(&ms)?;
44+
if let Terminal::Multi(inner) = ms.node {
45+
self.inner = inner;
46+
Ok(self)
47+
} else {
48+
unreachable!()
49+
}
4450
}
4551

4652
/// Create a new instance of `SortedMultiVec` given a list of keys and the threshold
@@ -49,8 +55,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
4955
pub fn new(k: usize, pks: Vec<Pk>) -> Result<Self, Error> {
5056
let ret =
5157
Self { inner: Threshold::new(k, pks).map_err(Error::Threshold)?, phantom: PhantomData };
52-
ret.constructor_check()?;
53-
Ok(ret)
58+
ret.constructor_check()
5459
}
5560

5661
/// Parse an expression tree into a SortedMultiVec
@@ -66,8 +71,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
6671
.translate_by_index(|i| expression::terminal(&tree.args[i + 1], Pk::from_str))?,
6772
phantom: PhantomData,
6873
};
69-
ret.constructor_check()?;
70-
Ok(ret)
74+
ret.constructor_check()
7175
}
7276

7377
/// This will panic if fpk returns an uncompressed key when
@@ -85,8 +89,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
8589
inner: self.inner.translate_ref(|pk| t.pk(pk))?,
8690
phantom: PhantomData,
8791
};
88-
ret.constructor_check().map_err(TranslateErr::OuterError)?;
89-
Ok(ret)
92+
ret.constructor_check().map_err(TranslateErr::OuterError)
9093
}
9194

9295
/// The threshold value for the multisig.

0 commit comments

Comments
 (0)