diff options
| author | mat <github@matdoes.dev> | 2022-09-18 23:13:45 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-09-18 23:13:45 -0500 |
| commit | 4247945df13507fb07a4746263bb702d6fbe04cd (patch) | |
| tree | d47d27704359939c7fa389e79ae0cb0aba8a0abd | |
| parent | 79cf19f93e273aa5421e2cc46fee36cc64e7731d (diff) | |
| download | azalea-drasl-4247945df13507fb07a4746263bb702d6fbe04cd.tar.xz | |
start work on optimizing block macros
| -rw-r--r-- | azalea-block/azalea-block-macros/src/lib.rs | 77 | ||||
| -rw-r--r-- | azalea-block/src/lib.rs | 6 | ||||
| -rw-r--r-- | azalea-core/src/bitset.rs | 50 |
3 files changed, 98 insertions, 35 deletions
diff --git a/azalea-block/azalea-block-macros/src/lib.rs b/azalea-block/azalea-block-macros/src/lib.rs index ac61912f..85887418 100644 --- a/azalea-block/azalea-block-macros/src/lib.rs +++ b/azalea-block/azalea-block-macros/src/lib.rs @@ -426,45 +426,45 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { quote! { BlockState::#block_name_pascal_case } }; - let block_struct = quote! { - #[derive(Debug)] - pub struct #block_struct_name { - #block_struct_fields - } + let block_struct = quote! { + #[derive(Debug, Copy, Clone)] + pub struct #block_struct_name { + #block_struct_fields + } - impl Block for #block_struct_name { - fn behavior(&self) -> BlockBehavior { - #block_behavior - } - fn id(&self) -> &'static str { - #block_id - } + impl Block for #block_struct_name { + fn behavior(&self) -> BlockBehavior { + #block_behavior + } + fn id(&self) -> &'static str { + #block_id } + } - impl From<#block_struct_name> for BlockState { - fn from(b: #block_struct_name) -> Self { - #from_block_to_state_match - } + impl From<#block_struct_name> for BlockState { + fn from(b: #block_struct_name) -> Self { + #from_block_to_state_match } + } - impl Default for #block_struct_name { - fn default() -> Self { - Self { - #block_default_fields - } + impl Default for #block_struct_name { + fn default() -> Self { + Self { + #block_default_fields } } - }; + } + }; - block_structs.extend(block_struct); - } + block_structs.extend(block_struct); + } let last_state_id = (state_id - 1) as u32; let mut generated = quote! { #property_enums #[repr(u32)] - #[derive(Copy, Clone, PartialEq, Eq, Debug)] + #[derive(Copy, Clone, PartialEq, Eq)] pub enum BlockState { #block_state_enum_variants } @@ -476,21 +476,28 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { #last_state_id } } + + impl std::fmt::Debug for BlockState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // having a big match statement here would take up 700kb + f.write_str("BlockState") + } + } }; - generated.extend(quote! { - #block_structs + generated.extend(quote! { + #block_structs - impl From<BlockState> for Box<dyn Block> { - fn from(b: BlockState) -> Self { - let b = b as usize; - match b { - #from_state_to_block_match - _ => panic!("Invalid block state: {}", b), - } + impl From<BlockState> for Box<dyn Block> { + fn from(b: BlockState) -> Self { + let b = b as usize; + match b { + #from_state_to_block_match + _ => panic!("Invalid block state: {}", b), } } - }); + } + }); generated.into() } diff --git a/azalea-block/src/lib.rs b/azalea-block/src/lib.rs index fdcfe631..969288f5 100644 --- a/azalea-block/src/lib.rs +++ b/azalea-block/src/lib.rs @@ -63,4 +63,10 @@ mod tests { assert!(BlockState::try_from(BlockState::max_state()).is_ok()); assert!(BlockState::try_from(BlockState::max_state() + 1).is_err()); } + + #[test] + fn test_from_blockstate() { + let box_block: Box<dyn Block> = Box::<dyn Block>::from(BlockState::Air); + assert_eq!(box_block.id(), "air"); + } } diff --git a/azalea-core/src/bitset.rs b/azalea-core/src/bitset.rs index 2ffd5657..6f0a27ca 100644 --- a/azalea-core/src/bitset.rs +++ b/azalea-core/src/bitset.rs @@ -7,6 +7,8 @@ pub struct BitSet { data: Vec<u64>, } +const ADDRESS_BITS_PER_WORD: usize = 6; + // the Index trait requires us to return a reference, but we can't do that impl BitSet { pub fn new(size: usize) -> Self { @@ -18,6 +20,54 @@ impl BitSet { pub fn index(&self, index: usize) -> bool { (self.data[index / 64] & (1u64 << (index % 64))) != 0 } + + // private static int wordIndex(int bitIndex) { + // return bitIndex >> ADDRESS_BITS_PER_WORD; + // } + pub fn word_index(bit_index: usize) -> usize { + bit_index >> ADDRESS_BITS_PER_WORD + } + + pub fn clear_from_to(&mut self, from: usize, to: usize) { + assert!(from <= to); + assert!(to <= self.data.len() * 64); + assert!(to > 0); + + if from == to { + return; + } + + // int startWordIndex = wordIndex(fromIndex); + // if (startWordIndex >= wordsInUse) + // return; + + // int endWordIndex = wordIndex(toIndex - 1); + // if (endWordIndex >= wordsInUse) { + // toIndex = length(); + // endWordIndex = wordsInUse - 1; + // } + + // long firstWordMask = WORD_MASK << fromIndex; + // long lastWordMask = WORD_MASK >>> -toIndex; + // if (startWordIndex == endWordIndex) { + // // Case 1: One word + // words[startWordIndex] &= ~(firstWordMask & lastWordMask); + // } else { + // // Case 2: Multiple words + // // Handle first word + // words[startWordIndex] &= ~firstWordMask; + + // // Handle intermediate words, if any + // for (int i = startWordIndex+1; i < endWordIndex; i++) + // words[i] = 0; + + // // Handle last word + // words[endWordIndex] &= ~lastWordMask; + // } + + // recalculateWordsInUse(); + // checkInvariants(); + } } impl McBufReadable for BitSet { |
