reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Explain memoization logic

Open gajus opened this issue 11 years ago • 3 comments

In the documentation it says:

Selectors are efficient. A selector is not recomputed unless one of its arguments change.

In the following example, I test memorisation by running testMemoizeSelector twice, which is generating a random number as an outcome. It works.

Then I add a new member to the data.screenings, which affects the outcome of one of the child selectors. However, it does not discard the testMemoizeSelector cache, which it should based on the description:

curiosity:2015 08 04 reselect gajus$ babel-node ./test
screenings.length 8
testMemoizeSelector #0 0.8094113706611097
testMemoizeSelector #1 0.8094113706611097
testMemoizeSelector #2 0.8094113706611097
import {
    createSelector
} from 'reselect';

import _ from 'lodash';

let data = {};

data.locations = [
    {
        id: 1,
        name: 'Foo',
        address: 'Foo st.',
    },
    {
        id: 2,
        name: 'Bar',
        address: 'Bar st.',
    }
];

data.screenings = [
    {
        locationId: 1,
        id: 1,
        timestamp: 1438423200000
    },
    {
        locationId: 1,
        id: 2,
        timestamp: 1438509600000
    },
    {
        locationId: 1,
        id: 3,
        timestamp: 1438527600000
    },
    {
        locationId: 1,
        id: 4,
        timestamp: 1438598600000
    },
    {
        locationId: 2,
        id: 1,
        timestamp: 1438423200000
    },
    {
        locationId: 2,
        id: 2,
        timestamp: 1438509600000
    },
    {
        locationId: 2,
        id: 3,
        timestamp: 1438527600000
    },
    {
        locationId: 2,
        id: 4,
        timestamp: 1438698600000
    }
];

let locationSelector = state => state.locations,
    screeningSelector = state => state.screenings,
    testMemoizeSelector;

testMemoizeSelector = createSelector(
    [
        locationSelector,
        screeningSelector
    ],
    (locations, screenings) => {
        console.log('screenings.length', screenings.length);

        return Math.random();
    }
);

console.log('testMemoizeSelector #0', testMemoizeSelector(data));

console.log('testMemoizeSelector #1', testMemoizeSelector(data));

data.screenings.push({
    locationId: 2,
    id: 4,
    timestamp: 1438698600001
});

console.log('testMemoizeSelector #2', testMemoizeSelector(data));

gajus avatar Aug 07 '15 12:08 gajus

Hey @gajus,

Thanks for creating this issue, we should probably add something to the docs about how this works.

In your example you are mutating an array in place. Redux, and by extension Reselect, do not expect you to mutate your state like this. See the Handling Actions section in the reducers docs.

This is how the memoization function:

function memoize(func, valueEquals) {
    let lastArgs = null;
    let lastResult = null;
    return (args) => {    // Note: the arguments are collected in an array before being passed into here
        if (lastArgs !== null && argsEquals(args, lastArgs, valueEquals)) {
            return lastResult;
        }
        lastArgs = args;
        lastResult = func(...args);
        return lastResult;
    }
}

As you can see it is super simple. If we make the assumption that the arguments are immutable we can simply store a reference and don't have to worry about hashing / doing a deep compare to determine if they have changed between calls. The valueEquals function passed to argsEquals can be swapped out using the createSelectorCreator function if a deep compare / something else is desired.

ellbee avatar Aug 07 '15 19:08 ellbee

Thank you. It makes sense in real life (flux app). This is only something I came across while testing the API.

On Aug 7, 2015, at 22:25, ellbee [email protected] wrote:

Hey @gajus,

Thanks for creating this issue, we should probably add something to the docs about how this works.

In your example you are mutating an array in place. Redux, and by extension Reselect, do not expect you to mutate your state like this. See the Handling Actions section in the reducers docs.

This is how the memoization function:

function memoize(func, valueEquals) { let lastArgs = null; let lastResult = null; return (args) => { // Note: the arguments are collected in an array before being passed into here if (lastArgs !== null && argsEquals(args, lastArgs, valueEquals)) { return lastResult; } lastArgs = args; lastResult = func(...args); return lastResult; } } As you can see it is super simple. If we make the assumption that the arguments are immutable we can simply store a reference and don't have to worry about hashing / doing a deep compare to determine if they have changed between calls. The valueEquals function passed to argsEquals can be swapped out using the createSelectorCreator function if a deep compare / something else is desired.

— Reply to this email directly or view it on GitHub.

gajus avatar Aug 07 '15 19:08 gajus

Yep, we should mention that we are are assuming the data is immutable. I'll add it to the list of things we need to document, and thanks again for taking the time to make the issue.

ellbee avatar Aug 07 '15 19:08 ellbee