perf: cache plugin_names.json with lru_cache for O(1) lookups#353
Open
sharma-sugurthi wants to merge 2 commits intojenkinsci:mainfrom
Open
perf: cache plugin_names.json with lru_cache for O(1) lookups#353sharma-sugurthi wants to merge 2 commits intojenkinsci:mainfrom
sharma-sugurthi wants to merge 2 commits intojenkinsci:mainfrom
Conversation
Contributor
|
Looks good. I believe in this case, we will need an integration test to check if it really accesses the cache for the second, third, and more calls. |
- Add load_plugin_names() with @lru_cache to read file once at first access - Replace O(n) linear scan with frozenset O(1) membership check - Extract shared tokenize_plugin_name() helper (removes duplicate tokenize()) - Add 10 tests: public API validation + integration tests for cache behavior Fixes jenkinsci#352
e7bc690 to
47c19ed
Compare
Contributor
Author
|
removed TestTokenizePluginName and TestLoadPluginNames classes entirely and all tests now exercise only the public API (is_valid_plugin, load_plugin_names, filter_retrieved_data),also added a TestPluginNameCacheIntegration class with 3 integration tests that verify the cache works correctly (file read once across multiple calls, shared cache across functions, cache_clear forces reload) as you suggested. |
berviantoleo
approved these changes
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #352
is_valid_plugin()was re-readingplugin_names.jsonfrom disk and performing an O(n) linear scan on every invocation. since this function is called during everysearch_plugin_docs()tool execution, it adds unnecessary disk I/O latency to every plugin-related query.Changes
chatbot-core/api/tools/utils.py_load_plugin_names()- readsplugin_names.jsononce on first access using@functools.lru_cache(maxsize=1), stores tokenized names in afrozensetfor O(1) membership checks._tokenize_plugin_name()- shared module-level helper that normalizes plugin names (strips hyphens, spaces, lowercases). Eliminates the duplicatetokenize()closures previously defined in bothis_valid_plugin()andfilter_retrieved_data().is_valid_plugin()- now a one-liner using cached set lookup.filter_retrieved_data()- uses the shared_tokenize_plugin_name()helper.chatbot-core/tests/unit/tools/test_utils.py[NEW]_tokenize_plugin_name()- case, hyphens, spaces, empty string_load_plugin_names()- frozenset return, tokenization, caching (file read once)is_valid_plugin()- exact match, case-insensitive, hyphen-insensitive, invalid namesfilter_retrieved_data()- matching, no match, empty input