Skip to content

Fix resultEqualityCheck behavior in weakMapMemoize - #699

Merged
EskiMojo14 merged 8 commits into
reduxjs:masterfrom
aryaemami59:fix-weakMapMemoize-resultEqualityCheck
Feb 21, 2024
Merged

EskiMojo14 merged 8 commits into
reduxjs:masterfrom
aryaemami59:fix-weakMapMemoize-resultEqualityCheck

Conversation

@aryaemami59

Copy link
Copy Markdown
Member

Overview

Previously using weakMapMemoizewith resultEqualityCheck would cause the resultEqualityCheck to be called with empty objects as arguments due to how inputStabilityCheck works. Here's a summary of the changes:

This PR:

  • Fixes resultEqualityCheck in weakMapMemoize so that when input selectors are stable, resultEqualityCheck does not get called on the first run of the output selector.
  • Adds runtime unit tests to verify that:
    • resultEqualityCheck works correctly when set to shallowEqual.
    • resultEqualityCheck is not called on the first output selector call.
    • weakMapMemoize/lruMemoize with resultEqualityCheck set to referenceEqualityCheck works the same as weakMapMemoize/lruMemoize without resultEqualityCheck.
  • Adds benchmarks to measure the resultEqualityCheck performance impact.
  • Adds unit tests to test the dynamics between inputStabilityCheck and resultEqualityCheck.
  • Fixes a specific bug within weakMapMemoize where setting resultEqualityCheck to referenceEqualityCheck would prevent memoizedResultFunc.resultsCount() from incrementing upon result recalculation.
  • Fixes a performance issue in weakMapMemoize when comparing two memoized functions, one with resultEqualityCheck set to referenceEqualityCheck and the other without resultEqualityCheck. Previously, there was a noticeable discrepancy between the two, which has now been corrected to achieve the desired effect.

  - Previously `resultEqualityCheck` would be called with empty objects as arguments due to how `inputStabilityCheck` works. This update Fixes `resultEqualityCheck` in `weakMapMemoize` so that when input selectors are stable it does not get called on the first run of the output selector.
  - Added runtime unit tests to verify that:
    - `resultEqualityCheck` works correctly when set to `shallowEqual`.
    - `resultEqualityCheck` is not called on the first output selector call.
    - `weakMapMemoize` with `resultEqualityCheck` set to `referenceEqualityCheck` works the same as `weakMapMemoize` without `resultEqualityCheck`.
  - Added runtime unit tests to verify that:
    - `resultEqualityCheck` works correctly when set to `shallowEqual`.
    - `resultEqualityCheck` is not called on the first output selector call.
    - `lruMemoize` with `resultEqualityCheck` set to `referenceEqualityCheck` works the same as `lruMemoize` without `resultEqualityCheck`.
@netlify

netlify Bot commented Feb 11, 2024 •

Copy link
Copy Markdown

✅ Deploy Preview for reselect-docs canceled.

Name Link
🔨 Latest commit cc884cc
🔍 Latest deploy log https://app.netlify.com/sites/reselect-docs/deploys/65d620ac0efa7c0008b51fa0

@codesandbox-ci

codesandbox-ci Bot commented Feb 11, 2024 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@keegan-lillo

Copy link
Copy Markdown

Thanks for working on this! I believe this PR is related to this issue: #693

@EskiMojo14

EskiMojo14 commented Feb 14, 2024 •

Copy link
Copy Markdown
Contributor

related, but not a complete fix - if inputs aren't stable, your resultEqualityCheck will still be called with an empty object

nonetheless the improvements here are nice

@aryaemami59

Copy link
Copy Markdown
Member Author

@keegan-lillo This should mitigate the problem as long as your input selectors are stable. It will at least make the default configs a tiny bit less problematic. But @EskiMojo14 is correct, it doesn't fully address the issue.

@EskiMojo14

Copy link
Copy Markdown
Contributor

this looks good, but has conflicts - happy to merge once fixed

@EskiMojo14
EskiMojo14 merged commit 3786506 into reduxjs:master Feb 21, 2024
@EskiMojo14

Copy link
Copy Markdown
Contributor

thanks!

@aryaemami59
aryaemami59 deleted the fix-weakMapMemoize-resultEqualityCheck branch February 21, 2024 16:17
@braeden

braeden commented May 31, 2024

Copy link
Copy Markdown

Hey @EskiMojo14, any chance a release could be cut which contains this change?

@markerikson

Copy link
Copy Markdown
Contributor

Sorry for the delay on this. I should have time to get it out on Saturday.

@markerikson

Copy link
Copy Markdown
Contributor

Okay, out in https://github.com/reduxjs/reselect/releases/tag/v5.1.1 !

descope Bot referenced this pull request in descope/descope-js Jun 23, 2024
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [reselect](https://togithub.com/reduxjs/reselect) | dependencies |
patch | [`5.1.0` ->
`5.1.1`](https://renovatebot.com/diffs/npm/reselect/5.1.0/5.1.1) |

---

### Release Notes

<details>
<summary>reduxjs/reselect (reselect)</summary>

###
[`v5.1.1`](https://togithub.com/reduxjs/reselect/releases/tag/v5.1.1)

[Compare
Source](https://togithub.com/reduxjs/reselect/compare/v5.1.0...v5.1.1)

This **patch release** fixes behavior of `resultEqualityCheck` in
`weakMapMemoize`, fixes the case of `lruMemoize` being given a `maxSize`
less than 1, and tweaks the internal implementation of `lruMemoize`.
(We've also updated our general build tooling.)

##### Changelog

##### Bug fixes

Previously, providing the `resultEqualityCheck` option to
`weakMapMemoize` resulted in it being called with empty objects as part
of the initialization / dev check process. That could be an issue if
your comparison function expected different values. We've updated the
logic to avoid that, as well as improving a couple other perf aspects.

Previously, passing a `maxSize` < 1 to `lruMemoize` would result in it
creating a larger cache. That's now fixed.

`lruMemoize` now uses a symbol for its `NOT_FOUND` value instead of a
string.

##### What's Changed

- Ensure `lruMemoize` correctly memoizes when `maxSize` is set to a
number less than 1 by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/698](https://togithub.com/reduxjs/reselect/pull/698)
- Fix `resultEqualityCheck` behavior in `weakMapMemoize` by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/699](https://togithub.com/reduxjs/reselect/pull/699)
- Update TypeScript to 5.4 by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/708](https://togithub.com/reduxjs/reselect/pull/708)
- Upgrade to Yarn 4 by
[@&#8203;aryaemami59](https://togithub.com/aryaemami59) in
[https://github.com/reduxjs/reselect/pull/705](https://togithub.com/reduxjs/reselect/pull/705)
- Fix: use unique value for NOT_FOUND by
[@&#8203;romgrk](https://togithub.com/romgrk) in
[https://github.com/reduxjs/reselect/pull/709](https://togithub.com/reduxjs/reselect/pull/709)

**Full Changelog**:
reduxjs/reselect@v5.1.0...v5.1.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Renovate
Bot](https://togithub.com/renovatebot/renovate).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xNzAuMCIsInVwZGF0ZWRJblZlciI6IjM3LjE3MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->

Co-authored-by: descope[bot] <descope[bot]@users.noreply.github.com>
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.

5 participants