Skip to content

forge-stream sync_elapsed_streams() is still O(n) even after migration to persistent storage — now reads persistent storage in a loop #333

@Austinaminu2

Description

@Austinaminu2

Summary

sync_elapsed_streams() has been updated to use persistent() storage for stream reads, but is still O(n):

while stream_id < next_id {
    let maybe_stream: Option<Stream> = env.storage().persistent().get(&DataKey::Stream(stream_id));
    // ...
    stream_id += 1;
}

Persistent storage reads are metered the same as instance storage reads. With 1000 streams this still reads 1000 persistent storage entries per get_active_streams_count() call. The migration to persistent storage solved the size limit issue but not the CPU cost issue.

Tasks

  • Remove the O(n) scan entirely from sync_elapsed_streams()
  • Maintain ActiveStreamsCount purely via increment on create_stream() and decrement on cancel_stream() and stream expiry during withdraw()
  • Add expiry detection in withdraw(): if now >= stream.end_time && stream.counted_active, decrement and set counted_active = false
  • Add a test with 20 streams where half have elapsed, verifying get_active_streams_count() is correct without a full scan

Labels: bug, forge-stream, performance, critical

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stellar WaveIssues in the Stellar wave program

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions