Skip to content

Commit 23a5808

Browse files
committed
Try reusing memory
Fix #10
1 parent b6671ce commit 23a5808

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ sqlx = { version = "0.5.2", features = ["runtime-tokio-native-tls", "sqlite"]}
1010
tokio = {version = "1.5.0", features = ["full"]}
1111
rand = "0.8.3"
1212
num_cpus = "1.0"
13-
rusqlite = "0.25.3"
13+
rusqlite = "0.25.3"
14+
rsor = "0.1"

src/bin/basic_batched.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! next: basic_batched_wp.rs
99
1010
use rusqlite::{Connection, ToSql, Transaction};
11+
use rsor::Slice;
1112

1213
mod common;
1314

@@ -46,34 +47,35 @@ fn faker(tx: &Transaction, count: i64) {
4647

4748
let mut stmt_with_area = tx.prepare_cached(st1.as_str()).unwrap();
4849
let mut stmt = tx.prepare_cached(st2.as_str()).unwrap();
50+
let mut params_with_area = Vec::with_capacity(min_batch_size as usize);
51+
let mut param_values = Slice::with_capacity(min_batch_size as usize * 3);
4952
for _ in 0..(count / min_batch_size) {
5053
let with_area = common::get_random_bool();
5154
let age = common::get_random_age();
5255
let is_active = common::get_random_active();
53-
let mut param_values: Vec<_> = Vec::new();
5456
if with_area {
55-
// lets prepare the batch
56-
let mut vector = Vec::<(String, i8, i8)>::new();
5757
for _ in 0..min_batch_size {
5858
let area_code = common::get_random_area_code();
59-
vector.push((area_code, age, is_active));
60-
}
61-
for batch in vector.iter() {
62-
param_values.push(&batch.0 as &dyn ToSql);
63-
param_values.push(&batch.1 as &dyn ToSql);
64-
param_values.push(&batch.2 as &dyn ToSql);
59+
params_with_area.push((area_code,));
6560
}
61+
let param_values = param_values.fill(|mut v| {
62+
for params in &params_with_area {
63+
v.push(&params.0 as &dyn ToSql);
64+
v.push(&age as &dyn ToSql);
65+
v.push(&is_active as &dyn ToSql);
66+
}
67+
v
68+
});
6669
stmt_with_area.execute(&*param_values).unwrap();
70+
params_with_area.clear();
6771
} else {
68-
// lets prepare the batch
69-
let mut vector = Vec::<(i8, i8)>::new();
70-
for _ in 0..min_batch_size {
71-
vector.push((age, is_active));
72-
}
73-
for batch in vector.iter() {
74-
param_values.push(&batch.0 as &dyn ToSql);
75-
param_values.push(&batch.1 as &dyn ToSql);
76-
}
72+
let param_values = param_values.fill(|mut v| {
73+
for _ in 0..min_batch_size {
74+
v.push(&age as &dyn ToSql);
75+
v.push(&is_active as &dyn ToSql);
76+
}
77+
v
78+
});
7779
stmt.execute(&*param_values).unwrap();
7880
}
7981
}

0 commit comments

Comments
 (0)