perf(eval): remove unnecessary clones of hands and table
authorMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 19:51:59 +0000 (20:51 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 19:51:59 +0000 (20:51 +0100)
src/evaluator.rs

index b7e8fa2..9b483ae 100644 (file)
@@ -60,46 +60,47 @@ impl Evaluator {
     }
 
     pub fn eval(&self) -> Result<HashMap<usize, u16>, MyError> {
-        fn parse_args(evaluator: &Evaluator) -> Result<(Vec<Vec<u32>>, Vec<u32>), MyError> {
-            let hands: Vec<Vec<u32>> = evaluator
-                .hands
-                .clone()
-                .into_iter()
-                .map(|cards| cards.into_inner())
-                .collect();
+        fn parse_args(evaluator: &Evaluator) -> Result<(), MyError> {
+            let hands = &evaluator.hands;
+            let table = &evaluator.table;
             if hands.is_empty() {
                 return Err(MyError::NoHands);
             }
-            let table = evaluator.table.clone().into_inner();
-            if hands[0].len() == 5 && matches!(evaluator.rules, Rules::Holdem) {
-                println!("Warning: card number in hands does not match the given Poker rules");
-                return Err(MyError::UnmatchedCardsTable);
-            }
-            if hands[0].len() == 2 && matches!(evaluator.rules, Rules::Classic) {
-                println!("Warning: card number in hands does not match the given Poker rules");
-                return Err(MyError::UnmatchedCardsTable);
+            for hand in hands {
+                if hand.len() == 5 && matches!(evaluator.rules, Rules::Holdem) {
+                    println!("Warning: card number in hands does not match the given Poker rules");
+                    return Err(MyError::UnmatchedCardsTable);
+                }
+                if hand.len() == 2 && matches!(evaluator.rules, Rules::Classic) {
+                    println!("Warning: card number in hands does not match the given Poker rules");
+                    return Err(MyError::UnmatchedCardsTable);
+                }
             }
-            if matches!(evaluator.rules, Rules::Holdem) && table.len() == 0 {
+            if matches!(evaluator.rules, Rules::Holdem) && table.is_empty() {
                 return Err(MyError::NoTable);
             }
-            Ok((hands, table))
+            Ok(())
         }
 
-        let (hands, table) = parse_args(self)?;
+        parse_args(&self)?;
+
+        let hands: Vec<Vec<u32>> = self.hands.iter().map(|h| h.clone().into()).collect();
+        let table: Vec<u32> = self.table.clone().into();
 
         let result = match self.rules {
             Rules::Classic => hands
                 .iter()
                 .map(|h| match self.algorithm {
-                    Algorithm::CactusKev => Evaluator::eval_5hand(h.as_slice()).unwrap(),
+                    Algorithm::CactusKev => Evaluator::eval_5hand(h).unwrap(),
                 })
                 .enumerate()
                 .collect(),
             Rules::Holdem => hands
                 .into_iter()
-                .map(|h| match self.algorithm {
+                .map(|mut h| match self.algorithm {
                     Algorithm::CactusKev => {
-                        Evaluator::eval_7hand([h, table.clone()].concat().as_slice()).unwrap()
+                        h.extend(&table);
+                        Evaluator::eval_7hand(&h).unwrap()
                     }
                 })
                 .enumerate()