Skip to content

Commit

Permalink
Reduce allocations when decoding maps (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai authored Oct 9, 2024
1 parent 54aa738 commit b7d36ee
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/protocol/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,12 +1092,8 @@ impl<K: Eq + Hash, V, E: Decoder<(K, V)>> Decoder<Option<IndexMap<K, V>>> for Ar
match Int32.decode(buf)? {
-1 => Ok(None),
n if n >= 0 => {
let mut result = IndexMap::new();
for _ in 0..n {
let (k, v) = self.0.decode(buf)?;
result.insert(k, v);
}
Ok(Some(result))
let result: Result<IndexMap<K, V>> = (0..n).map(|_| self.0.decode(buf)).collect();
Ok(Some(result?))
}
n => {
bail!("Array length is negative ({})", n);
Expand Down Expand Up @@ -1290,12 +1286,8 @@ impl<K: Eq + Hash, V, E: Decoder<(K, V)>> Decoder<Option<IndexMap<K, V>>> for Co
match UnsignedVarInt.decode(buf)? {
0 => Ok(None),
n => {
let mut result = IndexMap::new();
for _ in 1..n {
let (k, v) = self.0.decode(buf)?;
result.insert(k, v);
}
Ok(Some(result))
let result: Result<IndexMap<K, V>> = (1..n).map(|_| self.0.decode(buf)).collect();
Ok(Some(result?))
}
}
}
Expand Down

0 comments on commit b7d36ee

Please sign in to comment.