From 5ece998cacb8ce30ab69b332c93c192b528b7b38 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 6 May 2026 18:25:31 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= =?UTF-8?q?=20Pre-allocate=20String=20and=20use=20write!=20for=20D1=20SQL?= =?UTF-8?q?=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactored `build_upsert_stmt` and `build_delete_stmt` in `crates/flow/src/targets/d1.rs` to use `String::with_capacity` and the `write!` macro instead of `format!` and `Vec::join`. This reduces memory allocation overhead and intermediate string copying when constructing dynamic SQL queries for the Cloudflare D1 target. Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com> --- .jules/bolt.md | 4 ++ crates/flow/src/targets/d1.rs | 73 +++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index fb3e8f1..4b6f3b6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -2,3 +2,7 @@ ## 2026-04-08 - [Performance: Defer Allocation during Traversal] **Learning:** During DAG traversals, creating owned variants of identifiers (like `file.to_path_buf()`) *before* checking `visited` HashSets results in heap allocations (O(E)) for every edge instead of every visited node (O(V)). By moving the `&PathBuf` allocation strictly *after* all HashSet `contains` checks using the borrowed reference (`&Path`), we drastically reduce memory churn. **Action:** Always check `HashSet::contains` with a borrowed reference *before* creating the owned version required by `HashSet::insert`, especially in performance-critical graph traversal paths. + +## 2026-04-09 - [Performance: Pre-allocate and use write! for dynamic SQL] +**Learning:** Constructing dynamic SQL queries in hot loops (like D1 target `upsert` and `delete` batching) using `format!` and `Vec::join` creates excessive intermediate string allocations and memory copies. +**Action:** Use `String::with_capacity` pre-calculated with a conservative estimate, combined with the `write!` macro, to build dynamic SQL statements efficiently without intermediate vectors and heap allocations. diff --git a/crates/flow/src/targets/d1.rs b/crates/flow/src/targets/d1.rs index e45fd52..b3ed914 100644 --- a/crates/flow/src/targets/d1.rs +++ b/crates/flow/src/targets/d1.rs @@ -300,40 +300,55 @@ impl D1ExportContext { key: &KeyValue, values: &FieldValues, ) -> Result<(String, Vec), RecocoError> { - let mut columns = vec![]; - let mut placeholders = vec![]; - let mut params = vec![]; - let mut update_clauses = vec![]; + use std::fmt::Write; + let mut params = + Vec::with_capacity(self.key_fields_schema.len() + self.value_fields_schema.len()); + // ⚡ Bolt Optimization: Use String::with_capacity and write! to avoid intermediate Vec and format! allocations + let mut sql = String::with_capacity(128 + params.capacity() * 32); + + write!(sql, "INSERT INTO {} (", self.table_name).unwrap(); + let mut first = true; // Extract key parts - KeyValue is a wrapper around Box<[KeyPart]> for (idx, _key_field) in self.key_fields_schema.iter().enumerate() { if let Some(key_part) = key.0.get(idx) { - columns.push(self.key_fields_schema[idx].name.clone()); - placeholders.push("?".to_string()); + if !first { + sql.push_str(", "); + } + sql.push_str(&self.key_fields_schema[idx].name); params.push(key_part_to_json(key_part)?); + first = false; } } // Add value fields for (idx, value) in values.fields.iter().enumerate() { if let Some(value_field) = self.value_fields_schema.get(idx) { - columns.push(value_field.name.clone()); - placeholders.push("?".to_string()); + if !first { + sql.push_str(", "); + } + sql.push_str(&value_field.name); params.push(value_to_json(value)?); - update_clauses.push(format!( - "{} = excluded.{}", - value_field.name, value_field.name - )); + first = false; } } - let sql = format!( - "INSERT INTO {} ({}) VALUES ({}) ON CONFLICT DO UPDATE SET {}", - self.table_name, - columns.join(", "), - placeholders.join(", "), - update_clauses.join(", ") - ); + sql.push_str(") VALUES ("); + for i in 0..params.len() { + sql.push_str(if i > 0 { ", ?" } else { "?" }); + } + + sql.push_str(") ON CONFLICT DO UPDATE SET "); + first = true; + for (idx, _value) in values.fields.iter().enumerate() { + if let Some(value_field) = self.value_fields_schema.get(idx) { + if !first { + sql.push_str(", "); + } + write!(sql, "{0} = excluded.{0}", value_field.name).unwrap(); + first = false; + } + } Ok((sql, params)) } @@ -342,22 +357,24 @@ impl D1ExportContext { &self, key: &KeyValue, ) -> Result<(String, Vec), RecocoError> { - let mut where_clauses = vec![]; - let mut params = vec![]; + use std::fmt::Write; + let mut params = Vec::with_capacity(self.key_fields_schema.len()); + // ⚡ Bolt Optimization: Use String::with_capacity and write! to avoid intermediate Vec and format! allocations + let mut sql = String::with_capacity(32 + self.table_name.len() + params.capacity() * 32); + write!(sql, "DELETE FROM {} WHERE ", self.table_name).unwrap(); + let mut first = true; for (idx, _key_field) in self.key_fields_schema.iter().enumerate() { if let Some(key_part) = key.0.get(idx) { - where_clauses.push(format!("{} = ?", self.key_fields_schema[idx].name)); + if !first { + sql.push_str(" AND "); + } + write!(sql, "{} = ?", self.key_fields_schema[idx].name).unwrap(); params.push(key_part_to_json(key_part)?); + first = false; } } - let sql = format!( - "DELETE FROM {} WHERE {}", - self.table_name, - where_clauses.join(" AND ") - ); - Ok((sql, params)) }