Skip to content

fix: better memoizations of cache values#48

Draft
schummar wants to merge 2 commits intomasterfrom
feature/feature-test-pr
Draft

fix: better memoizations of cache values#48
schummar wants to merge 2 commits intomasterfrom
feature/feature-test-pr

Conversation

@schummar
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings February 12, 2026 15:46
@github-actions
Copy link
Copy Markdown
Contributor

Test Results

  1 files  ±0   31 suites  ±0   3s ⏱️ ±0s
411 tests ±0  410 ✅ ±0  1 💤 ±0  0 ❌ ±0 
412 runs  ±0  411 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 3ef8ed1. ± Comparison against base commit 1707e89.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Test Results

  1 files  ±0   31 suites  ±0   3s ⏱️ ±0s
411 tests ±0  410 ✅ ±0  1 💤 ±0  0 ❌ ±0 
412 runs  ±0  411 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 3ef8ed1. ± Comparison against base commit 1707e89.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 12, 2026

Test Results

  1 files  ±0   31 suites  ±0   3s ⏱️ ±0s
411 tests ±0  410 ✅ ±0  1 💤 ±0  0 ❌ ±0 
412 runs  ±0  411 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 74a7894. ± Comparison against base commit 1707e89.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown

Copilot AI left a comment

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 moves “stable identity” memoization for cache value/error from the React hook layer (useCache) down into the core Cache state updates, aiming to reduce unnecessary downstream updates when newly produced values are deeply equal to previous ones.

Changes:

  • Removed useMemoEquals-based memoization of value/error inside useCache.
  • Added deepEqual-based reference reuse inside Cache.watchPromise() when updating this.state, so equal value/error keep prior object identity.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/react/useCache.ts Removes hook-level memoization of returned value/error.
src/core/cache.ts Adds core-level memoization to reuse previous value/error references when deeply equal.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +225 to +230
// Memoize value and error to keep their identity stable if they're equal
if (
newState.status === 'value' &&
state.status === 'value' &&
deepEqual(state.value, newState.value)
) {
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deepEqual is now executed inside the cache update path (including the PromiseWithState fast-path). For large cached values this can add noticeable CPU overhead even when there are no consumers (no one subscribed to this.state). Consider guarding the deep-equality memoization behind a check like whether this.state has subscribers/needs identity stability, or centralizing the memoization so the equality check is only performed when it will actually prevent downstream work (e.g., listener notifications / React rerenders).

Copilot uses AI. Check for mistakes.
Comment on lines +262 to +266
this.state.set((state) => {
const newValue =
state.status === 'value' && deepEqual(state.value, value) ? state.value : value;
return {
status: 'value',
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value/error identity memoization logic is duplicated across the resolved-PromiseWithState branch and the normal resolve/error branches. Consider extracting a small helper (e.g., “reusePreviousIfEqual”) to avoid future drift if this logic needs to change.

Copilot uses AI. Check for mistakes.
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