From e2d272085737710de4f168cc8469400d4009a4f0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 25 Aug 2026 10:01:53 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20SQLite=20query?= =?UTF-8?q?=20map=20allocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-allocate an IndexMap template and clone it per row instead of allocating a new IndexMap and recalculating hashes for every column in every row. Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- compiler/llvm_backend/src/jit.rs | 4 ++-- stdlib/src/sqlite.rs | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/compiler/llvm_backend/src/jit.rs b/compiler/llvm_backend/src/jit.rs index 9005daa3..e05cbfe8 100644 --- a/compiler/llvm_backend/src/jit.rs +++ b/compiler/llvm_backend/src/jit.rs @@ -5,8 +5,8 @@ #![cfg(feature = "llvm")] use llvm_sys::core::*; -use llvm_sys::orc2::*; use llvm_sys::orc2::lljit::*; +use llvm_sys::orc2::*; use std::collections::HashMap; use std::ffi::CString; use std::ptr; @@ -60,7 +60,7 @@ impl LLVMJitEngine { // 3. Set host target triple let _host_triple = LLVMOrcLLJITGetExecutionSession(self.jit); // session triple fallback - // We can just keep the default LLVM target triple + // We can just keep the default LLVM target triple // 4. Wrap Module in ThreadSafeModule let tsm = LLVMOrcCreateNewThreadSafeModule(ctx.module, self.ts_ctx); diff --git a/stdlib/src/sqlite.rs b/stdlib/src/sqlite.rs index c2b0313f..051b7fd8 100644 --- a/stdlib/src/sqlite.rs +++ b/stdlib/src/sqlite.rs @@ -108,15 +108,24 @@ impl StdlibRegistry { let col_names: Vec = (0..col_count) .map(|i| stmt.column_name(i).unwrap_or("?").to_string()) .collect(); + + // Optimization: Pre-allocate a template map to avoid cloning string keys + // and recalculating hashes for every row in the query result. + let mut template_map = IndexMap::with_capacity(col_count); + for name in &col_names { + template_map.insert(name.clone(), RuntimeValue::Null); + } + let mut rows = Vec::new(); let row_iter = stmt .query_map([], |row| { - let mut map = IndexMap::new(); + let mut map = template_map.clone(); for i in 0..col_count { - let name = col_names[i].clone(); let val: String = row.get::<_, String>(i).unwrap_or_default(); - map.insert(name, RuntimeValue::Str(val)); + if let Some((_, v)) = map.get_index_mut(i) { + *v = RuntimeValue::Str(val); + } } Ok(map) })