perf: back calculator getters with $derived (#689)#703
Open
grzanka wants to merge 1 commit into
Open
Conversation
The public getters `rows`, `stpDisplayUnit`, and `validationSummary` in `calculator.svelte.ts` each ran a `compute*()` / `get*()` function on every property access, re-parsing every input row several times per frame and returning a fresh object on each read. Replace them with module-level `$derived` values (matching `entity-availability.svelte.ts`), so each computation is memoized and only reruns when a tracked dependency changes. `validationSummary` now derives from the memoized `rows` value instead of calling `computeRows()` a second time, so one parse pass backs both views. The public getter surface is unchanged; no call sites change. Add referential-stability unit tests asserting a getter read twice without an input change returns the same reference, and that the reference is invalidated after an input change.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves calculator render-time performance and correctness-by-identity by memoizing hot public getters in createCalculatorState() with Svelte 5 $derived, avoiding repeated per-row parsing/conversion on each property access.
Changes:
- Backed
rows,stpDisplayUnit, andvalidationSummarygetters with memoized$derived/$derived.byvalues (withvalidationSummaryderived from the memoizedrows). - Added unit tests asserting referential stability across repeated reads and invalidation after an input change.
- Logged the AI session (changelog entry + detailed session log) per repo process.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/lib/state/calculator.svelte.ts | Replaces recompute-on-access getters with $derived-memoized views; makes validationSummary depend on memoized rows to avoid duplicate row parsing. |
| src/tests/unit/calculator-state.test.ts | Adds focused tests for derived getter memoization (stable references) and invalidation on input updates. |
| docs/ai-logs/README.md | Adds the new session log entry to the index table. |
| docs/ai-logs/2026-06-04-issue-689-derived-getters.md | Adds the detailed AI session narrative and task record for Issue #689. |
| CHANGELOG-AI.md | Prepends a new bullet entry for this session with a link to the detailed log. |
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.
Closes #689.
What
The public getters
rows,stpDisplayUnit, andvalidationSummaryinsrc/lib/state/calculator.svelte.tseach called acompute*()/get*()function on every property access, rather than exposing memoized$derivedvalues:Templates read these several times per frame (table body, footer, validation banner, export wiring), so the per-row parse/convert/result-lookup work ran multiple times for a single render, and each read handed back a fresh object — a latent identity hazard for any
===/ capture-and-compare call site.Change
$derivedvalues, matching the existing pattern inentity-availability.svelte.ts:const rows = $derived(computeRows())const stpDisplayUnit = $derived(getStpDisplayUnit())const validationSummary = $derived.by(...)— now derives from the memoizedrowsvalue instead of re-runningcomputeRows(), so one parse pass backs both the row view and the validation counts.compute*/get*helpers are pure reads of reactive state (no mutation), so no untangling was needed.Acceptance criteria
rows,validationSummary,stpDisplayUnitare backed by$derived.calculator-state.test.tspasses unchanged.pnpm check(0 errors),pnpm lint(clean),calculator-state.test.ts59/59 pass.Notes
The only full-suite failures are in
guard-forbidden-files.test.ts, which fail because git commit signing is unavailable in the CI sandbox (the signing server returns HTTP 400 during the test'smakeRepo). They are unrelated to this change.https://claude.ai/code/session_01Tvdg51J44XeNxiwP9bnEjS
Generated by Claude Code