From c07b060080fbb43a3d5f6b13a103e4e8be673fd2 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 6 Apr 2023 10:49:15 +0200 Subject: [PATCH] feat(sdk): Create vectors with correct capacity to avoid reallocations. `updated_rooms` and `updated_lists` can be created with an initial capacity. Doing so will avoid reallocations if the vector is too small. --- crates/matrix-sdk/src/sliding_sync/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk/src/sliding_sync/mod.rs b/crates/matrix-sdk/src/sliding_sync/mod.rs index df2c83612..73fab5830 100644 --- a/crates/matrix-sdk/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk/src/sliding_sync/mod.rs @@ -354,9 +354,11 @@ impl SlidingSync { } let update_summary = { - let mut updated_rooms = Vec::new(); let mut rooms_map = self.inner.rooms.write().unwrap(); + // Update the rooms. + let mut updated_rooms = Vec::with_capacity(sliding_sync_response.rooms.len()); + for (room_id, mut room_data) in sliding_sync_response.rooms.into_iter() { // `sync_response` contains the rooms with decrypted events if any, so look at // the timeline events here first if the room exists. @@ -390,7 +392,8 @@ impl SlidingSync { updated_rooms.push(room_id); } - let mut updated_lists = Vec::new(); + // Update the lists. + let mut updated_lists = Vec::with_capacity(sliding_sync_response.lists.len()); for (name, updates) in sliding_sync_response.lists { let Some(list) = lists.get_mut(&name) else {