Skip to content

Performance Improvements Using Caching - #198

Merged
ccrisan merged 18 commits into
mainfrom
improvements/more-caching
Apr 15, 2026
Merged

Performance Improvements Using Caching#198
ccrisan merged 18 commits into
mainfrom
improvements/more-caching

Conversation

@ccrisan

@ccrisan ccrisan commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI 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.

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 with x.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 to deque with 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 DebouncerDebounced 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 _task was 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.

Comment thread qtoggleserver/peripherals/__init__.py
Comment thread qtoggleserver/core/ports.py
Comment thread qtoggleserver/slaves/devices.py Outdated
Comment thread qtoggleserver/slaves/devices.py
Comment thread tests/integration/expressions/test_misc.py Outdated
Comment thread qtoggleserver/core/expressions/base.py
@ccrisan
ccrisan merged commit cae0926 into main Apr 15, 2026
14 checks passed
@ccrisan
ccrisan deleted the improvements/more-caching branch April 15, 2026 20:43
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