From 09a70935c7df86548f347209819b2a4442e24936 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 21:25:00 +0800 Subject: [PATCH] perf: AllCases module of rust ffi --- src/cli/src/main.rs | 25 ++++++- src/rust_ffi/src/all_cases.rs | 132 +++++++++++++++++++++++++++++----- src/rust_ffi/src/lib.rs | 2 +- 3 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index bd245b9..5c79122 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,5 +1,10 @@ extern crate klotski_ffi; +use klotski_ffi::AllCases; + +// use std::thread::sleep; +// use std::time::Duration; + fn main() { // println!("{:#?}", klotski_ffi::metadata()); @@ -40,9 +45,23 @@ fn main() { // println!("{} => {}", r, r.to_common_code()); - let all_cases = klotski_ffi::all_cases_fetch(); - for &code in &all_cases[..16] { - println!("{:09X}", code); + // let all_cases = klotski_ffi::all_cases_fetch(); + // for &code in &all_cases[..16] { + // println!("{:09X}", code); + // } + + for raw_code in &AllCases::raw_codes()[..8] { + println!("{}", raw_code); } + for short_code in &AllCases::short_codes()[..8] { + println!("{}", short_code); + } + for common_code in &AllCases::common_codes()[..8] { + println!("{}", common_code); + } + + // loop { + // sleep(Duration::from_secs(1)); + // } } diff --git a/src/rust_ffi/src/all_cases.rs b/src/rust_ffi/src/all_cases.rs index 4668c0e..5cf6dcc 100644 --- a/src/rust_ffi/src/all_cases.rs +++ b/src/rust_ffi/src/all_cases.rs @@ -1,26 +1,103 @@ use crate::core::Core; +use crate::{RawCode, ShortCode, CommonCode}; + +#[derive(Debug)] +pub struct AllCases {} lazy_static! { static ref ALL_CASES: Vec = build_all_cases(); static ref BASIC_RANGES: Vec = build_basic_ranges(); -} -#[inline] -pub fn all_cases_fetch() -> &'static Vec { - &(*ALL_CASES) + static ref ALL_RAW_CODES: Vec = { + build_all_cases().iter() + .map(|code| CommonCode::new(*code).to_raw_code()) + .collect() + }; + + static ref ALL_COMMON_CODES: Vec = { + build_all_cases().iter() + .map(|code| CommonCode::new(*code)) + .collect() + }; + + static ref ALL_SHORT_CODES: Vec = { + ShortCode::warm_up_fast(); + build_all_cases().iter() + .map(|code| CommonCode::new(*code).to_short_code()) + .collect() + }; } -#[inline] -#[allow(dead_code)] -pub fn basic_ranges_fetch() -> &'static Vec { - &(*BASIC_RANGES) +impl AllCases { + /// Get all possible layouts and return an vector loaded with raw + /// `u64` common code. + #[inline] + #[allow(dead_code)] + pub(crate) fn all_cases() -> &'static Vec { + &(*ALL_CASES) + } + + /// Get all possible base ranges with `u32` value. + #[inline] + #[allow(dead_code)] + pub(crate) fn basic_ranges() -> &'static Vec { + &(*BASIC_RANGES) + } + + /// Get all possible layouts and return an vector loaded with RawCode. + /// # Example + /// ``` + /// use klotski_ffi::AllCases; + /// + /// let all_raw_codes = AllCases::raw_codes(); + /// + /// for raw_code in &all_raw_codes[..16] { + /// println!("{}", raw_code); + /// } + /// ``` + #[inline] + pub fn raw_codes() -> &'static Vec { + &(*ALL_RAW_CODES) + } + + /// Get all possible layouts and return an vector loaded with ShortCode. + /// # Example + /// ``` + /// use klotski_ffi::AllCases; + /// + /// let all_short_codes = AllCases::short_codes(); + /// + /// for short_code in &all_short_codes[..16] { + /// println!("{}", short_code); + /// } + /// ``` + #[inline] + pub fn short_codes() -> &'static Vec { + &(*ALL_SHORT_CODES) + } + + /// Get all possible layouts and return an vector loaded with CommonCode. + /// # Example + /// ``` + /// use klotski_ffi::AllCases; + /// + /// let all_common_codes = AllCases::common_codes(); + /// + /// for common_code in &all_common_codes[..16] { + /// println!("{}", common_code); + /// } + /// ``` + #[inline] + pub fn common_codes() -> &'static Vec { + &(*ALL_COMMON_CODES) + } } fn build_all_cases() -> Vec { unsafe { Core::all_cases_build(); let mut buffer: Vec = vec![0; Core::ALL_CASES_SIZE as usize]; - Core::export_all_cases(&mut buffer[0]); + Core::export_all_cases(buffer.as_mut_ptr()); buffer } } @@ -29,49 +106,70 @@ fn build_basic_ranges() -> Vec { unsafe { Core::basic_ranges_build(); let mut buffer: Vec = vec![0; Core::BASIC_RANGES_SIZE as usize]; - Core::export_basic_ranges(&mut buffer[0]); + Core::export_basic_ranges(buffer.as_mut_ptr()); buffer } } #[cfg(test)] mod tests { + use super::AllCases; use crypto::md5::Md5; use crypto::digest::Digest; - use crate::all_cases::{ALL_CASES, all_cases_fetch, BASIC_RANGES, basic_ranges_fetch}; + use super::{ALL_CASES, BASIC_RANGES}; + use super::{ALL_RAW_CODES, ALL_SHORT_CODES, ALL_COMMON_CODES}; const ALL_CASES_MD5: &str = "3888e9fab8d3cbb50908b12b147cfb23"; const BASIC_RANGES_MD5: &str = "6f385dc171e201089ff96bb010b47212"; #[test] - fn all_cases() { - assert_eq!(&(*ALL_CASES), all_cases_fetch()); + fn raw_codes() { + let tmp: Vec = ALL_RAW_CODES.iter() + .map(|code| code.to_common_code().unwrap()) + .collect(); + assert_eq!(tmp, *ALL_CASES); + assert_eq!(&*ALL_RAW_CODES, AllCases::raw_codes()); + } + + #[test] + fn short_codes() { + let tmp: Vec = ALL_SHORT_CODES.iter() + .map(|code| code.to_common_code().unwrap()) + .collect(); + assert_eq!(tmp, *ALL_CASES); + assert_eq!(&*ALL_SHORT_CODES, AllCases::short_codes()); } #[test] - fn basic_ranges() { - assert_eq!(&(*BASIC_RANGES), basic_ranges_fetch()); + fn common_codes() { + let tmp: Vec = ALL_COMMON_CODES.iter() + .map(|code| code.unwrap()) + .collect(); + assert_eq!(tmp, *ALL_CASES); + assert_eq!(&*ALL_COMMON_CODES, AllCases::common_codes()); } #[test] fn all_cases_data() { let mut md5 = Md5::new(); let mut tmp = String::new(); - for &common_code in &(*ALL_CASES) { + for &common_code in &*ALL_CASES { tmp.push_str(&format!("{:09X}\n", common_code)); } md5.input_str(&tmp); assert_eq!(md5.result_str(), ALL_CASES_MD5); + assert_eq!(&*ALL_CASES, AllCases::all_cases()); } #[test] fn basic_ranges_data() { let mut md5 = Md5::new(); let mut tmp = String::new(); - for &range in &(*BASIC_RANGES) { + for &range in &*BASIC_RANGES { tmp.push_str(&format!("{:08X}\n", range)); } md5.input_str(&tmp); assert_eq!(md5.result_str(), BASIC_RANGES_MD5); + assert_eq!(&*BASIC_RANGES, AllCases::basic_ranges()); } } diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index fb3e24e..f809766 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -10,6 +10,6 @@ pub use codec::RawCode; pub use codec::ShortCode; pub use codec::CommonCode; -pub use all_cases::all_cases_fetch; +pub use all_cases::AllCases; pub use metadata::load_metadata as metadata;