use VecDeque for todo queues

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-28 02:51:30 +00:00 committed by strawberry
parent 74eb30c106
commit e21403a4d4
3 changed files with 13 additions and 12 deletions

View file

@ -1,7 +1,7 @@
mod data; mod data;
use std::{ use std::{
collections::{BTreeSet, HashSet}, collections::{BTreeSet, HashSet, VecDeque},
fmt::Debug, fmt::Debug,
sync::Arc, sync::Arc,
}; };
@ -185,10 +185,10 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
event_id: &EventId, event_id: &EventId,
) -> Result<HashSet<ShortEventId>> { ) -> Result<HashSet<ShortEventId>> {
let mut todo = vec![event_id.to_owned()]; let mut todo: VecDeque<_> = [event_id.to_owned()].into();
let mut found = HashSet::new(); let mut found = HashSet::new();
while let Some(event_id) = todo.pop() { while let Some(event_id) = todo.pop_front() {
trace!(?event_id, "processing auth event"); trace!(?event_id, "processing auth event");
match self.services.timeline.get_pdu(&event_id).await { match self.services.timeline.get_pdu(&event_id).await {
@ -218,7 +218,8 @@ impl Service {
?auth_event, ?auth_event,
"adding auth event to processing queue" "adding auth event to processing queue"
); );
todo.push(auth_event.clone());
todo.push_back(auth_event.clone());
} }
} }
}, },

View file

@ -1,5 +1,5 @@
use std::{ use std::{
collections::{hash_map, BTreeMap, HashSet}, collections::{hash_map, BTreeMap, HashSet, VecDeque},
sync::Arc, sync::Arc,
time::Instant, time::Instant,
}; };
@ -62,10 +62,10 @@ pub(super) async fn fetch_and_handle_outliers<'a>(
// c. Ask origin server over federation // c. Ask origin server over federation
// We also handle its auth chain here so we don't get a stack overflow in // We also handle its auth chain here so we don't get a stack overflow in
// handle_outlier_pdu. // handle_outlier_pdu.
let mut todo_auth_events = vec![id.clone()]; let mut todo_auth_events: VecDeque<_> = [id.clone()].into();
let mut events_in_reverse_order = Vec::with_capacity(todo_auth_events.len()); let mut events_in_reverse_order = Vec::with_capacity(todo_auth_events.len());
let mut events_all = HashSet::with_capacity(todo_auth_events.len()); let mut events_all = HashSet::with_capacity(todo_auth_events.len());
while let Some(next_id) = todo_auth_events.pop() { while let Some(next_id) = todo_auth_events.pop_front() {
if let Some((time, tries)) = self if let Some((time, tries)) = self
.services .services
.globals .globals
@ -132,7 +132,7 @@ pub(super) async fn fetch_and_handle_outliers<'a>(
if let Ok(auth_event) = if let Ok(auth_event) =
serde_json::from_value::<OwnedEventId>(auth_event.clone().into()) serde_json::from_value::<OwnedEventId>(auth_event.clone().into())
{ {
todo_auth_events.push(auth_event); todo_auth_events.push_back(auth_event);
} else { } else {
warn!("Auth event id is not valid"); warn!("Auth event id is not valid");
} }

View file

@ -1,5 +1,5 @@
use std::{ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet, VecDeque},
sync::Arc, sync::Arc,
}; };
@ -30,13 +30,13 @@ pub(super) async fn fetch_prev(
)> { )> {
let mut graph: HashMap<OwnedEventId, _> = HashMap::with_capacity(initial_set.len()); let mut graph: HashMap<OwnedEventId, _> = HashMap::with_capacity(initial_set.len());
let mut eventid_info = HashMap::new(); let mut eventid_info = HashMap::new();
let mut todo_outlier_stack: Vec<OwnedEventId> = initial_set; let mut todo_outlier_stack: VecDeque<OwnedEventId> = initial_set.into();
let first_pdu_in_room = self.services.timeline.first_pdu_in_room(room_id).await?; let first_pdu_in_room = self.services.timeline.first_pdu_in_room(room_id).await?;
let mut amount = 0; let mut amount = 0;
while let Some(prev_event_id) = todo_outlier_stack.pop() { while let Some(prev_event_id) = todo_outlier_stack.pop_front() {
self.services.server.check_running()?; self.services.server.check_running()?;
if let Some((pdu, mut json_opt)) = self if let Some((pdu, mut json_opt)) = self
@ -74,7 +74,7 @@ pub(super) async fn fetch_prev(
amount = amount.saturating_add(1); amount = amount.saturating_add(1);
for prev_prev in &pdu.prev_events { for prev_prev in &pdu.prev_events {
if !graph.contains_key(prev_prev) { if !graph.contains_key(prev_prev) {
todo_outlier_stack.push(prev_prev.clone()); todo_outlier_stack.push_back(prev_prev.clone());
} }
} }