Skip to content

⚡ Bolt: Optimize single-model insert compilation performance#149

Closed
cungminh2710 wants to merge 2 commits into
mainfrom
bolt-optimize-single-model-insert-15259031699631847227
Closed

⚡ Bolt: Optimize single-model insert compilation performance#149
cungminh2710 wants to merge 2 commits into
mainfrom
bolt-optimize-single-model-insert-15259031699631847227

Conversation

@cungminh2710

Copy link
Copy Markdown
Contributor

This PR optimizes the SQL compilation path for single-model INSERT statements in Rain ORM.

By identifying that most inserts use a single model without additional .Set() overrides, we now bypass the expensive creation of intermediate assignment slices and map lookups. Instead, writeSingleModelSQL streams values directly from the model fields to the compilation context.

Additionally, we've optimized the assignment resolution logic with fast-paths for common primitive types (int64, int, string, bool), avoiding the overhead of reflect.Value.Interface() and associated boxing where possible.

Verification:

  • New benchmark BenchmarkInsertToSQL/SingleModel shows a ~69% reduction in allocations and ~56% improvement in compilation speed.
  • Full test suite passed via go test ./pkg/rain/....

PR created automatically by Jules for task 15259031699631847227 started by @cungminh2710

- Implement writeSingleModelSQL to stream SQL directly from models, bypassing intermediate assignment slice allocations.
- Add fast-paths for common primitive types in fieldValueForInsert and insertValueForField to avoid redundant reflection and boxing.
- Add BenchmarkInsertToSQL/SingleModel to track compilation performance.

BenchmarkInsertToSQL/SingleModel (Postgres dialect):
- Allocations: 16 -> 5 allocs/op (~69% reduction)
- Speed: 3415 -> 1503 ns/op (~56% faster)

Co-authored-by: cungminh2710 <8063319+cungminh2710@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@greptile-apps

greptile-apps Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR optimizes single-model insert SQL compilation in Rain ORM. The main changes are:

  • Adds a direct single-model insert writer for .Model() without .Set() overrides.
  • Adds primitive fast paths in model assignment resolution.
  • Adds a benchmark for the optimized single-model insert path.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
pkg/rain/query_insert.go Adds the direct single-model insert SQL path for model-only inserts.
pkg/rain/query_model_assignments.go Adds guarded primitive fast paths for insert value resolution and auto-increment zero checks.
pkg/rain/compile_bench_test.go Adds benchmark coverage for the single-model insert compilation path.

Reviews (2): Last reviewed commit: "refactor(rain): optimize single-model in..." | Re-trigger Greptile

Comment thread pkg/rain/query_model_assignments.go Outdated
Comment thread pkg/rain/query_model_assignments.go Outdated
@cungminh2710

Copy link
Copy Markdown
Contributor Author

@jules Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.


Issue 1 of 2

pkg/rain/query_model_assignments.go:167-176
Primitive Providers Are Bypassed

When a model field uses a named primitive type such as a string or int wrapper with custom insert serialization, this branch returns the raw primitive before the existing interface-based value path can run. That changes Insert().Model(...).ToSQL() from using the custom database value to inserting the raw field contents.

Issue 2 of 2

pkg/rain/query_model_assignments.go:108-127
Auto ID Providers Are Skipped

For an auto-increment column backed by a named primitive field, a zero Go value is dropped here before the custom value path is consulted. A field that intentionally supplies its ID through custom serialization can now be omitted from the INSERT, leaving the database to generate a different value.

@google-labs-jules

Copy link
Copy Markdown
Contributor

@jules Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.


Issue 1 of 2

pkg/rain/query_model_assignments.go:167-176
Primitive Providers Are Bypassed

When a model field uses a named primitive type such as a string or int wrapper with custom insert serialization, this branch returns the raw primitive before the existing interface-based value path can run. That changes Insert().Model(...).ToSQL() from using the custom database value to inserting the raw field contents.

Issue 2 of 2

pkg/rain/query_model_assignments.go:108-127
Auto ID Providers Are Skipped

For an auto-increment column backed by a named primitive field, a zero Go value is dropped here before the custom value path is consulted. A field that intentionally supplies its ID through custom serialization can now be omitted from the INSERT, leaving the database to generate a different value.

I've addressed both issues by restricting the primitive fast-paths to built-in types (where PkgPath() == ""). This ensures that named primitive wrappers or custom providers implementing setValueProvider are correctly identified and processed through the standard interface-based path. Performance gains for standard types are preserved. Ready for review!

…er bypass

- Implement writeSingleModelSQL for direct streaming of single-model inserts.
- Add fast-paths for built-in primitive types in assignment resolution.
- Ensure custom providers (named primitive wrappers) are still consulted by restricting fast-paths to types with PkgPath() == "".
- Add BenchmarkInsertToSQL/SingleModel to track compilation performance.

BenchmarkInsertToSQL/SingleModel:
- Allocations: 16 -> 5 allocs/op (~69% reduction)
- Speed: 3415 -> 1536 ns/op (~55% faster)

Co-authored-by: cungminh2710 <8063319+cungminh2710@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant