From 2c9e408753f0a792062ac129d1dc6e032fbf4881 Mon Sep 17 00:00:00 2001 From: MA Beaudet Date: Fri, 5 Nov 2021 10:59:11 +0100 Subject: [PATCH] fix: convert unnecessary u32 to u16 and u8 Debug builds are now 2x faster, release builds were already optimized --- src/constants.rs | 30 +++++++++++++++--------------- src/lib.rs | 15 ++++++++------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 419a1c5..97a288b 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,14 +1,14 @@ -pub const PRIMES: [u32; 13] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]; +pub const PRIMES: [u8; 13] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41]; -pub const STRAIGHT_FLUSH: u32 = 1; -pub const FOUR_OF_A_KIND: u32 = 2; -pub const FULL_HOUSE: u32 = 3; -pub const FLUSH: u32 = 4; -pub const STRAIGHT: u32 = 5; -pub const THREE_OF_A_KIND: u32 = 6; -pub const TWO_PAIR: u32 = 7; -pub const ONE_PAIR: u32 = 8; -pub const HIGH_CARD: u32 = 9; +pub const STRAIGHT_FLUSH: u8 = 1; +pub const FOUR_OF_A_KIND: u8 = 2; +pub const FULL_HOUSE: u8 = 3; +pub const FLUSH: u8 = 4; +pub const STRAIGHT: u8 = 5; +pub const THREE_OF_A_KIND: u8 = 6; +pub const TWO_PAIR: u8 = 7; +pub const ONE_PAIR: u8 = 8; +pub const HIGH_CARD: u8 = 9; pub const VALUE_STR: [&str; 10] = [ "", @@ -23,7 +23,7 @@ pub const VALUE_STR: [&str; 10] = [ "High Card", ]; -pub const FLUSHES: [u32; 7937] = [ +pub const FLUSHES: [u16; 7937] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1599, 0, 0, 0, 0, 0, 0, 0, 1598, 0, 0, 0, 1597, 0, 1596, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1595, 0, 0, 0, 0, 0, 0, 0, 1594, 0, 0, @@ -310,7 +310,7 @@ pub const FLUSHES: [u32; 7937] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ]; -pub const UNIQUE5: [u32; 7937] = [ +pub const UNIQUE5: [u16; 7937] = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1608, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7462, 0, 0, 0, 0, 0, 0, 0, 7461, 0, 0, 0, 7460, 0, 7459, 1607, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7458, 0, 0, 0, 0, 0, 0, 0, @@ -604,7 +604,7 @@ pub const UNIQUE5: [u32; 7937] = [ 0, 0, 0, 0, 0, 1600, ]; -pub const HASH_ADJUST: [u32; 512] = [ +pub const HASH_ADJUST: [u16; 512] = [ 0, 5628, 7017, 1298, 2918, 2442, 8070, 6383, 6383, 7425, 2442, 5628, 8044, 7425, 3155, 6383, 2918, 7452, 1533, 6849, 5586, 7452, 7452, 1533, 2209, 6029, 2794, 3509, 7992, 7733, 7452, 131, 6029, 4491, 1814, 7452, 6110, 3155, 7077, 6675, 532, 1334, 7555, 5325, 3056, 1403, 1403, 3969, @@ -639,7 +639,7 @@ pub const HASH_ADJUST: [u32; 512] = [ 5628, 7017, 8029, 6528, 4474, 6322, 5562, 6669, 4610, 7006, ]; -pub const HASH_VALUES: [u32; 8192] = [ +pub const HASH_VALUES: [u16; 8192] = [ 148, 2934, 166, 5107, 4628, 166, 166, 166, 166, 3033, 166, 4692, 166, 5571, 2225, 166, 5340, 3423, 166, 3191, 1752, 166, 5212, 166, 166, 3520, 166, 166, 166, 1867, 166, 3313, 166, 3461, 166, 166, 3174, 1737, 5010, 5008, 166, 4344, 2868, 3877, 166, 4089, 166, 5041, 4748, 4073, @@ -1127,7 +1127,7 @@ pub const HASH_VALUES: [u32; 8192] = [ 4767, 5829, 2925, 5916, 2133, 166, ]; -pub const PERM7: [[u32; 5]; 21] = [ +pub const PERM7: [[u16; 5]; 21] = [ [0, 1, 2, 3, 4], [0, 1, 2, 3, 5], [0, 1, 2, 3, 6], diff --git a/src/lib.rs b/src/lib.rs index 99e31af..fa4fa09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,7 @@ pub fn init_deck() -> Vec { let mut suit = 0x8000; for _ in 0..4 { for (j, prime) in PRIMES.iter().enumerate() { + let prime = *prime as u32; deck.push(prime | (u32::try_from(j).unwrap() << 8) | suit | (1 << (16 + j))); } suit >>= 1; @@ -105,12 +106,12 @@ pub fn card_from_str(input: &str) -> u32 { 's' => 0x1000, _ => panic!("{:?} is an invalid suit character", s), }; - PRIMES[r] | (u32::try_from(r).unwrap() << 8) | s | (1 << (16 + r)) + PRIMES[r] as u32 | (u32::try_from(r).unwrap() << 8) | s | (1 << (16 + r)) } /// Performs a perfect hash lookup /// -fn find_fast(mut u: u32) -> u32 { +fn find_fast(mut u: u32) -> u16 { u += 0xe91aaa35; u ^= u >> 16; // u += u << 8; @@ -119,7 +120,7 @@ fn find_fast(mut u: u32) -> u32 { let b = (u >> 8) & 0x1ff; // let a = (u + (u << 2)) >> 19; let a = (u.wrapping_add(u << 2)) >> 19; - a ^ HASH_ADJUST[b as usize] + u16::try_from(a).unwrap() ^ HASH_ADJUST[b as usize] } /// Wrapper over both five-card and seven-card hand evaluator from string. @@ -135,7 +136,7 @@ fn find_fast(mut u: u32) -> u32 { /// assert_eq!(eval_from_str("Ts 9s 8s 7s 6s 7h 6d"), 5); /// ``` #[wasm_bindgen] -pub fn eval_from_str(s: &str) -> u32 { +pub fn eval_from_str(s: &str) -> u16 { let cards = s.split(' ').map(|c| card_from_str(c)).collect::>(); match cards.len() { 7 => eval_7hand(&cards.try_into().unwrap()), @@ -149,7 +150,7 @@ pub fn eval_from_str(s: &str) -> u32 { /// Returns the hand's equivalence value. /// The evaluator orders hands from 1 to 7462. /// You can use `hand_rank` to get the hand category. -pub fn eval_5hand(hand: &[u32; 5]) -> u32 { +pub fn eval_5hand(hand: &[u32; 5]) -> u16 { let [c1, c2, c3, c4, c5] = hand; let mut q = (c1 | c2 | c3 | c4 | c5) >> 16; if c1 & c2 & c3 & c4 & c5 & 0xF000 != 0 { @@ -163,7 +164,7 @@ pub fn eval_5hand(hand: &[u32; 5]) -> u32 { } /// Non-optimized method of determining the best five-card hand possible of seven cards. -pub fn eval_7hand(hand: &[u32; 7]) -> u32 { +pub fn eval_7hand(hand: &[u32; 7]) -> u16 { let mut subhand: [u32; 5] = [0; 5]; let mut best = 9999; @@ -191,7 +192,7 @@ pub fn eval_7hand(hand: &[u32; 7]) -> u32 { /// assert_eq!(hand_rank(eval), FLUSH); /// ``` #[wasm_bindgen] -pub fn hand_rank(i: u32) -> u32 { +pub fn hand_rank(i: u16) -> u8 { match i { 6186..=7462 => HIGH_CARD, 3326..=6185 => ONE_PAIR, -- 2.20.1