Skip to content

Fix #1926: feat: improve MemOS performance#1944

Closed
Memtensor-AI wants to merge 1 commit into
dev-20260615-v2.0.20from
feature/autodev-1926
Closed

Fix #1926: feat: improve MemOS performance#1944
Memtensor-AI wants to merge 1 commit into
dev-20260615-v2.0.20from
feature/autodev-1926

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

Improves the textual-memory hot path in GeneralTextMemory (src/memos/memories/textual/general.py) with three small, isolated, backwards-compatible optimizations endorsed by the Phase-1 timeout default scope.

Specifically: (1) a per-instance bounded LRU cache (size 256) on _embed_one_sentence so identical queries within a process — common in retry / rerank loops, repeated chat lookups, and update() calls that reuse a recent search() text — skip the embedder round-trip; cached embeddings are deep-copied on the way in and on the way out so downstream mutation cannot poison cache state. (2) Drops a redundant in-process sorted(..., reverse=True) in search(): Qdrant's query_points already returns hits in descending score order; a regression test now guards that contract. (3) Switches TextualMemoryItem(**payload) to TextualMemoryItem.model_validate(payload) on the read paths (search, get, get_by_ids, get_all) — equivalent semantics on a dict input and the recommended Pydantic v2 idiom.

Tests: adds 4 new unit cases in tests/memories/textual/test_general.py covering cache hit, LRU eviction, search-ordering preservation, and search↔update cache sharing. All 62 textual-memory tests pass. Broader smoke (tests/memories/ tests/mem_os/ tests/mem_cube/) shows 101 passed; 2 test_kv failures are pre-existing transformers DynamicCache API drift reproduced on the unchanged base and unrelated to this PR. Ruff lint + format both clean. No public API change, no schema change, no new dependency, no API contract change. Branch feature/autodev-1926 pushed to origin at commit ecdd118a; specs archived to memos-autodev-specs main.

Related Issue (Required): Fixes #1926

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Not run; documentation-only change.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@MatthewZhuang, @CarltonXiang, @syzsunshine219, @World-controller please review this PR.

Reviewer Checklist

Three targeted hot-path improvements to GeneralTextMemory:

- Add a per-instance bounded LRU cache (size 256) on `_embed_one_sentence`
  so identical queries within a process — common during retry / rerank
  loops, repeated chat lookups, and update() calls that reuse a recent
  search() text — skip the embedder round-trip. Cached values are deep-
  copied on the way in and out so downstream mutation cannot poison the
  cache.

- Drop the redundant `sorted(..., reverse=True)` in `search`. Qdrant's
  `query_points` already returns hits in descending-by-score order;
  the in-process sort was a no-op. A regression test now guards the
  contract.

- Use `TextualMemoryItem.model_validate(payload)` instead of
  `TextualMemoryItem(**payload)` on the read paths (`search`, `get`,
  `get_by_ids`, `get_all`) — equivalent semantics on a dict input and
  the recommended Pydantic v2 idiom.

Adds 4 unit tests under tests/memories/textual/test_general.py covering
cache hit, LRU eviction, search ordering preservation, and search↔update
cache sharing. All 62 textual-memory tests pass; ruff format + check
clean. No public API change, no schema change, no new dependency.

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

Labels

ai-generated enhancement New feature or improvement | 新功能或改进

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants