Skip to content

Cache packaged stop vocabulary in the hot path - #845

Open
goldyfruit wants to merge 2 commits into
OpenVoiceOS:devfrom
goldyfruit:perf/cache-stop-vocabulary
Open

Cache packaged stop vocabulary in the hot path#845
goldyfruit wants to merge 2 commits into
OpenVoiceOS:devfrom
goldyfruit:perf/cache-stop-vocabulary

Conversation

@goldyfruit

@goldyfruit goldyfruit commented Aug 6, 2026

Copy link
Copy Markdown

Maintainer-requested architecture update

Jarbas correctly requested that resource lifecycle and caching belong to LocaleResources, not to a Core-local subclass.

OpenVoiceOS/ovos-spec-tools#95 now implements that ownership model directly:

  • installed skill/core resources are snapshotted and pre-expanded at construction;
  • dialogs and prompts stay in memory;
  • only user_locale remains live;
  • no bounded LRU, arbitrary capacity, subclass, getattr, or compatibility fallback exists.

The exact-head benchmark on the Core stop path measured 720.327 µs → 112.769 µs per request across en-us and fr-fr: 6.4× faster and 607.558 µs CPU saved per request. Full details and multi-skill measurements are in OpenVoiceOS/ovos-spec-tools#95.

Current state

This branch still contains the superseded _CachedStopResources implementation and must not merge as-is.

After #95 is merged and released, this PR should be reduced to:

  1. rebase onto current dev;
  2. remove _CachedStopResources and its duplicate cache tests;
  3. retain the existing plain LocaleResources(...) construction;
  4. bump the explicit ovos-spec-tools minimum to the released version;
  5. rerun focused, full, and end-to-end validation.

No consumer API switch is otherwise required because LocaleResources now owns the behavior by default. No CI workflow was changed and no upstream PR was merged.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

StopService now uses a cached vocabulary loader. The cache separates entries by vocabulary name and language, limits storage to 32 entries, and clears during shutdown. Unit tests verify reuse, isolation, returned values, and loader calls.

Changes

Stop resource caching

Layer / File(s) Summary
Cached vocabulary loader
ovos_core/intent_services/stop_service.py
_CachedStopResources caches vocabulary loads by base name and language, with an LRU limit of 32 entries.
Service integration and validation
ovos_core/intent_services/stop_service.py, test/unittests/test_stop_resource_cache.py
StopService uses _CachedStopResources and clears the cache during shutdown. Tests verify cache reuse, language and vocabulary-name isolation, returned values, instance isolation, and loader call counts.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: caching packaged stop vocabulary in the StopService hot path.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@goldyfruit
goldyfruit marked this pull request as ready for review August 6, 2026 22:43

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
ovos_core/intent_services/stop_service.py (1)

30-33: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Use an instance-scoped vocabulary cache.

@lru_cache on _CachedStopResources.load_vocabulary() makes self part of the cache key and shares the maxsize=32 limit across (resource, base_name, lang) entries. Localize the cache to the created _CachedStopResources instance and clear it when the service is restarted.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ovos_core/intent_services/stop_service.py` around lines 30 - 33, Replace the
class-level `@lru_cache` on _CachedStopResources.load_vocabulary with an
instance-scoped cache so entries are isolated per resource instance and keyed
only by vocabulary inputs. Initialize the cache when _CachedStopResources is
created, route load_vocabulary through it, and clear that cache during the
service restart/reset flow.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@ovos_core/intent_services/stop_service.py`:
- Around line 30-33: Replace the class-level `@lru_cache` on
_CachedStopResources.load_vocabulary with an instance-scoped cache so entries
are isolated per resource instance and keyed only by vocabulary inputs.
Initialize the cache when _CachedStopResources is created, route load_vocabulary
through it, and clear that cache during the service restart/reset flow.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 56136421-fe51-49b5-b582-4b915876c8c5

📥 Commits

Reviewing files that changed from the base of the PR and between 91021e7 and b9a2d5e.

📒 Files selected for processing (2)
  • ovos_core/intent_services/stop_service.py
  • test/unittests/test_stop_resource_cache.py

from ovos_utils.parse import match_one


class _CachedStopResources(LocaleResources):

@JarbasAl JarbasAl Aug 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why not add a caching option to LocaleResources class directly instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Agreed. I moved the cache into the resource owner in OpenVoiceOS/ovos-spec-tools#95. It is opt-in and instance-local, defaults off so live overrides remain live, returns defensive copies, and has explicit invalidation. Once that is reviewed and released, this PR should replace _CachedStopResources with LocaleResources(..., expanded_cache_size=32) and bump the explicit ovos-spec-tools minimum. I am leaving this thread open until that consumer rebase is complete; no fallback or getattr compatibility path will be added.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants