From d20940f89b0554010ec38350ba53468f34df2d6f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 20 Aug 2023 15:37:23 +0200 Subject: [PATCH] Improve Performance --- schemsearch-lib/src/lib.rs | 29 +++++++++++++-------------- schemsearch-lib/src/pattern_mapper.rs | 4 ++-- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/schemsearch-lib/src/lib.rs b/schemsearch-lib/src/lib.rs index de9720a..d3edec1 100644 --- a/schemsearch-lib/src/lib.rs +++ b/schemsearch-lib/src/lib.rs @@ -39,30 +39,30 @@ pub fn search( search_behavior: SearchBehavior, ) -> Vec { if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length { - return vec![]; + return Vec::new(); } if pattern_schem.palette.len() > schem.palette.len() { - return vec![]; + return Vec::new(); } let pattern_schem = match_palette(&schem, &pattern_schem, search_behavior.ignore_block_data); - let mut matches: Vec = Vec::new(); + let mut matches: Vec = Vec::with_capacity(4); - let pattern_data = pattern_schem.block_data.as_slice(); + let pattern_data = pattern_schem.block_data.as_ptr(); let schem_data = if search_behavior.ignore_block_data { match_palette_adapt(&schem, &pattern_schem.palette, search_behavior.ignore_block_data) } else { - schem.block_data.clone() + schem.block_data }; - let schem_data = schem_data.as_slice(); + let schem_data = schem_data.as_ptr(); let air_id = if search_behavior.ignore_air || search_behavior.air_as_any { pattern_schem.palette.get("minecraft:air").unwrap_or(&-1) } else { &-1}; - let pattern_blocks = pattern_data.len() as f32; + let pattern_blocks = pattern_schem.block_data.len() as f32; let i_pattern_blocks = pattern_blocks as i32; let pattern_width = pattern_schem.width as usize; @@ -86,16 +86,16 @@ pub fn search( for i in 0..pattern_width { let index = (x + i) + schem_width * ((z + k) + (y + j) * schem_length); let pattern_index = i + pattern_width * (k + j * pattern_length); - let data = unsafe {schem_data.get_unchecked(index) }; - let pattern_data = unsafe { pattern_data.get_unchecked(pattern_index) }; - if (search_behavior.ignore_air && *data != *air_id) || (search_behavior.air_as_any && *pattern_data != *air_id) { + let data = unsafe { *schem_data.add(index) }; + let pattern_data = unsafe { *pattern_data.add(pattern_index) }; + if (search_behavior.ignore_air && data != *air_id) || (search_behavior.air_as_any && pattern_data != *air_id) { continue 'inner; } - if *data != *pattern_data { + if data != pattern_data { not_matching += 1; - } - if not_matching >= skip_amount { - break 'outer; + if not_matching >= skip_amount { + break 'outer; + } } } } @@ -109,7 +109,6 @@ pub fn search( percent: (i_pattern_blocks - not_matching) as f32 / pattern_blocks, }); } - } } } diff --git a/schemsearch-lib/src/pattern_mapper.rs b/schemsearch-lib/src/pattern_mapper.rs index e554d4c..fd34a08 100644 --- a/schemsearch-lib/src/pattern_mapper.rs +++ b/schemsearch-lib/src/pattern_mapper.rs @@ -64,10 +64,10 @@ pub fn strip_data(schem: &SpongeSchematic) -> SpongeSchematic { } pub fn match_palette_adapt(schem: &SpongeSchematic, matching_palette: &HashMap, ignore_data: bool) -> Vec { - let mut data: Vec = Vec::new(); + let mut data = Vec::with_capacity(schem.block_data.len()); let reverse_palette = create_reverse_palette(schem); - for x in schem.block_data.iter() { + for x in schem.block_data.as_slice().iter() { let blockname = reverse_palette[*x as usize]; let blockname = if ignore_data { normalize_data(blockname, ignore_data) } else { blockname }; let block_id = match matching_palette.get(&*blockname) {