mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
optimize nbt bytearray
This commit is contained in:
parent
2a2e82efeb
commit
e33d57e767
4 changed files with 20 additions and 33 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue