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 | |
| parent | 2a2e82efeb4e090abf9144b39be834972c7a0a4d (diff) | |
| download | azalea-drasl-e33d57e767a8cc87d9616298c10b374fa0c72efe.tar.xz | |
optimize nbt bytearray
| -rwxr-xr-x | azalea-nbt/benches/my_benchmark.rs | 24 | ||||
| -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 |
4 files changed, 20 insertions, 33 deletions
diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs index ed963fb1..24c4dbda 100755 --- a/azalea-nbt/benches/my_benchmark.rs +++ b/azalea-nbt/benches/my_benchmark.rs @@ -25,13 +25,13 @@ fn bench_serialize(filename: &str, c: &mut Criterion) { group.throughput(Throughput::Bytes(decoded_src.len() as u64)); - group.bench_function("Decode", |b| { - b.iter(|| { - let mut owned_decoded_src_stream = decoded_src_stream.clone(); - owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); - Tag::read(&mut owned_decoded_src_stream).unwrap(); - }) - }); + // group.bench_function("Decode", |b| { + // b.iter(|| { + // let mut owned_decoded_src_stream = decoded_src_stream.clone(); + // owned_decoded_src_stream.seek(SeekFrom::Start(0)).unwrap(); + // Tag::read(&mut owned_decoded_src_stream).unwrap(); + // }) + // }); group.bench_function("Encode", |b| { b.iter(|| { @@ -42,11 +42,11 @@ fn bench_serialize(filename: &str, c: &mut Criterion) { } fn bench(c: &mut Criterion) { - bench_serialize("tests/bigtest.nbt", c); - bench_serialize("tests/simple_player.dat", c); - bench_serialize("tests/complex_player.dat", c); - bench_serialize("tests/level.dat", c); - bench_serialize("tests/stringtest.nbt", c); + // bench_serialize("tests/bigtest.nbt", c); + // bench_serialize("tests/simple_player.dat", c); + // bench_serialize("tests/complex_player.dat", c); + // bench_serialize("tests/level.dat", c); + // bench_serialize("tests/stringtest.nbt", c); bench_serialize("tests/inttest.nbt", c); } 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 |
