Preallocate a few more response parsers

This commit is contained in:
2025-12-08 14:27:42 +09:00
parent 00d93390d4
commit 7863b166a1
4 changed files with 9 additions and 5 deletions

View File

@@ -34,7 +34,10 @@ impl CommandResponse for OutputsResponse {
}
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
let mut outputs = Vec::new();
let parts = parts.into_vec()?;
let result_len = parts.iter().filter(|(k, _)| *k == "outputid").count();
let mut outputs = Vec::with_capacity(result_len);
let mut id: Option<AudioOutputId> = None;
let mut name: Option<String> = None;
@@ -42,7 +45,7 @@ impl CommandResponse for OutputsResponse {
let mut enabled: Option<bool> = None;
let mut attributes: HashMap<String, String> = HashMap::new();
for (k, v) in parts.into_vec()?.into_iter() {
for (k, v) in parts.into_iter() {
match k {
"outputid" => {
// Reset and store the previous output if all fields are present

View File

@@ -38,7 +38,7 @@ impl CommandRequest for ProtocolDisableRequest {
return Err(RequestParserError::UnexpectedEOF);
}
let mut features = Vec::new();
let mut features = Vec::with_capacity(parts.size_hint().0);
for part in parts {
let feature = part
.parse()

View File

@@ -38,7 +38,7 @@ impl CommandRequest for ProtocolEnableRequest {
return Err(RequestParserError::UnexpectedEOF);
}
let mut features = Vec::new();
let mut features = Vec::with_capacity(parts.size_hint().0);
for part in parts {
let feature = part
.parse()

View File

@@ -68,7 +68,8 @@ impl CommandResponse for StickerNamesTypesResponse {
fn parse(parts: ResponseAttributes<'_>) -> Result<Self, ResponseParserError> {
let parts: Vec<_> = parts.into_vec()?;
let mut result = HashMap::new();
debug_assert!(parts.len() % 2 == 0);
let mut result = HashMap::with_capacity(parts.len() / 2);
for name_type_pair in parts.chunks_exact(2) {
// TODO: don't depend on order, just make sure we have both