Skip to content

Commit

Permalink
refactor: Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Oct 12, 2024
1 parent 5ad8946 commit aac8390
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ cesu8 = "1.1.0"
derive_more = { version = "1.0.0", features = ["into", "from"] }
thiserror = "1.0.63"
serde = { version = "1.0.209", optional = true, features = ["derive"] }
rustc-hash = "2.0.0"

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
Expand Down
23 changes: 15 additions & 8 deletions benches/read.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use bytes::{Bytes, BytesMut};
use crab_nbt::Nbt;
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use flate2::read::GzDecoder;
use std::fs::File;
use std::io::Read;

fn criterion_benchmark(c: &mut Criterion) {
let mut file = File::open("tests/data/complex_player.dat").expect("Failed to open file");
fn decompress_data(file_path: &str) -> Vec<u8> {
let mut file = File::open(file_path).expect("Failed to open file");
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("Failed to read file");
let mut src = &buffer[..];
Expand All @@ -17,17 +17,24 @@ fn criterion_benchmark(c: &mut Criterion) {
input = buffer;
}

input
}

fn criterion_benchmark(c: &mut Criterion) {
let input = decompress_data("tests/data/complex_player.dat");

let bytes = Bytes::from_iter(input);
let bytes_mut = BytesMut::from(bytes);

let mut group = c.benchmark_group("read");
group.throughput(Throughput::Bytes(bytes_mut.len() as u64));

group.bench_function("read_bigtest_nbt", |b| {
b.iter(|| {
let output = Nbt::read(&mut bytes_mut.clone()).expect("Failed to parse NBT");
black_box(output)
})
group.bench_function("read_complex_player_nbt", |b| {
b.iter_batched_ref(
|| bytes_mut.clone(),
|bytes| Nbt::read(bytes).expect("Failed to parse NBT"),
BatchSize::SmallInput,
)
});
}

Expand Down
12 changes: 3 additions & 9 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,9 @@ macro_rules! nbt {
macro_rules! nbt_inner {
({ $($key:tt : $value:tt),* $(,)? }) => {
$crate::NbtCompound::from_iter({
#[allow(unused_mut)]
let mut values = ::std::vec::Vec::<(::std::string::String, $crate::NbtTag)>::new();
$(
let key: ::std::string::String = $key.into();
if !values.iter().any(|(k, _)| k == &key) {
values.push((key, nbt_inner!(@value $value)));
}
)*
values
let mut tags = ::std::vec::Vec::<(::std::string::String, $crate::NbtTag)>::new();
$(tags.push(($key.into(), nbt_inner!(@value $value)));)*
tags
})
};
(@value $ident:ident) => { $crate::NbtTag::from($ident) };
Expand Down
18 changes: 10 additions & 8 deletions src/nbt/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl NbtCompound {
}

pub fn deserialize_content(bytes: &mut impl Buf) -> Result<NbtCompound, Error> {
let mut child_tags = Vec::new();
let mut compound = NbtCompound::new();

while bytes.has_remaining() {
let tag_id = bytes.get_u8();
Expand All @@ -30,15 +30,13 @@ impl NbtCompound {
let name = get_nbt_string(bytes)?;

if let Ok(tag) = NbtTag::deserialize_data(bytes, tag_id) {
if !child_tags.iter().any(|(key, _)| key == &name) {
child_tags.push((name, tag));
}
compound.put(name, tag);
} else {
break;
}
}

Ok(NbtCompound { child_tags })
Ok(compound)
}

pub fn deserialize_content_from_cursor(
Expand All @@ -64,7 +62,9 @@ impl NbtCompound {
}

pub fn put(&mut self, name: String, value: impl Into<NbtTag>) {
self.child_tags.push((name, value.into()));
if !self.child_tags.iter().any(|(key, _)| key == &name) {
self.child_tags.push((name, value.into()));
}
}

pub fn get_byte(&self, name: &str) -> Option<i8> {
Expand Down Expand Up @@ -134,9 +134,11 @@ impl From<Nbt> for NbtCompound {

impl FromIterator<(String, NbtTag)> for NbtCompound {
fn from_iter<T: IntoIterator<Item = (String, NbtTag)>>(iter: T) -> Self {
Self {
child_tags: Vec::from_iter(iter),
let mut compound = NbtCompound::new();
for (key, value) in iter {
compound.put(key, value);
}
compound
}
}

Expand Down

0 comments on commit aac8390

Please sign in to comment.