perf: make get_contacts concurrent

and `get_contacts_by_ids`.

Based on hand-testing `get_contacts` with a dummy account
with 5000 dummy contacts, this very slightly increased performance.
From 1380ms to 1325ms.
This commit is contained in:
WofWca
2025-02-03 15:42:54 +04:00
parent aa82402151
commit d7a706d95b

View File

@@ -1396,17 +1396,13 @@ impl CommandApi {
) -> Result<Vec<ContactObject>> { ) -> Result<Vec<ContactObject>> {
let ctx = self.get_context(account_id).await?; let ctx = self.get_context(account_id).await?;
let contact_ids = Contact::get_all(&ctx, list_flags, query.as_deref()).await?; let contact_ids = Contact::get_all(&ctx, list_flags, query.as_deref()).await?;
let mut contacts: Vec<ContactObject> = Vec::with_capacity(contact_ids.len()); let ctx_ref = &ctx;
for id in contact_ids { return futures::future::try_join_all(contact_ids.into_iter().map(|id| async move {
contacts.push( let contact = deltachat::contact::Contact::get_by_id(ctx_ref, id).await?;
ContactObject::try_from_dc_contact( let contact_object = ContactObject::try_from_dc_contact(ctx_ref, contact).await?;
&ctx, Ok(contact_object)
deltachat::contact::Contact::get_by_id(&ctx, id).await?, }))
) .await;
.await?,
);
}
Ok(contacts)
} }
async fn get_contacts_by_ids( async fn get_contacts_by_ids(
@@ -1416,18 +1412,15 @@ impl CommandApi {
) -> Result<HashMap<u32, ContactObject>> { ) -> Result<HashMap<u32, ContactObject>> {
let ctx = self.get_context(account_id).await?; let ctx = self.get_context(account_id).await?;
let mut contacts = HashMap::with_capacity(ids.len()); let ctx_ref = &ctx;
for id in ids { return futures::future::try_join_all(ids.into_iter().map(|id| async move {
contacts.insert( let contact =
id, deltachat::contact::Contact::get_by_id(ctx_ref, ContactId::new(id)).await?;
ContactObject::try_from_dc_contact( let contact_object = ContactObject::try_from_dc_contact(ctx_ref, contact).await?;
&ctx, Ok((id, contact_object))
deltachat::contact::Contact::get_by_id(&ctx, ContactId::new(id)).await?, }))
) .await
.await?, .map(|vec| vec.into_iter().collect());
);
}
Ok(contacts)
} }
async fn delete_contact(&self, account_id: u32, contact_id: u32) -> Result<()> { async fn delete_contact(&self, account_id: u32, contact_id: u32) -> Result<()> {