Skip to content

⚡ Bolt: Optimize WalletService performance and idempotency#41

Draft
MethasMP wants to merge 1 commit into
mainfrom
bolt-wallet-optimization-9180414245420419233
Draft

⚡ Bolt: Optimize WalletService performance and idempotency#41
MethasMP wants to merge 1 commit into
mainfrom
bolt-wallet-optimization-9180414245420419233

Conversation

@MethasMP

Copy link
Copy Markdown
Owner

This PR optimizes several hot paths in WalletService to reduce latency and database load.

💡 What

  1. Atomic Idempotency: In ProcessPayment, replaced the two-step SELECT EXISTS + INSERT with a single INSERT ... ON CONFLICT DO NOTHING. This reduces database roundtrips and serialization conflict risks.
  2. String Concatenation: Replaced fmt.Sprintf with manual string concatenation in GetExchangeRate, ProcessPayment, and PayoutToPromptPay.
  3. Optimized JSON construction: In ProcessPayment, used strconv.FormatFloat and strconv.Quote for manual JSON building, which is faster than fmt.Sprintf and safer against special characters in merchant names.
  4. Benchmark Suite: Added internal/usecase/wallet_service_perf_test.go to provide a baseline and prevent future performance regressions.

🎯 Why

Reflection-heavy functions like fmt.Sprintf and multiple database roundtrips add unnecessary overhead to high-frequency payment and exchange rate operations.

📊 Impact

  • Cache Key Generation: ~3.5x speedup (~132ns vs ~38ns/op).
  • JSON Construction: ~2.2x speedup (~979ns vs ~436ns/op).
  • Database Latency: Reduced roundtrips for every successful payment.

🔬 Measurement

Run cd back-end && go test -bench . internal/usecase/wallet_service_perf_test.go to verify the micro-benchmarks.


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

- Implement atomic idempotency in `ProcessPayment` using `ON CONFLICT DO NOTHING`.
- Replace `fmt.Sprintf` with string concatenation for cache keys and descriptions.
- Use `strconv` and manual concatenation for performant and safe JSON construction.
- Hoist `strings.ToUpper` in `GetExchangeRate` to avoid redundant calls.
- Add `wallet_service_perf_test.go` to measure and verify optimizations.

Performance impact:
- Cache key generation: ~132ns -> ~38ns (~3.5x faster)
- JSON payload construction: ~979ns -> ~436ns (~2.2x faster)
- Reduced database roundtrips for payment processing.

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 Jun 29, 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 ↗︎.

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