Repository navigation
Conversation
|
I've been thinking about this one again lately, as it seems particularly wasteful we call the same selectors repeatedly during the same tick on the same state. It's less impactful if the selector is memoized, but not all selectors are memoized. Having a more broad-yet-simple cache could also avoid the need for My main concern would be that for a very simple selector, there's probably more overhead merely in the cache lookup than what actually occurs in the selector itself. Does this seem like something worth continuing to explore? Or should we close it? |
|
I think we should close this. Enabling caching by default can also result in unexpected behavior. It is probably better to it work uncached by default and only add caching when it results in a performance on the actual bottlenecks of the application. |
Do you have an example in mind of something which could result in unexpected behavior? I think this could only occur if state is mutated directly, which is definitely not advisable nor intended to be supported, and likely already would have unexpected behavior anyways. The main idea here (perhaps not as currently implemented) is that when state changes, we have many many components wrapped with The main hold-up as I see it is in accounting for arguments variations, and whether that can be considered performant-enough to make the optimization worthwhile. Alternatively, we could limit the application of caching to only those selectors which are called without any arguments except for the state object. |
|
I should have linked it, but I was mainly thinking about the issues mentioned here: https://redux.js.org/recipes/computingderiveddata#sharing-selectors-across-multiple-components. I don't know if those specific issues are relevant here, but I know that with caching there is always an extra layer of complexity which you need to think about when implementing. A different issue I encountered was where the memoized function would be called with different object references, which wouldn't be deduced, which resulted in memory leaks. Again, I'm not sure if that is relevant here. I just want to note that a caching layer always introduces another layer of complexity people need to think about. |
|
If it can't be done without imposing some awareness on consuming developers, then I agree it shouldn't be introduced. But I also don't think it'll require any awareness.
|
|
I don't feel strongly about it either way, just mentioning my concerns. |
|
Is this still under consideration? If so, are there any testing instructions, or examples others can try to decide which way forward (abandon, cache all, cache some) would make sense? |
|
I think there's still value in this, though it's a tricky balance between the overhead of the cache lookup vs. the cost of the raw underlying selector. In any case, I do not expect to revisit it in the immediate future. We can reopen it in the future if it becomes a pressing need. |
This pull request seeks to explore an option for aggressive selector caching on every selector when called with the same state. It does so by tracking an
EquivalentKeyMapper state object byWeakMap(meaning unreferenced states are garbage collected), where each entry is a combination of selector name and arguments set.This particularly optimizes for repeated calls to the same selector with the same state value, without requiring explicit memoization by
createSelector. The latter may still be useful for more granular caching across distinct state values.