From 30926da52fc99d7fd6518b98e5f1cce76ffea333 Mon Sep 17 00:00:00 2001
From: francesco <235083032+francesco-stacks@users.noreply.github.com>
Date: Thu, 11 Jun 2026 19:27:49 +0300
Subject: [PATCH 1/2] perf: cache prepared statements on hot SQLite read paths
and raise the per-connection statement-cache capacity
---
changelog.d/sqlite-prepare-cached.changed | 2 +
clarity/src/vm/database/sqlite.rs | 31 ++++-----
.../src/chainstate/stacks/index/trie_sql.rs | 66 ++++++-------------
stackslib/src/util_lib/db.rs | 28 ++++----
4 files changed, 55 insertions(+), 72 deletions(-)
create mode 100644 changelog.d/sqlite-prepare-cached.changed
diff --git a/changelog.d/sqlite-prepare-cached.changed b/changelog.d/sqlite-prepare-cached.changed
new file mode 100644
index 00000000000..7164cdb3274
--- /dev/null
+++ b/changelog.d/sqlite-prepare-cached.changed
@@ -0,0 +1,2 @@
+Cache prepared statements on hot SQLite read paths (Clarity side-store reads, MARF block-id/hash lookups, and the generic `query_*` helpers) instead of re-parsing the SQL on every call, and raise the per-connection statement-cache capacity so the cache is not LRU-thrashed
+
diff --git a/clarity/src/vm/database/sqlite.rs b/clarity/src/vm/database/sqlite.rs
index 3237a1d8dc9..b98177d7850 100644
--- a/clarity/src/vm/database/sqlite.rs
+++ b/clarity/src/vm/database/sqlite.rs
@@ -38,7 +38,10 @@ pub struct SqliteConnection {
fn sqlite_put(conn: &Connection, key: &str, value: &str) -> Result<(), VmExecutionError> {
let params = params![key, value];
- match conn.execute("REPLACE INTO data_table (key, value) VALUES (?, ?)", params) {
+ match conn
+ .prepare_cached("REPLACE INTO data_table (key, value) VALUES (?, ?)")
+ .and_then(|mut stmt| stmt.execute(params))
+ {
Ok(_) => Ok(()),
Err(e) => {
error!("Failed to insert/replace ({key},{value}): {e:?}");
@@ -51,11 +54,8 @@ fn sqlite_get(conn: &Connection, key: &str) -> Result