diff options
| author | mat <github@matdoes.dev> | 2022-08-24 21:02:11 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-08-24 21:02:11 -0500 |
| commit | e33d57e767a8cc87d9616298c10b374fa0c72efe (patch) | |
| tree | cf36006b00e70a0a17cf45c54cc3948d9c06eea1 /azalea-nbt/src | |
| parent | 2a2e82efeb4e090abf9144b39be834972c7a0a4d (diff) | |
| download | azalea-drasl-e33d57e767a8cc87d9616298c10b374fa0c72efe.tar.xz | |
optimize nbt bytearray
Diffstat (limited to 'azalea-nbt/src')
| -rwxr-xr-x | azalea-nbt/src/decode.rs | 6 | ||||
| -rwxr-xr-x | azalea-nbt/src/encode.rs | 21 | ||||
| -rwxr-xr-x | azalea-nbt/src/tag.rs | 2 |
3 files changed, 8 insertions, 21 deletions
diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index 5164bb0f..d4760807 100755 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -40,10 +40,8 @@ impl Tag { // integer (thus 4 bytes) 7 => { let length = stream.read_i32::<BE>()?; - let mut bytes = Vec::with_capacity(length as usize); - for _ in 0..length { - bytes.push(stream.read_i8()?); - } + let mut bytes = vec![0; length as usize]; + stream.read_exact(&mut bytes)?; Tag::ByteArray(bytes) } // A length-prefixed modified UTF-8 string. The prefix is an diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index 1bb8f366..53e618f4 100755 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -58,10 +58,7 @@ fn write_compound( Tag::ByteArray(value) => { writer.write_u8(7)?; write_string(writer, key)?; - writer.write_i32::<BE>(value.len() as i32)?; - for &byte in value { - writer.write_i8(byte)?; - } + write_bytearray(writer, value)? } Tag::String(value) => { writer.write_u8(8)?; @@ -81,18 +78,12 @@ fn write_compound( Tag::IntArray(value) => { writer.write_u8(11)?; write_string(writer, key)?; - writer.write_i32::<BE>(value.len() as i32)?; - for &int in value { - writer.write_i32::<BE>(int)?; - } + write_intarray(writer, value)? } Tag::LongArray(value) => { writer.write_u8(12)?; write_string(writer, key)?; - writer.write_i32::<BE>(value.len() as i32)?; - for &long in value { - writer.write_i64::<BE>(long)?; - } + write_longarray(writer, value)? } } } @@ -150,11 +141,9 @@ fn write_list(writer: &mut dyn Write, value: &[Tag]) -> Result<(), Error> { } #[inline] -fn write_bytearray(writer: &mut dyn Write, value: &Vec<i8>) -> Result<(), Error> { +fn write_bytearray(writer: &mut dyn Write, value: &Vec<u8>) -> Result<(), Error> { writer.write_i32::<BE>(value.len() as i32)?; - for &byte in value { - writer.write_i8(byte)?; - } + writer.write_all(value)?; Ok(()) } diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index 1b96a8cb..0e3ce08e 100755 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -9,7 +9,7 @@ pub enum Tag { Long(i64), // 4 Float(f32), // 5 Double(f64), // 6 - ByteArray(Vec<i8>), // 7 + ByteArray(Vec<u8>), // 7 String(String), // 8 List(Vec<Tag>), // 9 Compound(AHashMap<String, Tag>), // 10 |
