mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
simplify nbt list optimization
This commit is contained in:
parent
7df6522489
commit
99652200aa
4 changed files with 50 additions and 27 deletions
|
@ -1,6 +1,3 @@
|
|||
# Azalea NBT
|
||||
|
||||
Deserialize Minecraft NBT. This is somewhat based on [Hermatite NBT](https://github.com/PistonDevelopers/hematite_nbt).
|
||||
|
||||
|
||||
|
||||
A fast NBT serializer and deserializer.
|
||||
|
|
|
@ -25,17 +25,18 @@ fn bench_serialize(filename: &str, c: &mut Criterion) {
|
|||
.block_on(async { Tag::read(&mut decoded_src_stream).await.unwrap() });
|
||||
|
||||
let mut group = c.benchmark_group(filename);
|
||||
group.sample_size(1000);
|
||||
|
||||
group.throughput(Throughput::Bytes(decoded_src.len() as u64));
|
||||
|
||||
group.bench_function("Decode", |b| {
|
||||
b.to_async(tokio::runtime::Runtime::new().unwrap())
|
||||
.iter(|| async {
|
||||
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).await.unwrap();
|
||||
})
|
||||
});
|
||||
// group.bench_function("Decode", |b| {
|
||||
// b.to_async(tokio::runtime::Runtime::new().unwrap())
|
||||
// .iter(|| async {
|
||||
// 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).await.unwrap();
|
||||
// })
|
||||
// });
|
||||
|
||||
group.bench_function("Encode", |b| {
|
||||
b.iter(|| {
|
||||
|
|
|
@ -54,29 +54,27 @@ impl Tag {
|
|||
match first_tag {
|
||||
Self::Int(_) => {
|
||||
for i in value {
|
||||
if let Tag::Int(v) = i {
|
||||
writer.write_i32::<BE>(*v)?
|
||||
} else {
|
||||
panic!("List of Ints should only contain Ints")
|
||||
}
|
||||
writer.write_i32::<BE>(
|
||||
*i.as_int().expect("List of Int should only contains Int"),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
Self::String(_) => {
|
||||
for i in value {
|
||||
if let Tag::String(v) = i {
|
||||
write_string(writer, v)?;
|
||||
} else {
|
||||
panic!("List of Strings should only contain Strings")
|
||||
}
|
||||
write_string(
|
||||
writer,
|
||||
i.as_string()
|
||||
.expect("List of String should only contain String"),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
&Self::Compound(_) => {
|
||||
for i in value {
|
||||
if let Tag::Compound(v) = i {
|
||||
write_compound(writer, v)?;
|
||||
} else {
|
||||
panic!("List of Compounds should only contain Compounds")
|
||||
}
|
||||
write_compound(
|
||||
writer,
|
||||
i.as_compound()
|
||||
.expect("List of Compound should only contain Compound"),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
|
|
|
@ -36,4 +36,31 @@ impl Tag {
|
|||
Tag::LongArray(_) => 12,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_int(&self) -> Option<&i32> {
|
||||
if let Tag::Int(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_string(&self) -> Option<&str> {
|
||||
if let Tag::String(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_compound(&self) -> Option<&HashMap<String, Tag>> {
|
||||
if let Tag::Compound(v) = self {
|
||||
Some(v)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue