From d6d41c0d42463fda7fcb812b517601c9e2729766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joona=20K=C3=A4rkk=C3=A4inen?= Date: Fri, 2 Feb 2024 16:31:56 +0200 Subject: [PATCH] perf: directly collect join results to separate containers --- src/database/invoices.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/database/invoices.rs b/src/database/invoices.rs index 092da5c..8bd7101 100644 --- a/src/database/invoices.rs +++ b/src/database/invoices.rs @@ -2,6 +2,7 @@ use super::DatabaseConnection; use crate::api::invoices::{CreateInvoice, PopulatedInvoice}; use crate::error::Error; use crate::models::*; +use futures::TryStreamExt; use diesel::prelude::*; use diesel_async::RunQueryDsl; @@ -110,10 +111,17 @@ impl DatabaseConnection { invoices::table .inner_join(parties::table) .select((Invoice::as_select(), Party::as_select())) - .load::<(Invoice, Party)>(&mut self.0) + .load_stream::<(Invoice, Party)>(&mut self.0) + .await? + .try_fold( + (Vec::new(), Vec::new()), + |(mut invoices, mut parties), (invoice, party)| { + invoices.push(invoice); + parties.push(party); + futures::future::ready(Ok((invoices, parties))) + }, + ) .await? - .into_iter() - .unzip() }; let invoice_rows = InvoiceRow::belonging_to(&invoices) .select(InvoiceRow::as_select())