Improve valence_nbt write perf and remove zerocopy dependency (#174)

This commit is contained in:
Ryan Johnson 2022-12-20 20:17:07 -08:00 committed by GitHub
parent 4194acaa67
commit 2fc59d90a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 8 deletions

View file

@ -14,7 +14,6 @@ edition = "2021"
byteorder = "1.4.3"
cesu8 = "1.1.0"
indexmap = { version = "1.9.1", optional = true }
zerocopy = "0.6.1"
[features]
# When enabled, the order of fields in compounds are preserved.

View file

@ -83,15 +83,15 @@ pub fn encoded_len(text: &str) -> usize {
while i < bytes.len() {
match bytes[i] {
0 => {
n += 2;
i += 1;
}
// Fast path for ASCII here makes a huge difference in benchmarks.
1..=127 => {
n += 1;
i += 1;
}
0 => {
n += 2;
i += 1;
}
b => {
let w = utf8_char_width(b);

View file

@ -1,7 +1,6 @@
use std::io::Write;
use byteorder::{BigEndian, WriteBytesExt};
use zerocopy::AsBytes;
use crate::tag::Tag;
use crate::{modified_utf8, Compound, Error, List, Result, Value};
@ -135,7 +134,10 @@ impl<W: Write> EncodeState<W> {
}
}
Ok(self.writer.write_all(bytes.as_bytes())?)
// SAFETY: i8 has the same layout as u8.
let bytes: &[u8] = unsafe { std::mem::transmute(bytes) };
Ok(self.writer.write_all(bytes)?)
}
fn write_string(&mut self, s: &str) -> Result<()> {
@ -177,7 +179,10 @@ impl<W: Write> EncodeState<W> {
}
}
Ok(self.writer.write_all(bl.as_bytes())?)
// SAFETY: i8 has the same layout as u8.
let bytes: &[u8] = unsafe { std::mem::transmute(bl.as_slice()) };
Ok(self.writer.write_all(bytes)?)
}
List::Short(sl) => self.write_list(sl, Tag::Short, |st, s| st.write_short(*s)),
List::Int(il) => self.write_list(il, Tag::Int, |st, i| st.write_int(*i)),