From 4d7bf6c50eda970ac4998f10f5e46006dffb6ca2 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 30 Apr 2022 01:43:00 -0500 Subject: significantly optimize reading Vec unfortunately, this introduces the requirement of using rust nightly --- azalea-protocol/README.md | 2 ++ azalea-protocol/src/lib.rs | 2 ++ azalea-protocol/src/mc_buf/read.rs | 12 +++++++++++- azalea-protocol/src/mc_buf/write.rs | 8 +++++++- azalea-protocol/src/read.rs | 4 ++++ 5 files changed, 26 insertions(+), 2 deletions(-) (limited to 'azalea-protocol') diff --git a/azalea-protocol/README.md b/azalea-protocol/README.md index 69b28bef..99b8b8d2 100755 --- a/azalea-protocol/README.md +++ b/azalea-protocol/README.md @@ -5,3 +5,5 @@ Sent and receive Minecraft packets. You should probably use `azalea` or `azalea- The goal is to **only** support the latest Minecraft version in order to ease development. This is not yet complete, search for `TODO` in the code for things that need to be done. + +Unfortunately, compiling the crate requires Rust nightly because specialization is not stable yet. diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index 684add45..3573894c 100755 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -1,5 +1,7 @@ //! This lib is responsible for parsing Minecraft packets. +#![feature(min_specialization)] + use std::net::IpAddr; use std::str::FromStr; diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs index 3d1aa0b3..a0e3e79f 100755 --- a/azalea-protocol/src/mc_buf/read.rs +++ b/azalea-protocol/src/mc_buf/read.rs @@ -272,7 +272,7 @@ impl McBufReadable for UnsizedByteArray { #[async_trait] impl McBufReadable for Vec { - async fn read_into(buf: &mut R) -> Result + default async fn read_into(buf: &mut R) -> Result where R: AsyncRead + std::marker::Unpin + std::marker::Send, { @@ -285,6 +285,16 @@ impl McBufReadable for Vec { } } +#[async_trait] +impl McBufReadable for Vec { + async fn read_into(buf: &mut R) -> Result + where + R: AsyncRead + std::marker::Unpin + std::marker::Send, + { + buf.read_byte_array().await + } +} + // string #[async_trait] impl McBufReadable for String { diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs index bd5e3f52..4c7ac60c 100755 --- a/azalea-protocol/src/mc_buf/write.rs +++ b/azalea-protocol/src/mc_buf/write.rs @@ -212,11 +212,17 @@ impl McBufWritable for UnsizedByteArray { // TODO: use specialization when that gets stabilized into rust // to optimize for Vec byte arrays impl McBufWritable for Vec { - fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + default fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { buf.write_list(self, |buf, i| T::write_into(i, buf)) } } +impl McBufWritable for Vec { + fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { + buf.write_byte_array(self) + } +} + // string impl McBufWritable for String { fn write_into(&self, buf: &mut Vec) -> Result<(), std::io::Error> { diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index 88fe95cf..4f8b52af 100755 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -149,7 +149,11 @@ where if let Some(compression_threshold) = compression_threshold { buf = compression_decoder(&mut buf.as_slice(), compression_threshold).await?; } + + let start_time = std::time::Instant::now(); + println!("decoding packet"); let packet = packet_decoder(&mut buf.as_slice(), flow).await?; + println!("decoded packet in {}ms", start_time.elapsed().as_millis()); Ok(packet) } -- cgit v1.2.3