Performance Improvements Using Caching - #198
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to improve runtime performance primarily by reducing unnecessary allocations/copies, adding caching for frequently computed JSON/attribute structures, and optimizing some hot-path data structures (e.g., deque usage, debounced batching).
Changes:
- Replace many
dict(x)copies withx.copy()and introduce cached copies/filtered structures for repeated JSON/attrdef generation. - Introduce debounced batching for port
set_attr()side-effects and switch session event queue todequewith a single-pass dedupe. - Optimize JSON persistence ID allocation by caching per-collection max IDs.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/qtoggleserver/peripherals/api/test_funcs.py | Minor payload copy changes in tests (dict() → .copy()). |
| tests/unit/qtoggleserver/mock/peripherals.py | Use .copy() for kwargs cloning in test mock peripheral. |
| tests/unit/qtoggleserver/core/test_sessions.py | New unit tests for Session behavior (queueing, dedupe, limits). |
| tests/unit/qtoggleserver/core/test_ports.py | Update tests for new EvalContext usage + async timing changes + add to_json cache tests. |
| tests/unit/qtoggleserver/core/device/test_attrs.py | Large refactor/reorg of device attrs tests + add cache behavior tests. |
| tests/integration/expressions/test_misc.py | Adjust expression tests timing/ordering (includes a new 1s sleep). |
| qtoggleserver/web/base.py | Use .copy() for kwargs cloning before error handling. |
| qtoggleserver/utils/debounced.py | Rename Debouncer → Debounced and change arg aggregation logic. |
| qtoggleserver/slaves/ports.py | Use .copy() for attrdef cloning + cached attrs copy adjustments. |
| qtoggleserver/slaves/devices.py | Return cached attrs copies, adjust save/to_json payloads, change get_all() return type. |
| qtoggleserver/slaves/api/funcs/devices.py | Use .copy() for schema/properties copies. |
| qtoggleserver/peripherals/peripheral.py | Use .copy() for port args cloning. |
| qtoggleserver/peripherals/init.py | Change get_all() return type + use .copy() for params cloning. |
| qtoggleserver/lib/polled.py | Use .copy() for attrdef cloning. |
| qtoggleserver/lib/filtereventhandler.py | Add TODO comments for expression eval attrs context. |
| qtoggleserver/drivers/persist/redis.py | Use .copy() for filter/record cloning. |
| qtoggleserver/drivers/persist/mongo.py | Use .copy() for filter/record cloning. |
| qtoggleserver/drivers/persist/json.py | Cache max IDs per collection to speed up inserts; .copy() usage. |
| qtoggleserver/core/sessions.py | Switch session queue to deque, single-pass dedupe, tweak accessed timestamp type. |
| qtoggleserver/core/ports.py | Add caching for attrs/attrdefs JSON, introduce debounced post-set handling, change get_all() return type, switch to direct EvalContext usage. |
| qtoggleserver/core/main.py | Comment tweak clarifying expression reevaluation behavior. |
| qtoggleserver/core/expressions/base.py | Make EvalContext.now_ms optional (default 0). |
| qtoggleserver/core/device/attrs.py | Add caching for attrdefs/to_json/attrs, invalidation helpers, .copy() usage. |
| qtoggleserver/core/api/funcs/ports.py | Use .copy() for schema cloning during restore. |
Comments suppressed due to low confidence (1)
qtoggleserver/utils/debounced.py:70
- Debounced._run() clears the shared queue after awaiting the target function. If Debounced.call() is invoked while the debounced function is running (very likely for async funcs that await), those new queued calls will be wiped by the final
_queue.clear()and never executed (no new task is scheduled because_taskwas non-None). Consider snapshotting and clearing the queue before invoking the function, then after completion rescheduling if new items arrived (or looping until the queue stays empty).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
No description provided.