Skip to content

Commit

Permalink
editoast: collect early
Browse files Browse the repository at this point in the history
The macro was doing the job of collecting. However, most of the
objects sent are already collected before calling the macro
which creates twice the allocations.
  • Loading branch information
woshilapin committed May 17, 2024
1 parent eea95d4 commit 8bcaee1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl ToTokens for CreateBatchImpl {
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use futures_util::stream::TryStreamExt;
let values = values.into_iter().collect::<Vec<_>>();
Ok(crate::chunked_for_libpq! {
#field_count,
values,
Expand Down
10 changes: 4 additions & 6 deletions editoast/src/modelsv2/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ macro_rules! chunked_for_libpq {
const ASYNC_SUBDIVISION: usize = 2_usize;
const CHUNK_SIZE: usize = LIBPQ_MAX_PARAMETERS / ASYNC_SUBDIVISION / $parameters_per_row;
let mut result = Vec::new();
let values = $values.into_iter().collect::<Vec<_>>();
let chunks = values.chunks(CHUNK_SIZE);
for $chunk in chunks.into_iter() {
let chunks = $values.chunks(CHUNK_SIZE);
for $chunk in chunks {
let chunk_result = $query;
result.push(chunk_result);
}
Expand All @@ -143,9 +142,8 @@ macro_rules! chunked_for_libpq {
const ASYNC_SUBDIVISION: usize = 2_usize;
const CHUNK_SIZE: usize = LIBPQ_MAX_PARAMETERS / ASYNC_SUBDIVISION / $parameters_per_row;
let mut result = $result;
let values = $values.into_iter().collect::<Vec<_>>();
let chunks = values.chunks(CHUNK_SIZE);
for $chunk in chunks.into_iter() {
let chunks = $values.chunks(CHUNK_SIZE);
for $chunk in chunks {
let chunk_result = $query;
result.extend(chunk_result);
}
Expand Down

0 comments on commit 8bcaee1

Please sign in to comment.