diff --git a/.jules/bolt.md b/.jules/bolt.md index fb3e8f1..855a63e 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-10 - [Dynamic SQL Generation Allocation Overhead] +**Learning:** For dynamic SQL generation (e.g., in Cloudflare D1 targets), using intermediate `Vec` allocations and `format!` or `join()` causes unnecessary O(n) heap allocations and memory churn. +**Action:** Always construct queries directly using `String::with_capacity` and the `write!` macro (via `std::fmt::Write`) to minimize memory allocation overhead and improve latency. diff --git a/crates/flow/src/targets/d1.rs b/crates/flow/src/targets/d1.rs index e45fd52..a9bbb31 100644 --- a/crates/flow/src/targets/d1.rs +++ b/crates/flow/src/targets/d1.rs @@ -300,40 +300,67 @@ 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; + + // ⚡ Bolt: Optimize SQL generation by pre-allocating string capacity and avoiding + // intermediate `Vec` allocations for columns, placeholders, and updates. + let mut sql = String::with_capacity( + 128 + self.key_fields_schema.len() * 16 + self.value_fields_schema.len() * 32, + ); + let mut params = + Vec::with_capacity(self.key_fields_schema.len() + self.value_fields_schema.len()); + + write!(sql, "INSERT INTO {} (", self.table_name) + .map_err(|e| RecocoError::internal_msg(e.to_string()))?; + + let mut first = true; + let mut num_cols = 0; // Extract key parts - KeyValue is a wrapper around Box<[KeyPart]> - for (idx, _key_field) in self.key_fields_schema.iter().enumerate() { + 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 { + write!(sql, ", ").unwrap(); + } + first = false; + write!(sql, "{}", key_field.name).unwrap(); params.push(key_part_to_json(key_part)?); + num_cols += 1; } } // 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 { + write!(sql, ", ").unwrap(); + } + first = false; + write!(sql, "{}", value_field.name).unwrap(); params.push(value_to_json(value)?); - update_clauses.push(format!( - "{} = excluded.{}", - value_field.name, value_field.name - )); + num_cols += 1; } } - let sql = format!( - "INSERT INTO {} ({}) VALUES ({}) ON CONFLICT DO UPDATE SET {}", - self.table_name, - columns.join(", "), - placeholders.join(", "), - update_clauses.join(", ") - ); + write!(sql, ") VALUES (").unwrap(); + for i in 0..num_cols { + if i > 0 { + write!(sql, ", ").unwrap(); + } + write!(sql, "?").unwrap(); + } + + write!(sql, ") ON CONFLICT DO UPDATE SET ").unwrap(); + let mut first_update = true; + for (idx, _value) in values.fields.iter().enumerate() { + if let Some(value_field) = self.value_fields_schema.get(idx) { + if !first_update { + write!(sql, ", ").unwrap(); + } + first_update = false; + write!(sql, "{0} = excluded.{0}", value_field.name).unwrap(); + } + } Ok((sql, params)) } @@ -342,22 +369,29 @@ impl D1ExportContext { &self, key: &KeyValue, ) -> Result<(String, Vec), RecocoError> { - let mut where_clauses = vec![]; - let mut params = vec![]; + use std::fmt::Write; + + // ⚡ Bolt: Optimize SQL generation by pre-allocating string capacity and avoiding + // intermediate `Vec` allocations for the WHERE clauses. + let mut sql = String::with_capacity(64 + self.key_fields_schema.len() * 16); + let mut params = Vec::with_capacity(self.key_fields_schema.len()); - for (idx, _key_field) in self.key_fields_schema.iter().enumerate() { + write!(sql, "DELETE FROM {} WHERE ", self.table_name) + .map_err(|e| RecocoError::internal_msg(e.to_string()))?; + + 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 { + write!(sql, " AND ").unwrap(); + } + first = false; + write!(sql, "{} = ?", key_field.name).unwrap(); params.push(key_part_to_json(key_part)?); } } - let sql = format!( - "DELETE FROM {} WHERE {}", - self.table_name, - where_clauses.join(" AND ") - ); - Ok((sql, params)) }