From f1a3c0a8cfb34bc52f5da3b0727583d8685f75f3 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 20 Feb 2025 23:57:41 +0100 Subject: [PATCH] TODO Debug sqlite performance --- Cargo.lock | 49 ++++++++++++++++++++++++++---------------- Cargo.toml | 15 +++++++++++-- cove/src/vault/euph.rs | 40 +++++++++++++++++++++++++++------- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f4c43f..5341662 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,18 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy 0.7.35", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -550,12 +562,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foldhash" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" - [[package]] name = "fs_extra" version = "1.3.0" @@ -641,20 +647,26 @@ checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" [[package]] name = "hashbrown" -version = "0.15.2" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" dependencies = [ - "foldhash", + "ahash", ] [[package]] -name = "hashlink" -version = "0.10.0" +name = "hashbrown" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "hashlink" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" dependencies = [ - "hashbrown", + "hashbrown 0.14.5", ] [[package]] @@ -696,7 +708,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.2", ] [[package]] @@ -817,11 +829,10 @@ dependencies = [ [[package]] name = "libsqlite3-sys" -version = "0.31.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad8935b44e7c13394a179a438e0cebba0fe08fe01b54f152e29a93b5cf993fd4" +checksum = "0c10584274047cb335c23d3e61bcef8e323adae7c5c8c760540f73610177fc3f" dependencies = [ - "cc", "pkg-config", "vcpkg", ] @@ -1169,9 +1180,9 @@ dependencies = [ [[package]] name = "rusqlite" -version = "0.33.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c6d5e5acb6f6129fe3f7ba0a7fc77bca1942cb568535e18e7bc40262baf3110" +checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae" dependencies = [ "bitflags", "fallible-iterator", diff --git a/Cargo.toml b/Cargo.toml index b6edc2c..02b4525 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,10 @@ open = "5.3.2" parking_lot = "0.12.3" proc-macro2 = "1.0.93" quote = "1.0.38" -rusqlite = { version = "0.33.0", features = ["bundled", "time"] } +rusqlite = { version = "0.31.0", features = [ + # "bundled", + "time", +] } rustls = "0.23.23" serde = { version = "1.0.218", features = ["derive"] } serde_either = "0.2.1" @@ -46,7 +49,7 @@ path = "../toss" [workspace.dependencies.vault] path = "../vault" # git = "https://github.com/Garmelon/vault.git" -# tag = "v0.4.0" +# tag = "v0.5.0" features = ["tokio"] [workspace.lints] @@ -74,3 +77,11 @@ clippy.use_self = "warn" [profile.dev.package."*"] opt-level = 3 + +# For profiling + +[profile.release] +debug = 1 + +[rust] +debuginfo-level = 1 diff --git a/cove/src/vault/euph.rs b/cove/src/vault/euph.rs index c7d6410..8ee7b72 100644 --- a/cove/src/vault/euph.rs +++ b/cove/src/vault/euph.rs @@ -1,4 +1,5 @@ use std::str::FromStr; +use std::time::Instant; use std::{fmt, mem}; use async_trait::async_trait; @@ -686,12 +687,12 @@ impl Action for GetTree { type Error = rusqlite::Error; fn run(self, conn: &mut Connection) -> Result { - let msgs = conn - .prepare( - " + let start = Instant::now(); + + let query = " WITH RECURSIVE tree (domain, room, id) AS ( - VALUES (?, ?, ?) + VALUES (:domain, :room, :id) UNION SELECT euph_msgs.domain, euph_msgs.room, euph_msgs.id FROM euph_msgs @@ -700,14 +701,35 @@ impl Action for GetTree { AND tree.room = euph_msgs.room AND tree.id = euph_msgs.parent ) - SELECT id, parent, time, name, content, seen + SELECT id, parent, time, 'name', 'content', 1 FROM euph_msgs JOIN tree USING (domain, room, id) ORDER BY id ASC - ", - )? + "; + + let mut statement = conn.prepare(&format!("EXPLAIN QUERY PLAN {query}"))?; + let mut rows = statement.query(named_params! { + ":domain": self.room.domain, + ":room": self.room.name, + ":id": WSnowflake(self.root_id.0), + })?; + + while let Some(row) = rows.next()? { + let id = row.get::<_, i64>("id")?; + let parent = row.get::<_, i64>("parent")?; + let notused = row.get::<_, i64>("notused")?; + let detail = row.get::<_, String>("detail")?; + eprintln!("{parent:3} -> {id:3} (notused {notused:3}): {detail}"); + } + + let msgs = conn + .prepare(query)? .query_map( - params![self.room.domain, self.room.name, WSnowflake(self.root_id.0)], + named_params! { + ":domain": self.room.domain, + ":room": self.room.name, + ":id": WSnowflake(self.root_id.0), + }, |row| { Ok(SmallMessage { id: MessageId(row.get::<_, WSnowflake>(0)?.0), @@ -720,6 +742,8 @@ impl Action for GetTree { }, )? .collect::>()?; + let end = Instant::now(); + eprintln!("{:10}", end.duration_since(start).as_micros()); Ok(Tree::new(self.root_id, msgs)) } }