Skip to content

Conversation

@luqven
Copy link
Owner

@luqven luqven commented Dec 29, 2025

Summary

Optimize stack discovery by replacing sequential API calls with a batch-fetch strategy. This dramatically improves performance for the gh-stack log and gh-stack status commands.

Problem

The previous implementation made O(N+M) sequential API calls to walk the PR chain:

  • Walk UP: fetch_pr_by_head() for each ancestor
  • Walk DOWN: fetch_prs_by_base() for each descendant

For a 6-PR stack, this meant ~12 HTTP requests with network latency (~100-500ms each), resulting in 6+ seconds of wait time.

Solution

Batch fetch all open PRs first, then walk the chain in-memory:

  1. Single paginated API call to fetch all open PRs
  2. Build in-memory PrIndex for fast head/base lookups
  3. Walk the chain without any additional API calls

Changes

Modified: src/api/stack.rs

  • Add pagination to fetch_all_open_prs() - supports up to 1000 PRs (10 pages)
  • Add PrIndex struct for O(1) lookup by head or base branch
  • Refactor discover_stack() to use batch fetch + in-memory walk
  • Extract discover_stack_from_index() for pure in-memory operation
  • Extract group_into_stacks() for code reuse

Performance

Scenario Before After
6-PR stack ~12 API calls (~6s) 1-2 API calls (~2s)
Any stack size O(N) calls O(1) calls

Enterprise Support

  • Pagination handles repositories with 100+ open PRs
  • Capped at 1000 PRs (10 pages) for safety and to avoid runaway requests

Tests

11 new tests added:

  • test_pr_index_get_by_head / get_by_base
  • test_discover_stack_from_index_* (4 scenarios)
  • test_group_into_stacks_* (3 scenarios)
  • test_fetch_all_open_prs_pagination
  • test_discover_stack_batch_fetch

Stacked PR Chain: smart-log

PR Title Merges Into
#35 feat: add stack discovery API -
#36 feat: add rate limit retry logic #35
#37 feat: add identifier detection and prompts #36
#38 feat: add gh CLI helpers #37
#39 feat: make log and status identifier optional with branch inference #38
#40 docs: update log and status documentation for smart defaults #39
#41 👉perf: optimize stack discovery with batch fetch #40
#42 perf: parallelize status check fetches #41

Replace sequential API calls with batch-fetch strategy:
- Fetch all open PRs in one paginated request (up to 1000 PRs)
- Walk the PR chain in-memory using PrIndex lookup structure
- Reduces API calls from O(N) to O(1) for stack discovery

Changes:
- Add pagination support to fetch_all_open_prs (MAX_PAGES=10)
- Add PrIndex struct for fast in-memory head/base lookups
- Refactor discover_stack to use batch fetch + in-memory walk
- Extract discover_stack_from_index for pure in-memory operation
- Extract group_into_stacks for reuse between functions

Performance improvement:
- Before: ~12 sequential API calls for 6-PR stack (~6+ seconds)
- After: 1-2 API calls for any stack size (~2 seconds)

Enterprise support:
- Pagination handles repos with 100+ open PRs
- Capped at 1000 PRs (10 pages) for safety

Adds 11 new tests for PrIndex, batch fetch, and pagination.
@luqven
Copy link
Owner Author

luqven commented Dec 30, 2025

Landed via #42

@luqven luqven closed this Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants