aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--azalea-nbt/benches/my_benchmark.rs2
-rw-r--r--azalea-nbt/src/decode.rs25
-rw-r--r--azalea-nbt/src/tag.rs1
3 files changed, 15 insertions, 13 deletions
diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs
index 3d7eaeee..c77928d8 100644
--- a/azalea-nbt/benches/my_benchmark.rs
+++ b/azalea-nbt/benches/my_benchmark.rs
@@ -38,7 +38,7 @@ fn bench_serialize(filename: &str, c: &mut Criterion) {
}
fn bench(c: &mut Criterion) {
- bench_serialize("tests/bigtest.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);
diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs
index f7cda3f9..90bc25f9 100644
--- a/azalea-nbt/src/decode.rs
+++ b/azalea-nbt/src/decode.rs
@@ -4,6 +4,17 @@ use byteorder::{ReadBytesExt, BE};
use flate2::read::{GzDecoder, ZlibDecoder};
use std::{collections::HashMap, io::Read};
+#[inline]
+fn read_string(stream: &mut impl Read) -> Result<String, Error> {
+ let length = stream.read_u16::<BE>().map_err(|_| Error::InvalidTag)?;
+
+ let mut buf = Vec::with_capacity(length as usize);
+ for _ in 0..length {
+ buf.push(stream.read_u8().map_err(|_| Error::InvalidTag)?);
+ }
+ String::from_utf8(buf).map_err(|_| Error::InvalidTag)
+}
+
impl Tag {
fn read_known(stream: &mut impl Read, id: u8) -> Result<Tag, Error> {
let tag = match id {
@@ -37,14 +48,7 @@ impl Tag {
// A length-prefixed modified UTF-8 string. The prefix is an
// unsigned short (thus 2 bytes) signifying the length of the
// string in bytes
- 8 => {
- let length = stream.read_u16::<BE>().map_err(|_| Error::InvalidTag)?;
- let mut bytes = Vec::with_capacity(length as usize);
- for _ in 0..length {
- bytes.push(stream.read_u8().map_err(|_| Error::InvalidTag)?);
- }
- Tag::String(String::from_utf8(bytes).map_err(|_| Error::InvalidTag)?)
- }
+ 8 => Tag::String(read_string(stream)?),
// A list of nameless tags, all of the same type. The list is
// prefixed with the Type ID of the items it contains (thus 1
// byte), and the length of the list as a signed integer (a further
@@ -71,10 +75,7 @@ impl Tag {
if tag_id == 0 {
break;
}
- let name = match Tag::read_known(stream, 8)? {
- Tag::String(name) => name,
- _ => panic!("Expected a string tag"),
- };
+ let name = read_string(stream)?;
let tag = Tag::read_known(stream, tag_id).map_err(|_| Error::InvalidTag)?;
map.insert(name, tag);
}
diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs
index 53ec6680..3ebf63b6 100644
--- a/azalea-nbt/src/tag.rs
+++ b/azalea-nbt/src/tag.rs
@@ -18,6 +18,7 @@ pub enum Tag {
}
impl Tag {
+ #[inline]
pub fn id(&self) -> u8 {
match self {
Tag::End => 0,