From 8643489096af2ce3e5440cf10bccb9b37cc901a0 Mon Sep 17 00:00:00 2001 From: nicoo Date: Wed, 29 Jul 2020 20:45:45 +0200 Subject: [PATCH] factor::Factors: Use a RefCell rather than copy data when printing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ~2.9% faster than the previous commit, ~11% faster than “master” overall. --- src/uu/factor/src/factor.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 07cad8434..4e5322084 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -7,6 +7,7 @@ extern crate rand; +use std::cell::RefCell; use std::fmt; use crate::numeric::{Arithmetic, Montgomery}; @@ -64,16 +65,16 @@ impl PartialEq for Decomposition { impl Eq for Decomposition {} #[derive(Clone, Debug, Eq, PartialEq)] -pub struct Factors(Decomposition); +pub struct Factors(RefCell); impl Factors { pub fn one() -> Factors { - Factors(Decomposition::one()) + Factors(RefCell::new(Decomposition::one())) } pub fn add(&mut self, prime: u64, exp: Exponent) { debug_assert!(miller_rabin::is_prime(prime)); - self.0.add(prime, exp) + self.0.borrow_mut().add(prime, exp) } pub fn push(&mut self, prime: u64) { @@ -82,13 +83,13 @@ impl Factors { #[cfg(test)] fn product(&self) -> u64 { - self.0.product() + self.0.borrow().product() } } impl fmt::Display for Factors { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut v = (self.0).0.clone(); + let v = &mut (self.0).borrow_mut().0; v.sort_unstable(); for (p, exp) in v.iter() {