From 21f3f0c1ebcfad22e5a3ab497d2139a36bd77d6e Mon Sep 17 00:00:00 2001 From: Jade Ellis Date: Sat, 21 Nov 2026 21:58:40 +0000 Subject: [PATCH] perf(service/sending): Seek past destination prefixes in queued_destinations --- src/service/sending/data.rs | 100 ++++++++++++++++++++--------------- src/service/sending/tests.rs | 6 ++- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/src/service/sending/data.rs b/src/service/sending/data.rs index fc8bc03a0..afb08439c 100644 --- a/src/service/sending/data.rs +++ b/src/service/sending/data.rs @@ -171,25 +171,37 @@ impl Data { } /// Destinations with at least one queued request not yet in flight. + /// + /// Seeks past each destination's key prefix so large per-destination + /// backlogs are skipped in O(destinations) seeks rather than scanning + /// every queued event. pub(super) async fn queued_destinations(&self) -> Vec { - let mut dests: Vec = Vec::new(); - let mut last_prefix: Vec = Vec::new(); - self.servernameevent_data - .raw_stream() + let mut dests = Vec::new(); + let mut cursor = Vec::new(); + + while let Some(key) = self + .servernameevent_data + .raw_keys_from(&cursor) .ignore_err() - .ready_for_each(|(key, val)| { - if !last_prefix.is_empty() && key.starts_with(&last_prefix) { - return; - } - match parse_servercurrentevent(key, val) { - | Ok((dest, _)) => { - last_prefix = dest.get_prefix(); - dests.push(dest); - }, - | Err(e) => warn!(?key, "invalid queued request key: {e}"), - } - }) - .await; + .next() + .await + { + match parse_destination(key) { + | Ok((dest, _)) => { + let prefix = dest.get_prefix(); + dests.push(dest); + let Some(next) = next_prefix(&prefix) else { + break; + }; + cursor = next; + }, + | Err(e) => { + warn!(?key, "invalid queued request key: {e}"); + cursor = key.to_vec(); + cursor.push(0); + }, + } + } dests } @@ -216,9 +228,17 @@ impl Data { } } -fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, SendingEvent)> { +/// Smallest byte slice strictly greater than every key starting with `prefix`. +fn next_prefix(prefix: &[u8]) -> Option> { + let pos = prefix.iter().rposition(|&b| b < u8::MAX)?; + let mut next = prefix[..=pos].to_vec(); + next[pos] = next[pos].saturating_add(1); + Some(next) +} + +fn parse_destination(key: &[u8]) -> Result<(Destination, &[u8])> { // Appservices start with a plus - Ok::<_, Error>(if key.starts_with(b"+") { + if key.starts_with(b"+") { let mut parts = key[1..].splitn(2, |&b| b == 0xFF); let server = parts.next().expect("splitn always returns one element"); @@ -230,14 +250,7 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se Error::bad_database("Invalid server bytes in server_currenttransaction") })?; - ( - Destination::Appservice(server), - if value.is_empty() { - SendingEvent::Pdu(event.into()) - } else { - SendingEvent::Edu(value.into()) - }, - ) + Ok((Destination::Appservice(server), event)) } else if key.starts_with(b"$") { let mut parts = key[1..].splitn(3, |&b| b == 0xFF); @@ -257,15 +270,7 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se .next() .ok_or_else(|| Error::bad_database("Invalid bytes in servercurrentpdus."))?; - ( - Destination::Push(user_id, pushkey_string), - if value.is_empty() { - SendingEvent::Pdu(event.into()) - } else { - // I'm pretty sure this should never be called - SendingEvent::Edu(value.into()) - }, - ) + Ok((Destination::Push(user_id, pushkey_string), event)) } else { let mut parts = key.splitn(2, |&b| b == 0xFF); @@ -278,15 +283,22 @@ fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, Se Error::bad_database("Invalid server bytes in server_currenttransaction") })?; - ( + Ok(( Destination::Federation(ServerName::parse(&server).map_err(|_| { Error::bad_database("Invalid server string in server_currenttransaction") })?), - if value.is_empty() { - SendingEvent::Pdu(event.into()) - } else { - SendingEvent::Edu(value.into()) - }, - ) - }) + event, + )) + } +} + +fn parse_servercurrentevent(key: &[u8], value: &[u8]) -> Result<(Destination, SendingEvent)> { + let (dest, event) = parse_destination(key)?; + let event = if value.is_empty() { + SendingEvent::Pdu(event.into()) + } else { + SendingEvent::Edu(value.into()) + }; + + Ok((dest, event)) } diff --git a/src/service/sending/tests.rs b/src/service/sending/tests.rs index be466d65e..0e59f1921 100644 --- a/src/service/sending/tests.rs +++ b/src/service/sending/tests.rs @@ -10,12 +10,16 @@ use crate::test_utils::test_services; async fn queued_destinations_lists_each_destination_with_unsent_requests() { let (services, path, _database) = test_services().await; let db = &services.sending.db; + let queued_sub = Destination::Federation(server_name!("queued.example.org").to_owned()); let queued = Destination::Federation(server_name!("queued.example").to_owned()); + let appservice = Destination::Appservice("bridge".to_owned()); let active = Destination::Federation(server_name!("active.example").to_owned()); let event = SendingEvent::Edu(EduBuf::from_slice(br#"{"edu_type":"m.typing"}"#)); db.queue_requests(once((&event, &queued))); db.queue_requests(once((&event, &queued))); + db.queue_requests(once((&event, &queued_sub))); + db.queue_requests(once((&event, &appservice))); db.queue_requests(once((&event, &active))); let in_flight: Vec<_> = db.queued_requests(&active).collect().await; db.mark_as_active(in_flight.iter()); @@ -24,7 +28,7 @@ async fn queued_destinations_lists_each_destination_with_unsent_requests() { drop(services); std::fs::remove_dir_all(path).unwrap(); - assert_eq!(dests, [queued]); + assert_eq!(dests, [appservice, queued_sub, queued]); } #[test]