Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does selector memoization work? #81

Closed
RunDevelopment opened this issue May 19, 2022 · 4 comments
Closed

How does selector memoization work? #81

RunDevelopment opened this issue May 19, 2022 · 4 comments

Comments

@RunDevelopment
Copy link

The RFC proposes that both the context and the selector are memoized.

However, it seems like this library works differently. E.g.:

  const count1 = useContextSelector(context, v => v[0].count1);

From my understanding of the RFC, this should defeat the memorization and make useContextSelector ineffective as a performance optimization.

Obviously, the above snippet works for this library, so could you please explain how? Is the selector memoized, and if so, how?

To cite a more complex example, let's look at this snippet:

function Foo({ id }) {
  const value: number = useContextSelector(Context, c => c.values[id])
  return <>{id} = {value}</>
}

How will this behave if both Context and id may change? (If props are too constant, then id may also be state.)

@dai-shi
Copy link
Owner

dai-shi commented May 19, 2022

As far as I understand, the RFC says if the selector is memoized like

  const count1 = useContextSelector(context, useCallback(v => v[0].count1, []));

It skips re-evaluation. But, if not

  const count1 = useContextSelector(context, v => v[0].count1);

It will re-evaluate.

So, it's just about optimization of re-evaluation of the selector function.

This library doesn't include such optimization, assuming the selector function is fairly lightweight.

How will this behave if both Context and id may change?

It will use the latest values, so it behaves correctly.

Is it clear? I'm afraid I don't get the point of your question.

@RunDevelopment
Copy link
Author

Thank you for your explanation.

In that case, I think there is a bug in the useContextSelector implementation of this library. This dependency array should also include selector.

Right now, the selector captured by the reducer function can become stale. This happens with the following steps:

  1. Let the context be the number 1, and the selector be () => 1.
  2. Change the selector to be c => c (yield the whole context).
    This will cause the selector in useReducer to be become stale because the deps array is still [value, selected] = [1, 1]
  3. Change the context to 2.
    The stale selector will still return 1.

I think those steps will produce incorrect results.

@dai-shi
Copy link
Owner

dai-shi commented May 19, 2022

Sorry for code readability, but that's not a dependency array. It's an initial value for useReducer. selector will never be stale.

@RunDevelopment
Copy link
Author

I see. Thank you for the clarification.

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

No branches or pull requests

2 participants