Skip to content

Commit

Permalink
📝 add batch for queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Xudong-Huang committed Apr 24, 2024
1 parent bd9ff68 commit 796d287
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions examples/techempower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,28 @@ mod __impl {
rand: &mut WyRand,
) -> Result<SmallVec<[WorldRow; 32]>, may_postgres::Error> {
let mut queries = SmallVec::<[_; 32]>::new();
for _ in 0..num {
let random_id = (rand.generate::<u32>() % 10_000 + 1) as i32;
queries.push(
self.client
.query_raw(&self.statement.world, &[&random_id])?,
);
}

let mut worlds = SmallVec::<[_; 32]>::new();
for mut q in queries {
match q.next().transpose()? {
Some(row) => worlds.push(WorldRow {
id: row.get(0),
randomnumber: row.get(1),
}),
None => unreachable!(),
let mut i = 0;
let batch_size = 10;
while i < num {
let batch = batch_size.min(num - i);
for _ in 0..batch {
let random_id = (rand.generate::<u32>() % 10_000 + 1) as i32;
queries.push(
self.client
.query_raw(&self.statement.world, &[&random_id])?,
);
}
for q in &mut queries[i..i + batch] {
match q.next().transpose()? {
Some(row) => worlds.push(WorldRow {
id: row.get(0),
randomnumber: row.get(1),
}),
None => unreachable!(),
}
}
i += batch;
}
Ok(worlds)
}
Expand Down

0 comments on commit 796d287

Please sign in to comment.