performance optimisation#2238
Open
arteck wants to merge 5 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a set of runtime performance optimizations in the JavaScript adapter (primarily src/main.ts and src/lib/sandbox.ts) plus scheduler bugfixes, and adds several regression/performance-focused test suites to validate the changes.
Changes:
- Replace several hot-path linear scans / repeated allocations with
Set/Maplookups and precomputed structures (e.g.,stateIdSet,subscriptionsObjectMap,sendToinstance cache,timersByScript). - Improve scheduler correctness and timer management (
monthDifffix, start boundary condition, timer leak mitigation, astro “yesterday” calculation, deterministic_getId). - Add new regression and micro-benchmark style tests covering scheduler fixes and performance improvements.
Reviewed changes
Copilot reviewed 20 out of 36 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/testSchedulerBugfixes.js | New regression tests for scheduler bugfixes (monthDiff, timer lifecycle, boundary conditions, astro times, id uniqueness). |
| test/testMainPerformance.js | New correctness + benchmark-style tests for data-structure optimizations (binary remove, Set vs Array). |
| test/performance.test.js | New combined correctness/performance tests for multiple main.ts optimizations. |
| test/performance-improvements.test.js | New baseline/expectation regression tests for planned main.ts performance improvements. |
| test/io-improvements.test.js | New baseline/expectation regression tests for sandbox I/O hot-path optimizations. |
| src/types.d.ts | Update public/internal typings to reflect new Set/Map based structures in runtime context and scripts. |
| src/main.ts | Core performance changes: status text table hoist, state ID Set + binary ops, object subscription map, sendTo cache invalidation, unload safety, reverse name map, timers reverse index, etc. |
| src/lib/scheduler.ts | Scheduler bugfixes + timer management improvements + deterministic ID generation. |
| src/lib/sandbox.ts | Sandbox hot-path optimizations (Object.assign, Set-based dedupe, Set-backed adapterSubs/channels/devices/enums, sendTo cache, timers reverse index hooks, subscription dispatch map, Set-backed interval/timeout tracking). |
| src/lib/eventObj.ts | Optimize enum dedup via Sets and adapt to context.enums being a Set. |
| src-editor/package.json | Version bump (9.2.1 → 9.2.2). |
| src-editor/package-lock.json | Lockfile version bump corresponding to editor package version. |
| src-admin/package.json | Version bump (9.2.1 → 9.2.2). |
| src-admin/package-lock.json | Lockfile version bump corresponding to admin package version. |
| build/types.d.ts | Generated typings updated to match src/types.d.ts. |
| build/main.js | Generated build output reflecting src/main.ts changes. |
| build/lib/scheduler.js | Generated build output reflecting src/lib/scheduler.ts changes. |
| build/lib/sandbox.js | Generated build output reflecting src/lib/sandbox.ts changes. |
| build/lib/eventObj.js.map | Generated sourcemap updated for eventObj changes. |
| build/lib/eventObj.js | Generated build output reflecting src/lib/eventObj.ts changes. |
| admin/tab.html | Bundled admin entrypoint hash update. |
| admin/assets/ScriptEditor-Cryz6vV4.js | Bundled admin asset hash/content update. |
| admin/assets/AiChatPanel-Caoxu5lo.js | Bundled admin asset hash/content update. |
Files not reviewed (7)
- admin/remoteEntry.js: Language not supported
- build/lib/eventObj.js: Language not supported
- build/lib/sandbox.js: Language not supported
- build/lib/scheduler.js: Language not supported
- build/main.js: Language not supported
- src-admin/package-lock.json: Language not supported
- src-editor/package-lock.json: Language not supported
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
2190
to
2194
| for (const id in res) { | ||
| if (Object.prototype.hasOwnProperty.call(res, id)) { | ||
| this.stateIds.push(id); | ||
| this.stateIdSet.add(id); | ||
| } |
Contributor
Author
There was a problem hiding this comment.
after the loop we need sorted array
Comment on lines
+853
to
+854
| // Precompute once – avoids string template alloc on every setState call | ||
| this._adapterFrom = `system.adapter.${this.namespace}`; |
Comment on lines
2792
to
+2796
| : isAck, | ||
| scriptName: name, | ||
| }); | ||
| // Keep reverse-index in sync for O(1) cleanup in stopScript | ||
| if (!context.timersByScript.has(name)) { |
Comment on lines
+2829
to
+2837
| delete timers[id]; | ||
| } | ||
| } | ||
| // IO-7: timersByScript Reverse-Index aktualisieren wenn State keine Timer mehr hat | ||
| if (!timers[id]) { | ||
| const scriptName = name; // name = Script-Name aus sandBox()-Closure | ||
| const stateIds = context.timersByScript.get(scriptName); | ||
| if (stateIds) { | ||
| stateIds.delete(id); |
Comment on lines
2813
to
2819
| if (timers[id]) { | ||
| for (let i = timers[id].length - 1; i >= 0; i--) { | ||
| if (timerId === undefined || timers[id][i].id === timerId) { | ||
| clearTimeout(timers[id][i].t); | ||
| if (timerId !== undefined) { | ||
| timers[id].splice(i, 1); | ||
| } |
Comment on lines
+60
to
+66
| it('Map-Instanz wird nur EINMAL erzeugt – kein GC-Druck durch neue Objekte', () => { | ||
| const start = process.memoryUsage().heapUsed; | ||
| for (let i = 0; i < 100_000; i++) httpStatusText(500); | ||
| const end = process.memoryUsage().heapUsed; | ||
| // Heap-Delta sollte minimal sein (< 1 MB) | ||
| assert.ok(end - start < 1024 * 1024, `Zu viel Heap-Zuwachs: ${end - start} Bytes`); | ||
| }); |
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.