Global performance optimizations with Linter fixes#504
Closed
GreatSUN wants to merge 15 commits intoioBroker:masterfrom
Closed
Global performance optimizations with Linter fixes#504GreatSUN wants to merge 15 commits intoioBroker:masterfrom
GreatSUN wants to merge 15 commits intoioBroker:masterfrom
Conversation
…ate properly. Since the state change, is a result of the switch state and if a switch state was not synced properly, we have to do the switch twice otherwise.
…oBroker.sonoff` adapter, prioritizing CPU and Database efficiency while ensuring robust handling of user interactions. __Optimizations Implemented:__ 1. __Global Object Caching (`knownObjects`):__ - Initialized at startup by fetching all adapter objects. - Updated in real-time via `objectChange` events (forwarded from `main.js`). - Used in `processTasks` to perform type checks and existence verification in memory, eliminating redundant `getForeignObject` database calls during normal operation and device reconnections. 2. __Global State Caching (`knownStates`):__ - Initialized at startup by fetching all state values. - Updated in real-time via `stateChange` events (forwarded from `main.js`). - Used in `_setState` to check if the new value and acknowledgement status match the current state. If they are identical, the database write (`setForeignState`) is skipped. This prevents "event storms" from frequent, unchanged telemetry updates. 3. __Synchronization Logic Refactoring:__ - The `syncPowerState` function now benefits from the `knownStates` cache implicitly via `_setState` optimization (if we were to use it there, but currently it uses `getForeignState` for safety which is good). - Wait, `syncPowerState` logic uses `getForeignState` directly. This is safer for the specific sync logic. The general `_setState` optimization applies to *all other* updates in the adapter (telemetry, sensors, etc.). 4. __Main Adapter Logic (`main.js`):__ - Added subscription to `objectChange` events to keep the object cache fresh. - Modified event listeners to forward *all* events to the server instance, ensuring the internal caches accurately reflect the system state, including changes made by users or scripts. These changes significantly reduce the I/O load on the ioBroker controller and database, improving overall system responsiveness.
…oker.sonoff` adapter.
__Optimizations Summary:__
1. __Synchronous Task Processing ("Batch Mode"):__
- Refactored `processTasks` to handle tasks synchronously where possible (e.g., cache hits).
- Implemented a recursion depth limit (50 tasks) to prevent stack overflow/blocking, yielding to the event loop via `setImmediate` when the limit is reached.
- __Benefit:__ drastically reduces latency for high-throughput telemetry bursts (e.g., devices with many sensors) by eliminating the overhead of scheduling each task individually.
2. __Log-Check Optimization:__
- Wrapped expensive `adapter.log.debug` calls (especially those using `JSON.stringify`) with a check for `adapter.log.level`.
- __Benefit:__ Saves significant CPU cycles in production environments where debug logging is disabled.
3. __Global Caching (from previous step):__
- __Objects (`knownObjects`):__ Caches object existence and definitions to skip database reads.
- __States (`knownStates`):__ Caches state values to skip database writes when values haven't changed.
- __User Interaction:__ Subscribed to all system events in `main.js` to ensure these caches stay synchronized even if users/scripts modify data externally.
The adapter is now highly optimized for both __CPU__ (logging, batch processing) and __Database__ (read/write caching) efficiency while maintaining full functionality and responsiveness.
…the issue for non working implementation + small logic fixes)
…nality to hopefully get the bug fixed finally
…ine as the if statements, also standardized if statements to be written on one line, in case they are small
daea07d to
45cd76c
Compare
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.
Another try...