Skip to content

Commit

Permalink
reduce allocations in shr
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Feb 8, 2018
1 parent 4714f82 commit e45b2b7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,12 @@ pub fn biguint_shr(n: Cow<BigUint>, bits: usize) -> BigUint {
if n_unit >= n.data.len() {
return Zero::zero();
}
let mut data = match n_unit {
0 => n.into_owned().data,
_ => n.data[n_unit..].to_vec(),
let mut data = match n {
Cow::Borrowed(n) => n.data[n_unit..].to_vec(),
Cow::Owned(mut n) => {
n.data.drain(..n_unit);
n.data
}
};

let n_bits = bits % big_digit::BITS;
Expand Down
3 changes: 2 additions & 1 deletion src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ impl<'a> Shr<usize> for &'a BigUint {
impl ShrAssign<usize> for BigUint {
#[inline]
fn shr_assign(&mut self, rhs: usize) {
*self = biguint_shr(Cow::Borrowed(&*self), rhs);
let n = mem::replace(self, BigUint::zero());
*self = n >> rhs;
}
}

Expand Down

0 comments on commit e45b2b7

Please sign in to comment.