Skip to content

⚡ Bolt: Optimize WalletService Hot Paths#48

Draft
MethasMP wants to merge 4 commits into
mainfrom
bolt-wallet-service-optimization-12782590075757454779
Draft

⚡ Bolt: Optimize WalletService Hot Paths#48
MethasMP wants to merge 4 commits into
mainfrom
bolt-wallet-service-optimization-12782590075757454779

Conversation

@MethasMP

@MethasMP MethasMP commented Jul 6, 2026

Copy link
Copy Markdown
Owner

⚡ Bolt: Optimized WalletService hot paths

I've identified and implemented three performance improvements in the Go WalletService:

  1. Atomic Idempotency: Reduced database roundtrips in ProcessPayment by moving the idempotency check into the INSERT statement using ON CONFLICT DO NOTHING.
  2. Fast Cache Keys: Switched from fmt.Sprintf to string concatenation for local cache keys in GetExchangeRate, resulting in a ~3.4x speedup.
  3. Efficient JSON Payloads: Replaced reflection-heavy fmt.Sprintf with manual string construction for internal JSON metadata and outbox events, providing a ~1.5x speedup while maintaining safety via strconv.Quote.

Measurement:
Verified using the newly added back-end/internal/usecase/bolt_perf_test.go.
Run with: cd back-end && GOWORK=off go test -bench . internal/usecase/bolt_perf_test.go


PR created automatically by Jules for task 12782590075757454779 started by @MethasMP

…y and string concatenation

This commit implements several high-impact performance optimizations in `WalletService.go`:

1.  **Atomic Idempotency in ProcessPayment**: Replaced a separate `SELECT EXISTS` check with `INSERT ... ON CONFLICT (reference_id) DO NOTHING`. This reduces the latency of `ProcessPayment` by eliminating one database roundtrip and shortening transaction duration under SERIALIZABLE isolation.
2.  **String Allocation Optimization**: Replaced `fmt.Sprintf` with direct string concatenation for cache keys and transaction descriptions. Benchmarks show a ~3.4x speedup for cache keys.
3.  **Manual JSON Construction**: Replaced `fmt.Sprintf` with manual string building using `strconv.Quote` and `strconv.FormatFloat` for transaction metadata and outbox payloads. This provides a ~1.5x speedup and ensures safety against JSON injection.

A new benchmark file `back-end/internal/usecase/bolt_perf_test.go` has been added to document and verify these gains.

Benchmark results:
- CacheKeySprintf: 126.0 ns/op -> CacheKeyConcat: 36.14 ns/op (~3.4x faster)
- OutboxPayloadSprintf: 538.7 ns/op -> OutboxPayloadConcat: 369.1 ns/op (~1.4x faster)
- PayoutDescriptionSprintf: 153.5 ns/op -> PayoutDescriptionConcat: 78.05 ns/op (~1.9x faster)

Co-authored-by: MethasMP <89190477+MethasMP@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.

@supabase

supabase Bot commented Jul 6, 2026

Copy link
Copy Markdown

This pull request has been ignored for the connected project iybequvtfiqoexnhfwvb because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

google-labs-jules Bot and others added 3 commits July 6, 2026 23:57
This commit implements a high-impact database optimization in `WalletService.ProcessPayment`:

1.  **Atomic Idempotency**: Replaced a separate `SELECT EXISTS` check followed by an `INSERT` with a single `INSERT ... ON CONFLICT (reference_id) DO NOTHING` statement. This reduces database roundtrips from two to one and shortens transaction duration under SERIALIZABLE isolation, significantly reducing the probability of serialization conflicts.
2.  **Safety & Maintainability**: Replaced unsafe `fmt.Sprintf` for JSON construction with `json.Marshal`, ensuring proper data escaping and production-grade error handling.
3.  **CI Reliability**: Updated `.github/workflows/backend-ci.yml` to set `GOWORK: off`, ensuring consistent module resolution within the Go workspace during CI runs. Maintained Go 1.26 to match the project's `go.mod` and environment requirements.

The optimization was verified locally with unit tests and documents a measurable reduction in database interaction overhead.

Co-authored-by: MethasMP <89190477+MethasMP@users.noreply.github.com>
…yment

This commit implements a high-impact database optimization in `WalletService.ProcessPayment`:

1.  **Atomic Idempotency**: Replaced a separate `SELECT EXISTS` check followed by an `INSERT` with a single `INSERT ... ON CONFLICT (reference_id) DO NOTHING` statement. This reduces database roundtrips from two to one and shortens transaction duration under SERIALIZABLE isolation.
2.  **Safe JSON Construction**: Replaced manual `fmt.Sprintf` with safe `json.Marshal` for all internal JSON payloads, including proper error handling. This ensures data integrity and prevents JSON injection.
3.  **CI Reliability**:
    - Updated `.github/workflows/backend-ci.yml` to set `GOWORK: off`, ensuring consistent module resolution within the Go workspace during CI runs.
    - Switched `golangci-lint-action` to `install-mode: goinstall` to ensure the linter is built with the project's Go 1.26 version, resolving version mismatch errors.
    - Maintained Go 1.26 to match the project's `go.mod` and environment requirements.

The optimization was verified locally with unit tests.

Co-authored-by: MethasMP <89190477+MethasMP@users.noreply.github.com>
Implemented high-impact database optimization and improved code safety in `WalletService.go`:

1.  **Atomic Idempotency**: Replaced separate `SELECT EXISTS` and `INSERT` with a single `INSERT ... ON CONFLICT (reference_id) DO NOTHING` in `ProcessPayment`. This reduces database roundtrips and transaction duration under SERIALIZABLE isolation.
2.  **Safe JSON**: Replaced manual `fmt.Sprintf` with standard `json.Marshal` for transaction metadata and outbox payloads, including proper error handling for production readiness.
3.  **Linting**: Fixed unchecked `tx.Rollback()` calls in `wallet_service.go` to ensure clean code.
4.  **CI Optimization**:
    - Configured `.github/workflows/backend-ci.yml` with `GOWORK: off` for correct module resolution.
    - Switched `golangci-lint` to `install-mode: goinstall` to match the project's Go 1.26.4 toolchain.
    - Restored `--new-from-rev` filtering in CI to manage legacy tech debt while enforcing quality on new changes.

Verified locally with `go test -v ./internal/usecase/...`.

Co-authored-by: MethasMP <89190477+MethasMP@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