From 23a5808833c757a2c824b1fdb5c10183d8a3d8e4 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sun, 25 Jul 2021 20:46:20 +0800 Subject: [PATCH] Try reusing memory Fix #10 --- Cargo.lock | 9 +++++++++ Cargo.toml | 3 ++- src/bin/basic_batched.rs | 38 ++++++++++++++++++++------------------ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13942ab..204402d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "ahash" version = "0.4.7" @@ -214,6 +216,7 @@ version = "0.1.0" dependencies = [ "num_cpus", "rand", + "rsor", "rusqlite", "sqlx", "tokio", @@ -822,6 +825,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "rsor" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e89d08ecc43776a04f71ea96132897f591887209c0c0a6b266e6752b584159e" + [[package]] name = "rusqlite" version = "0.25.3" diff --git a/Cargo.toml b/Cargo.toml index b1aa9a8..e6a63b5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ sqlx = { version = "0.5.2", features = ["runtime-tokio-native-tls", "sqlite"]} tokio = {version = "1.5.0", features = ["full"]} rand = "0.8.3" num_cpus = "1.0" -rusqlite = "0.25.3" \ No newline at end of file +rusqlite = "0.25.3" +rsor = "0.1" diff --git a/src/bin/basic_batched.rs b/src/bin/basic_batched.rs index 21700bc..e8cb294 100644 --- a/src/bin/basic_batched.rs +++ b/src/bin/basic_batched.rs @@ -8,6 +8,7 @@ //! next: basic_batched_wp.rs use rusqlite::{Connection, ToSql, Transaction}; +use rsor::Slice; mod common; @@ -46,34 +47,35 @@ fn faker(tx: &Transaction, count: i64) { let mut stmt_with_area = tx.prepare_cached(st1.as_str()).unwrap(); let mut stmt = tx.prepare_cached(st2.as_str()).unwrap(); + let mut params_with_area = Vec::with_capacity(min_batch_size as usize); + let mut param_values = Slice::with_capacity(min_batch_size as usize * 3); for _ in 0..(count / min_batch_size) { let with_area = common::get_random_bool(); let age = common::get_random_age(); let is_active = common::get_random_active(); - let mut param_values: Vec<_> = Vec::new(); if with_area { - // lets prepare the batch - let mut vector = Vec::<(String, i8, i8)>::new(); for _ in 0..min_batch_size { let area_code = common::get_random_area_code(); - vector.push((area_code, age, is_active)); - } - for batch in vector.iter() { - param_values.push(&batch.0 as &dyn ToSql); - param_values.push(&batch.1 as &dyn ToSql); - param_values.push(&batch.2 as &dyn ToSql); + params_with_area.push((area_code,)); } + let param_values = param_values.fill(|mut v| { + for params in ¶ms_with_area { + v.push(¶ms.0 as &dyn ToSql); + v.push(&age as &dyn ToSql); + v.push(&is_active as &dyn ToSql); + } + v + }); stmt_with_area.execute(&*param_values).unwrap(); + params_with_area.clear(); } else { - // lets prepare the batch - let mut vector = Vec::<(i8, i8)>::new(); - for _ in 0..min_batch_size { - vector.push((age, is_active)); - } - for batch in vector.iter() { - param_values.push(&batch.0 as &dyn ToSql); - param_values.push(&batch.1 as &dyn ToSql); - } + let param_values = param_values.fill(|mut v| { + for _ in 0..min_batch_size { + v.push(&age as &dyn ToSql); + v.push(&is_active as &dyn ToSql); + } + v + }); stmt.execute(&*param_values).unwrap(); } }