⚡ Bolt: [performance improvement] Optimize D1 SQL string allocation#275
⚡ Bolt: [performance improvement] Optimize D1 SQL string allocation#275bashandbone wants to merge 1 commit into
Conversation
Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Reviewer's GuideOptimizes D1 upsert SQL construction to reduce heap allocations while also making minor readability and lifetime-related cleanup in AST and rule engine modules, and documenting the performance pattern in .jules/bolt.md. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In
build_upsert_stmt, allwrite!calls currently discard theirResult; consider at least usingexpector propagating the error so that formatting failures don’t get silently ignored. - The changes to rule-engine lifetimes (
check_var_in_constraints,check_var_in_transform) and formatting tweaks in other modules seem orthogonal to the D1 SQL performance optimization; consider keeping such refactors in a separate PR to keep diffs focused.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `build_upsert_stmt`, all `write!` calls currently discard their `Result`; consider at least using `expect` or propagating the error so that formatting failures don’t get silently ignored.
- The changes to rule-engine lifetimes (`check_var_in_constraints`, `check_var_in_transform`) and formatting tweaks in other modules seem orthogonal to the D1 SQL performance optimization; consider keeping such refactors in a separate PR to keep diffs focused.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
Optimizes SQL statement construction in the D1 export target to reduce heap allocations on high-throughput upsert paths, alongside small refactors/formatting cleanups in the rule engine and AST engine, plus a Bolt note update.
Changes:
- Reworked
D1ExportContext::build_upsert_stmtto build SQL into a preallocatedStringusingstd::fmt::Write, avoiding intermediateVec<String>/.join()allocations. - Minor rule-engine signature cleanup (remove unnecessary lifetimes) and formatting-only adjustments.
- Added a Bolt note describing the direct SQL formatting optimization.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rule-engine/src/rule/referent_rule.rs | Formatting-only change to condense read() implementation. |
| crates/rule-engine/src/rule/mod.rs | Formatting-only change for defined_vars() mapping/collection. |
| crates/rule-engine/src/check_var.rs | Removes unnecessary lifetimes from helper function signatures. |
| crates/flow/src/targets/d1.rs | Core change: optimized upsert SQL string construction to reduce allocations. |
| crates/ast-engine/src/tree_sitter/mod.rs | Formatting-only adjustments in string conversion and a test assertion. |
| .jules/bolt.md | Adds a new Bolt performance note about direct SQL string formatting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sql.push_str(") VALUES ("); | ||
| for i in 0..params.len() { | ||
| if i > 0 { | ||
| sql.push_str(", "); | ||
| } | ||
| sql.push('?'); | ||
| } | ||
| let _ = write!(sql, ") ON CONFLICT DO UPDATE SET {}", update_clauses); | ||
|
|
| ## 2026-04-10 - [Performance: Direct SQL String Formatting] | ||
| **Learning:** In D1 target components (`build_upsert_stmt`), dynamically generating SQL using intermediate `Vec` allocations combined with `.join()` operations causes unnecessary heap allocations and copying. Constructing the raw SQL query directly via `std::fmt::Write` to a single pre-allocated `String` significantly reduces memory overhead. | ||
| **Action:** For dynamic SQL generation on high-frequency paths, use `String::with_capacity` and `write!` instead of intermediate arrays and `.join()`. |
💡 What:
Replaced dynamic
Stringformat and.join(", ")loop logic withString::with_capacityandstd::fmt::Writeto construct the SQL query directly into a pre-allocated String buffer insidecrates/flow/src/targets/d1.rs(build_upsert_stmt).🎯 Why:
The original approach created multiple intermediate
Veccollections andStringallocations (viaformat!) for generating parameter lists and update clauses. By constructing the SQL string sequentially, we avoid these unnecessary intermediate heap allocations and string copy operations, directly reducing O(n) memory churn for every upsert call on high-throughput database paths.📊 Impact:
Reduces heap allocation per query building substantially. This eliminates intermediate
Vec<String>instances when generating lists of keys and update values.🔬 Measurement:
Run
cargo bench -p thread-flow --bench d1_profiling --features cachingto measure latency, though running it requirescachingfeature. Tests were verified withcargo test -p thread-flow --test d1_target_tests --test d1_minimal_tests --test d1_cache_integration.PR created automatically by Jules for task 16231611141592875904 started by @bashandbone
Summary by Sourcery
Optimize D1 upsert SQL construction to reduce allocations and perform minor API and formatting cleanups across AST and rule engine modules.
Enhancements:
Documentation: