Skip to content

Python: Copy only the requested in-memory search page - #8544

Open
emme1t (emme1t) wants to merge 1 commit into
microsoft:mainfrom
emme1t:perf/in-memory-search-page-copy
Open

emme1t (emme1t) wants to merge 1 commit into
microsoft:mainfrom
emme1t:perf/in-memory-search-page-copy

Conversation

@emme1t

@emme1t emme1t (emme1t) commented Sep 19, 2026

Copy link
Copy Markdown

Motivation & Context

InMemoryCollection.search() copies every matching record before selecting a result page. Small-page vector retrieval therefore allocates copies of vectors and nested data from the entire matching collection. This change limits defensive copying to the selected page.

Description & Review Guide

  • What are the major changes? Keep internal record references while scoring and sorting, then deep-copy the selected page before returning SearchResults. Add parameterized tests for paged results with vectors included and excluded.
  • What is the impact of these changes? Existing ordering, thresholds, paging, total-count metadata, and mutable-result isolation are preserved. A local public-SDK benchmark with 2,000 records, 768-dimensional vectors, top=3, and skip=4 reduced peak search allocations from 14,696,088 to 463,508 bytes. Five untraced timing runs had medians of 3.098 seconds before and 2.046 seconds after the change on the same Windows machine. An independent rerun confirmed the same 463,508-byte peak and result semantics. These measurements apply to this workload and exclude collection setup.
  • What do you want reviewers to focus on? The copy boundary in _inner_search and the tests covering mutable tags and vectors in a non-first result page. The scoring/sorting section contains no suspension point, and returned results receive their defensive copies before callers can consume them.

The 387 related in-memory/vector tests pass, including an independent rerun. Core package lint, changed-file formatting, strict source Pyright, all five test type checkers, and applicable prek hooks pass. The core wheel and source distribution build successfully; the subsequent Poe move-dist task requires sh, which is unavailable in this Windows environment. Full core Pyright reported 1,312 errors in the reduced dependency environment, including unresolved optional provider/MCP imports; that broader run has no baseline comparison. Full workspace and external-service integration tests were not run.

Related Issue

Fixes #8543

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

Prepared with Codex using Astra Ultra as part of an automated PR project contributing verified improvements to the open-source community. Implementation, tests, benchmarking, and an independent review were performed by Codex agents.

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The focused optimization preserves ordering, counts, paging, and result isolation with appropriate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Optimizes in-memory vector search by deferring defensive copying until after paging.

Changes:

  • Retains record references during scoring and sorting, then deep-copies only the selected page.
  • Adds paged-search tests covering mutable-data isolation with vectors included and excluded.
File Description
python/​packages/​core/​agent_framework/​_in_memory.py Moves deep-copying to the final paged result set.
python/​packages/​core/​tests/​core/​test_in_memory.py Verifies paging metadata, vectors, and defensive-copy behavior.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@emme1t

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: In-memory vector search copies every match before paging

2 participants