[smart-log] perf: optimize stack discovery with batch fetch #41
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