[smart-log] perf: optimize stack discovery with batch fetch#41
Closed
luqven wants to merge 1 commit intosmart-log/6-docsfrom
Closed
[smart-log] perf: optimize stack discovery with batch fetch#41luqven wants to merge 1 commit intosmart-log/6-docsfrom
luqven wants to merge 1 commit intosmart-log/6-docsfrom
Conversation
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.
This was referenced Dec 29, 2025
Owner
Author
|
Landed via #42 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 stack discovery by replacing sequential API calls with a batch-fetch strategy. This dramatically improves performance for the
gh-stack logandgh-stack statuscommands.Problem
The previous implementation made O(N+M) sequential API calls to walk the PR chain:
fetch_pr_by_head()for each ancestorfetch_prs_by_base()for each descendantFor 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:
PrIndexfor fast head/base lookupsChanges
Modified:
src/api/stack.rsfetch_all_open_prs()- supports up to 1000 PRs (10 pages)PrIndexstruct for O(1) lookup by head or base branchdiscover_stack()to use batch fetch + in-memory walkdiscover_stack_from_index()for pure in-memory operationgroup_into_stacks()for code reusePerformance
Enterprise Support
Tests
11 new tests added:
test_pr_index_get_by_head/get_by_basetest_discover_stack_from_index_*(4 scenarios)test_group_into_stacks_*(3 scenarios)test_fetch_all_open_prs_paginationtest_discover_stack_batch_fetchStacked PR Chain: smart-log