From f9c28ca5faa5ac9ba78f13e025675d2c56123361 Mon Sep 17 00:00:00 2001 From: urisinger <60300761+urisinger@users.noreply.github.com> Date: Thu, 23 May 2024 05:08:15 +0300 Subject: make getting shapes faster (#149) * supposed to make getting shapes faster * why was this reversed * forgot to run codegen * don't panic when getting the shape for invalid block ids --------- Co-authored-by: mat --- codegen/lib/code/shapes.py | 42 ++++++++++++++++++++++++++---------------- codegen/lib/extract.py | 4 ++-- 2 files changed, 28 insertions(+), 18 deletions(-) (limited to 'codegen/lib') diff --git a/codegen/lib/code/shapes.py b/codegen/lib/code/shapes.py index 6906328c..bedab647 100755 --- a/codegen/lib/code/shapes.py +++ b/codegen/lib/code/shapes.py @@ -73,9 +73,11 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, generated_shape_code += generate_code_for_shape(shape_id, shape) - # 1..100 | 200..300 => &SHAPE1, - generated_match_inner_code = '' - shape_ids_to_block_state_ids = {} + # static SHAPES_MAP: [&Lazy; 26644] = [&SHAPE0, &SHAPE1, &SHAPE1, ...] + empty_shapes = [] + full_shapes = [] + + block_state_ids_to_shape_ids = [] for block_id, shape_ids in blocks.items(): if isinstance(shape_ids, int): shape_ids = [shape_ids] @@ -84,19 +86,24 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report, for possible_state, shape_id in zip(block_report_data['states'], shape_ids): block_state_id = possible_state['id'] - if shape_id not in shape_ids_to_block_state_ids: - shape_ids_to_block_state_ids[shape_id] = [] - shape_ids_to_block_state_ids[shape_id].append(block_state_id) + if shape_id == 0 : + empty_shapes.append(block_state_id) + elif shape_id == 1 : + full_shapes.append(block_state_id) + + block_state_ids_to_shape_ids.append((block_state_id, shape_id)) + + + generated_map_code = f'static SHAPES_MAP: [&Lazy; {len(block_state_ids_to_shape_ids)}] = [' - empty_shape_match_code = convert_ints_to_rust_ranges(shape_ids_to_block_state_ids[0]) - block_shape_match_code = convert_ints_to_rust_ranges(shape_ids_to_block_state_ids[1]) + block_state_ids_to_shape_ids = sorted(block_state_ids_to_shape_ids, key=lambda x: x[0]) - # shape 1 is the most common so we have a _ => &SHAPE1 at the end - del shape_ids_to_block_state_ids[1] + empty_shape_match_code = convert_ints_to_rust_ranges(empty_shapes) + block_shape_match_code = convert_ints_to_rust_ranges(full_shapes) - for shape_id, block_state_ids in shape_ids_to_block_state_ids.items(): - generated_match_inner_code += f'{convert_ints_to_rust_ranges(block_state_ids)} => &SHAPE{shape_id},\n' - generated_match_inner_code += '_ => &SHAPE1' + for block_state_id, shape_id in block_state_ids_to_shape_ids: + generated_map_code += f'&SHAPE{shape_id},\n' + generated_map_code += '];' if empty_shape_match_code == '': print('Error: shape 0 was not found') @@ -126,11 +133,10 @@ pub trait BlockWithShape {{ {generated_shape_code} + impl BlockWithShape for BlockState {{ fn shape(&self) -> &'static VoxelShape {{ - match self.id {{ - {generated_match_inner_code} - }} + SHAPES_MAP.get(self.id as usize).unwrap_or(&&SHAPE1) }} fn is_shape_empty(&self) -> bool {{ @@ -141,8 +147,12 @@ impl BlockWithShape for BlockState {{ matches!(self.id, {block_shape_match_code}) }} }} + +{generated_map_code} ''' + + def generate_code_for_shape(shape_id: str, parts: list[list[float]]): def make_arguments(part: list[float]): diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py index fa75c49e..d3bd46d3 100755 --- a/codegen/lib/extract.py +++ b/codegen/lib/extract.py @@ -236,9 +236,9 @@ def get_pixlyzer_data(version_id: str, category: str): assert pom_xml_dependencies != '' pom_xml = open(f'{pixlyzer_dir}/pom.xml', 'r').read() pom_xml = re.sub( - '.*?', f'{pom_xml_dependencies}', pom_xml, flags=re.DOTALL) + r'.*?', f'{pom_xml_dependencies}', pom_xml, flags=re.DOTALL) pom_xml = re.sub( - '.*?', f'{version_id}', pom_xml, flags=re.DOTALL) + r'.*?', f'{version_id}', pom_xml, flags=re.DOTALL) open(f'{pixlyzer_dir}/pom.xml', 'w').write(pom_xml) # compile -- cgit v1.2.3