Skip to content

perf(apps): avoid quadratic scans of compacted session history - #7568

Closed
LingYi-Liang wants to merge 1 commit into
google:mainfrom
LingYi-Liang:perf/compaction-interval-lookup
Closed

LingYi-Liang wants to merge 1 commit into
google:mainfrom
LingYi-Liang:perf/compaction-interval-lookup

Conversation

@LingYi-Liang

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Problem: Compacted histories still do all-pairs range checks and scan every retained range for each raw event. The cost grows quadratically with session history.

Solution: Sweep sorted ranges to keep non-subsumed summaries, then use binary search for raw-event coverage. Keep the existing overlap, tie and event-order semantics.

In a local CPU fixture with 1,024 summaries and 8,193 raw events, request-content assembly drops from 122 ms to 26 ms with identical output. This measures preprocessing, not model inference.

Testing Plan

  • Added regression tests; relevant tests pass locally.
  • Ran the real Runner locally with the repository's MockModel.
Tests and reproduction

Windows, Python 3.12.14; base f78149a.

pytest -q tests/unittests/apps tests/unittests/flows/llm_flows/context
# 294 passed, 1 xfailed (existing app-name validation case)
python tests/benchmarks/benchmark_compaction.py --output result.json

Two comparison-count regressions fail on the base and pass here, without wall-clock assertions. Also checked 300 deterministic randomized histories against the direct containment/coverage rules.

Same fixture and environment, one warmup then five samples per case; median _get_contents time in milliseconds:

Summaries / raw events Base Patch
64 / 513 1.75 1.47
256 / 2,049 11.37 5.98
1,024 / 8,193 122.39 25.87

The benchmark emits all samples and output hashes. Copy the same script to a base checkout for comparison; all three output hashes match. These are synthetic CPU timings, not production latency or throughput claims.

The existing Runner/SQLite/rewind E2E cases are included in the pytest command. A separate Runner probe loaded 128 summaries through InMemorySessionService and used the existing MockModel; both versions retained every summary, removed covered raw events, produced done, and sent identical request contents. No network model call was made. Scoped formatting, spelling, compliance and documentation-link checks pass. Full-repository and Linux tests were not run.

Checklist

  • Read the contribution guidelines.
  • Performed a human self-review of the complete diff.
  • Commented the non-obvious interval handling.
  • Added tests and a reproducible benchmark.
  • Relevant existing tests pass with the change.
  • Tested the integrated Runner path locally with MockModel.
  • No dependent changes are needed.

Use a sorted range sweep and binary search for compacted history, preserving summary and event ordering.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes the compaction range calculation and subsumption checks to avoid O(N^2) all-pairs interval comparisons. It introduces _get_compaction_ranges in compaction.py using sorting, and leverages binary search (bisect_right) in _compaction.py to check if timestamps are compacted. Additionally, new benchmarks and unit tests are added to verify performance scaling and correctness of the interval-query behavior. There are no review comments to address.

copybara-service Bot pushed a commit that referenced this pull request Oct 16, 2026
@adk-bot

adk-bot commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Thank you @LingYi-Liang for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 2a28c3e.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 16, 2026
@adk-bot adk-bot closed this Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants