diff --git a/core/benches/benchmark.rs b/core/benches/benchmark.rs index 5dcc82586..c8ca207f6 100644 --- a/core/benches/benchmark.rs +++ b/core/benches/benchmark.rs @@ -1,6 +1,9 @@ use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; use pprof::criterion::{Output, PProfProfiler}; -use std::sync::Arc; +use std::{ + sync::Arc, + time::{Duration, Instant}, +}; use turso_core::{Database, PlatformIO}; fn rusqlite_open() -> rusqlite::Connection { @@ -74,27 +77,43 @@ fn bench_alter(criterion: &mut Criterion) { let mut group = criterion.benchmark_group("`ALTER TABLE _ RENAME TO _`"); - let stmts = ["CREATE TABLE x(a)", "ALTER TABLE x RENAME TO y", "DROP TABLE y"]; - group.bench_function(BenchmarkId::new("limbo_rename_table", ""), |b| { #[allow(clippy::arc_with_non_send_sync)] let io = Arc::new(PlatformIO::new().unwrap()); let db = Database::open_file(io.clone(), "../testing/schema_5k.db", false, false).unwrap(); let conn = db.connect().unwrap(); - b.iter(|| { - for stmt in stmts { - conn.execute(stmt).unwrap(); - } + b.iter_custom(|iters| { + (0..iters) + .map(|_| { + conn.execute("CREATE TABLE x(a)").unwrap(); + let elapsed = { + let start = Instant::now(); + conn.execute("ALTER TABLE x RENAME TO y").unwrap(); + start.elapsed() + }; + conn.execute("DROP TABLE y").unwrap(); + elapsed + }) + .sum() }); }); if enable_rusqlite { group.bench_function(BenchmarkId::new("sqlite_rename_table", ""), |b| { let conn = rusqlite::Connection::open("../testing/schema_5k.db").unwrap(); - b.iter(|| { - for stmt in stmts { - conn.execute(stmt, ()).unwrap(); - } + b.iter_custom(|iters| { + (0..iters) + .map(|_| { + conn.execute("CREATE TABLE x(a)", ()).unwrap(); + let elapsed = { + let start = Instant::now(); + conn.execute("ALTER TABLE x RENAME TO y", ()).unwrap(); + start.elapsed() + }; + conn.execute("DROP TABLE y", ()).unwrap(); + elapsed + }) + .sum() }); }); } diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index 0bbf53ce2..1a7630290 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -6753,12 +6753,6 @@ pub fn op_rename_table( }; let conn = program.connection.clone(); - // set auto commit to false in order for parse schema to not commit changes as transaction state is stored in connection, - // and we use the same connection for nested query. - let previous_auto_commit = conn.auto_commit.get(); - conn.auto_commit.set(false); - - let stmt = conn.prepare("SELECT * FROM sqlite_schema")?; conn.with_schema_mut(|schema| { schema.indexes.remove(from).map(|mut indexes| { @@ -6770,20 +6764,25 @@ pub fn op_rename_table( schema.indexes.insert(to.to_owned(), indexes); }); - let mut table_ref = schema + let mut table = schema .tables .remove(from) .expect("table being renamed should be in schema"); - let table = Arc::make_mut(&mut table_ref); - let Table::BTree(btree) = table else { - panic!("only btree tables can be renamed"); - }; + { + let table = Arc::make_mut(&mut table); - schema.tables.insert(to.to_owned(), table_ref); + let Table::BTree(btree) = table else { + panic!("only btree tables can be renamed"); + }; + + let btree = Arc::make_mut(btree); + btree.name = to.to_owned(); + } + + schema.tables.insert(to.to_owned(), table); }); - conn.auto_commit.set(previous_auto_commit); state.pc += 1; Ok(InsnFunctionStepResult::Step) }