feat(pulse-scheduler): optimize gas when querying active subscriptions #2604
  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
Optimize gas usage when querying for all active subscriptions.
Rationale
This is one the most frequent access patterns in scheduler, since keepers need to poll the set of active subscriptions periodically to keep their state consistent. It's a view function so it will just be simulated, but since we could potentially store thousands of subscriptions, we still want to make it efficient.
This optimization sacrifices a bit of gas when activating/deactivating subscriptions (which should be relatively uncommon), for efficiency gains in the
getActiveSubscriptionsfunction. Previously we needed to iterate through all subscriptions to collect the active ones. This doesn't scale well as more subscriptions get deactivated.Now, we store the active subscriptions in an
activeSubscriptionIdsarray for efficient querying and paging, and store a map ofsub id -> index in activeSubscriptionIdsfor efficient removal when deactivating subscriptions.How has this been tested?
Gas benchmark results