mirror of
https://github.com/italicsjenga/valence.git
synced 2024-12-23 14:31:30 +11:00
Improve valence_nbt write perf and remove zerocopy dependency (#174)
This commit is contained in:
parent
4194acaa67
commit
2fc59d90a8
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)),
|
||||
|
|
Loading…
Reference in a new issue