⚡ Bolt: [performance improvement] Optimize D1 target SQL statement building#248
⚡ Bolt: [performance improvement] Optimize D1 target SQL statement building#248bashandbone wants to merge 1 commit into
Conversation
Refactors `build_upsert_stmt` and `build_delete_stmt` in the D1 target to use `String::with_capacity` and direct `write!` formatting instead of repeatedly creating intermediate vectors and strings using `format!` and `.join()`. Also pre-allocates query parameters vectors to their exact required length. This drastically reduces heap allocations and improves SQL string generation throughput. 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 SQL statement construction for D1 exports by building queries directly into preallocated String buffers and parameter vectors, plus a few minor API-signature and formatting cleanups elsewhere in the codebase. 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 both
build_upsert_stmtandbuild_delete_stmt, you repeatedly useuse std::fmt::Write; let _ = write!(...); sinceStringwrites are infallible, consider either pulling the import to the module level and usingwrite!directly (withoutlet _ =) or adding a small helper to avoid the repeated boilerplate. - The
String::with_capacitycalls in the D1 SQL builders use fairly opaque constants (e.g.,128,64, and per-field multipliers); adding a brief comment explaining how these were chosen or tying them to typical query shapes would make future maintenance and tuning easier.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In both `build_upsert_stmt` and `build_delete_stmt`, you repeatedly use `use std::fmt::Write; let _ = write!(...)`; since `String` writes are infallible, consider either pulling the import to the module level and using `write!` directly (without `let _ =`) or adding a small helper to avoid the repeated boilerplate.
- The `String::with_capacity` calls in the D1 SQL builders use fairly opaque constants (e.g., `128`, `64`, and per-field multipliers); adding a brief comment explaining how these were chosen or tying them to typical query shapes would make future maintenance and tuning easier.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
Refactors D1 SQL statement generation in thread-flow to reduce heap allocations on a hot path by building SQL directly into pre-allocated String buffers and pre-sizing parameter vectors.
Changes:
- Optimized
build_upsert_stmt/build_delete_stmtincrates/flow/src/targets/d1.rsto avoid intermediateVec<String>/format!allocations. - Minor formatting-only refactors across rule-engine and ast-engine modules.
- Added a new performance “learning/action” entry to
.jules/bolt.md.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| crates/flow/src/targets/d1.rs | Builds INSERT/DELETE SQL directly into pre-allocated buffers; pre-allocates params vec capacity. |
| crates/rule-engine/src/rule/referent_rule.rs | Formatting-only refactor of a read-lock chain. |
| crates/rule-engine/src/rule/mod.rs | Formatting-only refactor for defined_vars chain readability. |
| crates/rule-engine/src/check_var.rs | Removes unnecessary explicit lifetimes in helper functions. |
| crates/ast-engine/src/tree_sitter/mod.rs | Formatting-only refactors in string splice and a test assert. |
| .jules/bolt.md | Adds a new entry describing the SQL string-building optimization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| ## 2026-05-20 - [Performance: Direct String Formatting for SQL Queries] | ||
| **Learning:** In hot paths building dynamic strings (like SQL query generators `build_upsert_stmt`), repeatedly using `format!` and creating intermediate vectors for `.join()` results in high memory churn and excessive heap allocations per query. | ||
| **Action:** Use `String::with_capacity` paired with the `write!` macro (`std::fmt::Write`) and exact-capacity `Vec` pre-allocations to build dynamic strings directly into a single buffer, drastically reducing intermediate allocations. |
💡 What
Refactored
build_upsert_stmtandbuild_delete_stmtincrates/flow/src/targets/d1.rsto construct dynamic SQL queries directly into pre-allocatedStringbuffers usingstd::fmt::Write. This replaces the previous pattern of collecting query parts into intermediate vectors (vec![]) and usingformat!with.join(). Query parameter vectors are also now pre-allocated with exact capacities.🎯 Why
The previous implementation performed multiple heap allocations per query to create intermediate strings and vectors, causing unnecessary memory churn on a hot path during data export to Cloudflare D1 targets.
📊 Impact
Significantly reduces heap allocations and string copying overhead per generated SQL query, which improves memory efficiency and query generation throughput during bulk data exports.
🔬 Measurement
Verify the optimizations by running
cargo test -p thread-flow --test d1_target_tests --test d1_minimal_teststo ensure that identical, correct SQL queries are still being generated under the new, faster implementation.PR created automatically by Jules for task 10239718748718497456 started by @bashandbone
Summary by Sourcery
Optimize SQL statement construction for D1 exports and perform minor API and formatting cleanups across supporting crates.
Enhancements:
Documentation: