From 83220b43a2174ba12db80126300292960abd57e4 Mon Sep 17 00:00:00 2001 From: strawberry Date: Sat, 1 Jun 2024 00:49:56 -0400 Subject: [PATCH] use saturating_add and vec with_capacity in even more places Signed-off-by: strawberry --- src/admin/media/media_commands.rs | 4 ++-- src/admin/room/room_moderation_commands.rs | 4 ++-- src/service/appservice/mod.rs | 4 ++-- src/service/globals/mod.rs | 2 +- src/service/media/mod.rs | 6 +++--- src/service/rooms/auth_chain/mod.rs | 20 ++++++++++---------- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/admin/media/media_commands.rs b/src/admin/media/media_commands.rs index 8e87b736..e69fbfec 100644 --- a/src/admin/media/media_commands.rs +++ b/src/admin/media/media_commands.rs @@ -23,7 +23,7 @@ pub(crate) async fn delete( debug!("Got event ID to delete media from: {event_id}"); let mut mxc_urls = vec![]; - let mut mxc_deletion_count = 0; + let mut mxc_deletion_count: usize = 0; // parsing the PDU for any MXC URLs begins here if let Some(event_json) = services().rooms.timeline.get_pdu_json(&event_id)? { @@ -123,7 +123,7 @@ pub(crate) async fn delete( for mxc_url in mxc_urls { services().media.delete(mxc_url).await?; - mxc_deletion_count += 1; + mxc_deletion_count = mxc_deletion_count.saturating_add(1); } return Ok(RoomMessageEventContent::text_plain(format!( diff --git a/src/admin/room/room_moderation_commands.rs b/src/admin/room/room_moderation_commands.rs index 88b05fd0..46ae0184 100644 --- a/src/admin/room/room_moderation_commands.rs +++ b/src/admin/room/room_moderation_commands.rs @@ -185,7 +185,7 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) -> .try_into() .expect("#admins:server_name is a valid alias name"); - let mut room_ban_count = 0; + let mut room_ban_count: usize = 0; let mut room_ids: Vec = Vec::new(); for &room in &rooms_s { @@ -297,7 +297,7 @@ pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) -> for room_id in room_ids { if services().rooms.metadata.ban_room(&room_id, true).is_ok() { debug!("Banned {room_id} successfully"); - room_ban_count += 1; + room_ban_count = room_ban_count.saturating_add(1); } debug!("Making all users leave the room {}", &room_id); diff --git a/src/service/appservice/mod.rs b/src/service/appservice/mod.rs index 1d3bc98a..8970dba8 100644 --- a/src/service/appservice/mod.rs +++ b/src/service/appservice/mod.rs @@ -64,8 +64,8 @@ impl TryFrom> for NamespaceRegex { type Error = regex::Error; fn try_from(value: Vec) -> Result { - let mut exclusive = vec![]; - let mut non_exclusive = vec![]; + let mut exclusive = Vec::with_capacity(value.len()); + let mut non_exclusive = Vec::with_capacity(value.len()); for namespace in value { if namespace.exclusive { diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 2621ae24..b30d1566 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -305,7 +305,7 @@ impl Service { pub fn block_non_admin_invites(&self) -> bool { self.config.block_non_admin_invites } pub fn supported_room_versions(&self) -> Vec { - let mut room_versions: Vec = vec![]; + let mut room_versions: Vec = Vec::with_capacity(self.stable_room_versions.len()); room_versions.extend(self.stable_room_versions.clone()); if self.allow_unstable_room_versions() { room_versions.extend(self.unstable_room_versions.clone()); diff --git a/src/service/media/mod.rs b/src/service/media/mod.rs index 0697577f..c7326e1a 100644 --- a/src/service/media/mod.rs +++ b/src/service/media/mod.rs @@ -182,7 +182,7 @@ impl Service { /// Deletes all remote only media files in the given at or after /// time/duration. Returns a u32 with the amount of media files deleted. - pub async fn delete_all_remote_media_at_after_time(&self, time: String) -> Result { + pub async fn delete_all_remote_media_at_after_time(&self, time: String) -> Result { let all_keys = self.db.get_all_media_keys(); let user_duration: SystemTime = match cyborgtime::parse_duration(&time) { @@ -278,12 +278,12 @@ impl Service { debug!("Deleting media now in the past \"{:?}\".", user_duration); - let mut deletion_count = 0; + let mut deletion_count: usize = 0; for mxc in remote_mxcs { debug!("Deleting MXC {mxc} from database and filesystem"); self.delete(mxc).await?; - deletion_count += 1; + deletion_count = deletion_count.saturating_add(1); } Ok(deletion_count) diff --git a/src/service/rooms/auth_chain/mod.rs b/src/service/rooms/auth_chain/mod.rs index d8e38c85..fce98521 100644 --- a/src/service/rooms/auth_chain/mod.rs +++ b/src/service/rooms/auth_chain/mod.rs @@ -54,9 +54,9 @@ impl Service { "start", ); - let mut hits = 0; - let mut misses = 0; - let mut full_auth_chain = Vec::new(); + let mut hits: usize = 0; + let mut misses: usize = 0; + let mut full_auth_chain = Vec::with_capacity(buckets.len()); for chunk in buckets { if chunk.is_empty() { continue; @@ -70,13 +70,13 @@ impl Service { { trace!("Found cache entry for whole chunk"); full_auth_chain.extend(cached.iter().copied()); - hits += 1; + hits = hits.saturating_add(1); continue; } - let mut hits2 = 0; - let mut misses2 = 0; - let mut chunk_cache = Vec::new(); + let mut hits2: usize = 0; + let mut misses2: usize = 0; + let mut chunk_cache = Vec::with_capacity(chunk.len()); for (sevent_id, event_id) in chunk { if let Some(cached) = services() .rooms @@ -85,7 +85,7 @@ impl Service { { trace!(?event_id, "Found cache entry for event"); chunk_cache.extend(cached.iter().copied()); - hits2 += 1; + hits2 = hits2.saturating_add(1); } else { let auth_chain = self.get_auth_chain_inner(room_id, event_id)?; services() @@ -93,7 +93,7 @@ impl Service { .auth_chain .cache_auth_chain(vec![sevent_id], &auth_chain)?; chunk_cache.extend(auth_chain.iter()); - misses2 += 1; + misses2 = misses2.saturating_add(1); debug!( event_id = ?event_id, chain_length = ?auth_chain.len(), @@ -111,7 +111,7 @@ impl Service { .auth_chain .cache_auth_chain_vec(chunk_key, &chunk_cache)?; full_auth_chain.extend(chunk_cache.iter()); - misses += 1; + misses = misses.saturating_add(1); debug!( chunk_cache_length = ?chunk_cache.len(), hits = ?hits2,