Skip to content

Commit

Permalink
getOne and memoizedWeak
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed May 4, 2019
1 parent 7c751f2 commit fd7533a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {str as crc32_str} from "crc-32";

import ProxyPolyfill from './proxy-polyfill';
import {getCollectionHandlers, shouldInstrument} from "./shouldInstrument";
import {weakMemoizeArray} from "./weakMemoize";
import {weakMemoizeArray, weakMemoizeWalk} from "./weakMemoize";
import {EDGE, pushObjTrie} from "./objectTrie";

const hasProxy = typeof Proxy !== 'undefined';
Expand Down Expand Up @@ -257,6 +257,16 @@ const get = (target, path) => {
return result;
};

const getOne = (target, key) => {
if (!target) return target;
if (key[0] === '!') {
if (key === objectKeysMarker) {
return Object.keys(target).map(crc32_str).reduce((acc, x) => acc ^ x, 0);
}
}
return target[key];
};

let differs = [];

export const drainDifference = () => {
Expand Down Expand Up @@ -286,10 +296,8 @@ const proxyCompare = (a, b, locations) => {
return ret;
};

const getterHelper = ['',''];

let differ = [];
const walk = (la, lb, node) => {
const memoizedWalk = weakMemoizeWalk((la, lb, node) => {
if (la === lb || deepDeproxify(la) === deepDeproxify(lb)) {
return true;
}
Expand All @@ -299,25 +307,24 @@ const walk = (la, lb, node) => {
const items = Object.keys(node);
for (let i = 0; i < items.length; ++i) {
const item = items[i];
getterHelper[1]=item;
if (!walk(
get(la, getterHelper),
get(lb, getterHelper),
if (!memoizedWalk(
getOne(la, item),
getOne(lb, item),
node[item],

This comment has been minimized.

Copy link
@theKashey

theKashey May 6, 2019

Owner

That would be a unique object every run. Plus EDGE nodes are all the same. We have to maintain "affected immutability" to be able use node here.

This comment has been minimized.

Copy link
@dai-shi

dai-shi May 6, 2019

Author Collaborator

If you mean "every run" = "every render", yes. The later commit e7f0ad0 should keep ref equality?

EDGE nodes are strings and not used for caching. I'm not sure that's what you mean by affected immutability.

As a side note, the stockticker benchmark is not an outlier, I think. It's a big challenge for auto-detection.

)) {
differ.unshift(item);
return false;
}
}
return true;
};
});
const proxyShallowEqual = (a, b, locations) => {
DISABLE_ALL_PROXIES = true;
differ = [];
differs = [];
const ret = (() => {
const root = locations;
return walk(a, b, root);
return memoizedWalk(a, b, root);
})();
DISABLE_ALL_PROXIES = false;
if(!ret) {
Expand Down
22 changes: 22 additions & 0 deletions src/weakMemoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,25 @@ export const weakMemoizeArray = fn => {
return ret
}
}

export const weakMemoizeWalk = fn => {
let cache = new WeakMap();
return (a, b, node) => {
if (typeof node !== 'object') {
return fn(a, b, node);
}
if (cache.has(node)) {
const old = cache.get(node);
if (old.a === a && old.b === b) {
return old.value;
}
}
const ret = fn(a, b, node);
cache.set(node, {
value: ret,
a,
b,
});
return ret;
}
};

0 comments on commit fd7533a

Please sign in to comment.