Skip to content

perf(core): replace O(N) active-campaign loop with O(1) counter#51

Open
divinemike019 wants to merge 1 commit into
MillestoneX:mainfrom
divinemike019:fix/active-campaign-count-o1
Open

perf(core): replace O(N) active-campaign loop with O(1) counter#51
divinemike019 wants to merge 1 commit into
MillestoneX:mainfrom
divinemike019:fix/active-campaign-count-o1

Conversation

@divinemike019

Copy link
Copy Markdown
Contributor

Summary

Fixes the O(N) storage read in get_dashboard_metrics by introducing a dedicated ActiveCampaignCount storage key (actvcnt) that is maintained incrementally, reducing the active-campaign lookup from a full scan of every campaign to a single instance-storage read.

Changes

  • active_campaign_count_key() — new storage key helper returning symbol_short!("actvcnt")
  • create_campaign — increments the counter on every new campaign (all campaigns start active)
  • get_dashboard_metrics — replaced the while id <= total_campaigns loop with a single O(1) counter read
  • test_active_campaign_count_tracking — new test asserting:
    • counter starts at 0 on a fresh contract
    • increments by 1 for each create_campaign
    • donate and withdraw do not alter the counter
    • counter equals total_campaigns (since no deactivation path exists yet in core)
  • Updated test snapshots for all affected tests

Before / After

Before After
Storage reads in get_dashboard_metrics O(N) — one per campaign O(1) — single counter read
Soroban resource budget risk Exceeds limits at ~10 000 campaigns Constant regardless of campaign count

Test Results

Closes #24

Introduce ActiveCampaignCount storage key (symbol_short!("actvcnt"))
incremented on every create_campaign call (all campaigns start active).
get_dashboard_metrics now reads this counter directly instead of
iterating over every stored campaign, reducing the view call from O(N)
persistent-storage reads to O(1).

- Add active_campaign_count_key() helper in core/lib.rs
- Increment counter in create_campaign
- Replace while-loop in get_dashboard_metrics with single instance read
- Add test_active_campaign_count_tracking covering increment per
  create_campaign, no side-effects from donate/withdraw, and equality
  with total_campaigns
- Update test snapshots for all affected tests

Closes MillestoneX#24
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.

get_dashboard_metrics reads every campaign in core/lib.rs` — quadratic in storage cost

1 participant