Skip to content

Fix #1926: feat: improve MemOS performance#1942

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

Fix #1926: feat: improve MemOS performance#1942
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

Issue #1926 asks for "better overall MemOS performance" without a specific scenario. A Phase-1 clarification round timed out, so per opsp rules the locked-in default scope applies: targeted, low-risk hot-path wins inside src/memos/memories/textual/general.py only — no public API change, no schema change, no new dependency.

This PR makes three concrete improvements to GeneralTextMemory. First, _embed_one_sentence now consults a per-instance bounded LRU cache (size cap 256) keyed on raw sentence text, so identical queries within a process — common in retry / rerank loops, repeated chat lookups, and update() calls whose memory text matches a recent search() — skip the embedder round-trip; cached values are deep-copied on the way in and out so downstream mutation cannot poison the cache. Second, the redundant sorted(..., reverse=True) in search is removed because qdrant_client.query_points already returns hits in descending-by-score order; a regression test guards the ordering contract. Third, the read paths (search, get, get_by_ids, get_all) construct TextualMemoryItem via model_validate(payload) instead of **payload — equivalent semantics on a dict input and the idiomatic Pydantic v2 path.

Verification: 4 new unit tests in tests/memories/textual/test_general.py cover cache hit, LRU eviction, search ordering preservation, and search↔update cache sharing. Full textual suite passes 62/62. Broader smoke (tests/memories/, tests/mem_os/, tests/mem_cube/) passes 101 tests; 2 pre-existing failures in tests/memories/activation/test_kv.py are an unrelated transformers DynamicCache.key_cache API drift, confirmed reproducible on the unmodified base branch via git stash. Ruff format and ruff check are clean on the touched files.

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