Skip to content

perf: optimize list_gists_by_cell with location_cell index mapping #24

Description

@snowrugar-beep

Problem Statement

The Soroban contract's list_gists_by_cell function iterates over all gist IDs from the cursor to the total count, checking each gist's location_cell for a match. This is an O(n) scan that becomes prohibitively expensive (in terms of computation and fee) as the number of gists grows.

Evidence

  • contracts/src/lib.rs lines 66-85: for id in start..=total { ... if gist.location_cell == location_cell { ... } }
  • No mapping from location_cell to gist IDs exists
  • Contract iterates over every gist regardless of cell

Impact

Contract becomes unusable at scale. Transaction fees increase linearly with total gist count, not with the result set size. Users will be priced out of querying their local area.

Proposed Solution

  1. Add a storage mapping: Map<String, Vec<u64>> mapping location_cell to list of gist IDs
  2. On post_gist, append the new gist_id to the cell's list
  3. Rewrite list_gists_by_cell to read directly from the cell index instead of scanning all gists
  4. Maintain backward compatibility of the function signature
  5. Add cursor pagination within the cell's gist list

Technical Requirements

  • Must maintain backward compatibility
  • Must handle gists with author set (Option)
  • Must use Soroban SDK's Vec efficiently
  • Must not exceed Soroban contract size limits

Acceptance Criteria

  1. list_gists_by_cell returns same results as before for existing data
  2. Time complexity is O(k) where k = gists in cell (was O(n) where n = total gists)
  3. 1000 gists in different cells does not increase query cost for a single cell
  4. contract size stays under Soroban limits
  5. All existing tests pass
  6. New test: 1000 gists across 10 cells, verify cell query returns correct subset

File Inventory

  • contracts/src/lib.rs

Dependencies

Issue #1 (deduplicate contracts) should be done first to avoid wasted work.

Testing Strategy

  • Unit test: post 100 gists, query cell, verify correct count
  • Performance test: post 1000 gists, measure fee difference for cell query
  • Edge cases: empty cell, cell with 1 gist, cell with max gists

Security Considerations

More efficient contract execution reduces attack surface for DoS via expensive queries.

Definition of Done

  • Index mapping implemented
  • O(k) query working
  • All tests passing
  • Contract size within limits

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions