From 922701104ab432eea6185a1a5e96211eaeba5bda Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 28 Dec 2023 17:46:24 -0600 Subject: [PATCH] add NbtTag::read_optional --- simdnbt/Cargo.toml | 2 ++ simdnbt/benches/nbt.rs | 5 ++++- simdnbt/src/borrow/mod.rs | 8 ++++++++ simdnbt/src/owned/mod.rs | 8 ++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/simdnbt/Cargo.toml b/simdnbt/Cargo.toml index 990a813..3b31fde 100644 --- a/simdnbt/Cargo.toml +++ b/simdnbt/Cargo.toml @@ -23,6 +23,8 @@ fastnbt = "2.4.4" azalea-nbt = { git = "https://github.com/azalea-rs/azalea", rev = "84e036ce3752ecf57904b0f5aff1f33d43e95a32" } hematite-nbt = { version = "0.5.2", default-features = false } +tikv-jemallocator = "0.5" + [features] default = ["derive"] derive = ["dep:simdnbt-derive"] diff --git a/simdnbt/benches/nbt.rs b/simdnbt/benches/nbt.rs index 07956d9..cfe4e0b 100644 --- a/simdnbt/benches/nbt.rs +++ b/simdnbt/benches/nbt.rs @@ -46,8 +46,11 @@ fn bench_file(filename: &str, c: &mut Criterion) { group.finish(); } +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + fn bench(c: &mut Criterion) { - // bench_file("bigtest.nbt", c); + bench_file("bigtest.nbt", c); // bench_file("simple_player.dat", c); bench_file("complex_player.dat", c); // bench_file("level.dat", c); diff --git a/simdnbt/src/borrow/mod.rs b/simdnbt/src/borrow/mod.rs index dbed7ca..6d6a262 100644 --- a/simdnbt/src/borrow/mod.rs +++ b/simdnbt/src/borrow/mod.rs @@ -170,6 +170,14 @@ impl<'a> NbtTag<'a> { Self::read_with_type(data, tag_type, 0) } + pub fn read_optional(data: &mut Cursor<&'a [u8]>) -> Result, Error> { + let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?; + if tag_type == END_ID { + return Ok(None); + } + Ok(Some(Self::read_with_type(data, tag_type, 0)?)) + } + pub fn byte(&self) -> Option { match self { NbtTag::Byte(byte) => Some(*byte), diff --git a/simdnbt/src/owned/mod.rs b/simdnbt/src/owned/mod.rs index 1e77326..6e58da5 100644 --- a/simdnbt/src/owned/mod.rs +++ b/simdnbt/src/owned/mod.rs @@ -264,6 +264,14 @@ impl NbtTag { Self::read_with_type(data, tag_type, 0) } + pub fn read_optional(data: &mut Cursor<&[u8]>) -> Result, Error> { + let tag_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?; + if tag_type == END_ID { + return Ok(None); + } + Ok(Some(Self::read_with_type(data, tag_type, 0)?)) + } + /// Write to the data without checking that there's enough space in it. /// /// # Safety