Skip to content

perf: Remove redundant linear scan from contributor registration #865

Description

@Samuel1505

Description

// registry.rs:9-35
pub fn register_contributor(env: &Env, address: &Address, name: &String) -> Result<(), ContractError> {
    let mut index = Storage::get_contributor_index(env);
    for entry in index.iter() {
        if entry.address == *address {
            return Err(ContractError::AlreadyRegistered);
        }
    }
    ...
    index.push_back(entry);
    Storage::set_contributor_index(env, &index);
}

Every call loads the entire global contributor index into memory, scans it linearly for a duplicate, then writes the whole (now one-longer) index back to persistent storage. Registration cost grows linearly with the number of prior registrations, making total registration cost across n users O(n²). This scan is also redundant: lib.rs's contributor_register entrypoint already performs an O(1) duplicate check via Storage::get_contributor before calling into register_contributor, so the linear scan here is pure dead weight on top of that.

As the contributor base grows into the thousands, each new contributor_register call becomes progressively more expensive in CPU/read-bytes, eventually risking Soroban's per-invocation resource limits — a scaling liveness issue for new registrations, not a one-off bug.

Technical Requirements

Files to update

  • contracts/contracts/stellar-grants/src/registry.rs (register_contributor, lines 9-35)

Fix direction

Since lib.rs already does the O(1) duplicate check via Storage::get_contributor before calling this function, remove the redundant linear scan here entirely and rely on the caller's check (or, if this function needs to remain independently safe against duplicate calls, replace the linear scan with the same O(1) Storage::get_contributor lookup instead of scanning the whole index).

Acceptance Criteria

  • register_contributor no longer performs an O(n) scan over the full contributor index.
  • Duplicate registration is still rejected (via the O(1) lookup, whether performed here or relied upon from the caller — document which).
  • A test with many pre-existing contributors confirms registering a new one no longer requires reading the entire index.
  • cargo test passes.

Estimated Effort

Beginner: 3 hours
Intermediate: 1.5 hours
Expert: 1 hour

How to work this issue

  1. Read contracts/ContributionGuide.md for the contribution workflow.
  2. Comment on the issue to claim it before starting.
  3. Branch: fix/issue-920-registry-register-contributor-scan.
  4. Run cargo fmt, cargo clippy -- -D warnings, cargo test before opening your PR.
  5. Use a Conventional Commit message, e.g. perf: remove redundant linear scan from contributor registration.

Before you start

If you find this project interesting, please consider starring the repository on GitHub. It helps the project gain visibility and supports the Drips Wave program that rewards contributors for merged fixes like this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions