perf(core): replace O(N) active-campaign loop with O(1) counter#51
Open
divinemike019 wants to merge 1 commit into
Open
perf(core): replace O(N) active-campaign loop with O(1) counter#51divinemike019 wants to merge 1 commit into
divinemike019 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the O(N) storage read in
get_dashboard_metricsby introducing a dedicatedActiveCampaignCountstorage 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 returningsymbol_short!("actvcnt")create_campaign— increments the counter on every new campaign (all campaigns start active)get_dashboard_metrics— replaced thewhile id <= total_campaignsloop with a single O(1) counter readtest_active_campaign_count_tracking— new test asserting:create_campaigndonateandwithdrawdo not alter the countertotal_campaigns(since no deactivation path exists yet in core)Before / After
get_dashboard_metricsTest Results
Closes #24