perf: slightly optimize Vec usage

This commit is contained in:
Ryan 2025-03-10 19:58:59 -04:00
parent 2b2cf1d069
commit b2d8618bba
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
7 changed files with 11 additions and 11 deletions

1
Cargo.lock generated
View File

@ -1471,6 +1471,7 @@ dependencies = [
"ncr", "ncr",
"parking_lot", "parking_lot",
"serde_json", "serde_json",
"smallvec",
"tokio", "tokio",
"zip", "zip",
] ]

View File

@ -36,6 +36,7 @@ mlua = { version = "0", features = ["async", "luajit", "send"] }
ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] } ncr = { version = "0", features = ["cfb8", "ecb", "gcm"] }
parking_lot = "0" parking_lot = "0"
serde_json = "1" serde_json = "1"
smallvec = { version = "1", features = ["write"] }
tokio = { version = "1", features = ["macros"] } tokio = { version = "1", features = ["macros"] }
zip = "2" zip = "2"

View File

@ -41,7 +41,7 @@ pub async fn get_block_states(
lua: Lua, lua: Lua,
(block_names, filter_fn): (Vec<String>, Option<Function>), (block_names, filter_fn): (Vec<String>, Option<Function>),
) -> Result<Vec<u16>> { ) -> Result<Vec<u16>> {
let mut matched = Vec::new(); let mut matched = Vec::with_capacity(16);
for block_name in block_names { for block_name in block_names {
for block in for block in
(u32::MIN..u32::MAX).map_while(|possible_id| BlockState::try_from(possible_id).ok()) (u32::MIN..u32::MAX).map_while(|possible_id| BlockState::try_from(possible_id).ok())

View File

@ -109,11 +109,7 @@ fn id(_lua: &Lua, client: &Client) -> Result<i32> {
} }
fn tab_list(_lua: &Lua, client: &Client) -> Result<Vec<Player>> { fn tab_list(_lua: &Lua, client: &Client) -> Result<Vec<Player>> {
let mut tab_list = Vec::new(); Ok(client.tab_list().into_values().map(Player::from).collect())
for (_, player_info) in client.tab_list() {
tab_list.push(Player::from(player_info));
}
Ok(tab_list)
} }
fn username(_lua: &Lua, client: &Client) -> Result<String> { fn username(_lua: &Lua, client: &Client) -> Result<String> {

View File

@ -56,7 +56,7 @@ pub async fn find_all_entities(
client: UserDataRef<Client>, client: UserDataRef<Client>,
(): (), (): (),
) -> Result<Vec<Table>> { ) -> Result<Vec<Table>> {
let mut matched = Vec::new(); let mut matched = Vec::with_capacity(256);
for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in for (position, custom_name, kind, uuid, direction, id, owner_uuid, pose) in
get_entities!(client) get_entities!(client)
{ {

View File

@ -13,7 +13,7 @@ pub struct Player {
impl From<PlayerInfo> for Player { impl From<PlayerInfo> for Player {
fn from(p: PlayerInfo) -> Self { fn from(p: PlayerInfo) -> Self {
Self { Self {
display_name: p.display_name.map(|n| n.to_string()), display_name: p.display_name.map(|text| text.to_string()),
gamemode: p.gamemode.to_id(), gamemode: p.gamemode.to_id(),
latency: p.latency, latency: p.latency,
name: p.profile.name, name: p.profile.name,

View File

@ -8,6 +8,7 @@ use azalea::{
protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME}, protocol::packets::{PROTOCOL_VERSION, ProtocolPacket, VERSION_NAME},
}; };
use serde_json::json; use serde_json::json;
use smallvec::SmallVec;
use std::{ use std::{
fs::File, fs::File,
io::Write, io::Write,
@ -72,15 +73,16 @@ impl Recorder {
} }
fn save_raw_packet(&mut self, raw_packet: &[u8]) -> Result<()> { fn save_raw_packet(&mut self, raw_packet: &[u8]) -> Result<()> {
let mut data = Vec::from(self.get_timestamp()?); let mut data = Vec::with_capacity(raw_packet.len() + 8);
data.extend(TryInto::<u32>::try_into(raw_packet.len())?.to_be_bytes()); data.extend(self.get_timestamp()?);
data.extend(&TryInto::<u32>::try_into(raw_packet.len())?.to_be_bytes());
data.extend(raw_packet); data.extend(raw_packet);
self.zip_writer.write_all(&data)?; self.zip_writer.write_all(&data)?;
Ok(()) Ok(())
} }
fn save_packet<T: ProtocolPacket>(&mut self, packet: &T) -> Result<()> { fn save_packet<T: ProtocolPacket>(&mut self, packet: &T) -> Result<()> {
let mut raw_packet = Vec::new(); let mut raw_packet = SmallVec::<[u8; 256]>::new();
packet.id().azalea_write_var(&mut raw_packet)?; packet.id().azalea_write_var(&mut raw_packet)?;
packet.write(&mut raw_packet)?; packet.write(&mut raw_packet)?;
self.save_raw_packet(&raw_packet) self.save_raw_packet(&raw_packet)