Speed up 'BudgetsController#show'

Closed
TE
5mo ago
+113-36

I am currently very interested in this project. However, its slow response time is a drawback. Consequently, after investigating the project using Skylight, I decided to optimize "BudgetsController#show"—the specific endpoint identified as having a slow response time.

Summary

Speed up BudgetsController#show by cutting repeated per-category work in models/helpers and reducing Sentry overhead on hot paths so production traces reflect real app time.

  • BudgetCategory: Memoize actual_spending, available_to_spend, and percent_of_budget_spent; replace per-call SQL in subcategories with an in-memory filter over the already-loaded budget.budget_categories association.
  • Budget: Memoize allocated_spending; route income helpers through the memoized income_statement instead of family.income_statement.
  • BudgetsHelper: Derive budget_has_over_budget? from budget_categories_view_state[:over_budget_count] instead of a second full-category pass.
  • Sentry: Disable log forwarding by default; traces_sampler skips /budgets/*, /reports/*, dashboard, and Rails internals; env-tunable sample rates.
  • Production: Optional Server-Timing when SERVER_TIMING=true.

Test plan

  • Open Budgets from the sidebar; use prev/next, Today, and month picker.
  • [ x] Confirm category cards (spent %, over-budget styling, subcategories with parent budget).
  • [ x] Compare latency with SERVER_TIMING=true if needed.
  • [ x] Confirm Sentry transaction volume for /budgets/* drops while errors still capture.

Summary by CodeRabbit

  • Performance Improvements

    • Implemented caching for budget calculations to reduce computational overhead on repeated requests.
  • Configuration

    • Made error tracking and performance monitoring settings configurable via environment variables for greater operational control.

Review Change Stack