Skip to content

Global performance optimizations with Linter fixes#504

Closed
GreatSUN wants to merge 15 commits intoioBroker:masterfrom
GreatSUN:global_performance_optimizations
Closed

Global performance optimizations with Linter fixes#504
GreatSUN wants to merge 15 commits intoioBroker:masterfrom
GreatSUN:global_performance_optimizations

Conversation

@GreatSUN
Copy link
Contributor

Another try...

…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
@GreatSUN GreatSUN force-pushed the global_performance_optimizations branch from daea07d to 45cd76c Compare December 14, 2025 15:17
@GreatSUN GreatSUN closed this Dec 14, 2025
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.

1 participant