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

Cache the function wrapper, no need to recreate each render. #903

Merged
merged 1 commit into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/core/vdom.ts
@@ -1,5 +1,5 @@
import global from '../shim/global';
import has from '../core/has';
import has from './has';
import WeakMap from '../shim/WeakMap';
import Set from '../shim/Set';
import Map from '../shim/Map';
Expand Down Expand Up @@ -184,6 +184,7 @@ export interface WidgetMeta {
customDiffProperties?: Set<string>;
customDiffMap?: Map<string, Map<string, (current: any, next: any) => any>>;
propertiesCalled: boolean;
wrappedFunctionMap: Map<string, Function>;
}

export interface WidgetData {
Expand Down Expand Up @@ -1171,26 +1172,31 @@ export const defer = factory(({ id }) => {
};
});

function wrapFunctionProperties(id: string, properties: any) {
function wrapFunctionProperties(widgetMeta: WidgetMeta, id: string, properties: any) {
const props: any = {};
const propertyNames = Object.keys(properties);
for (let i = 0; i < propertyNames.length; i++) {
const propertyName = propertyNames[i];
if (typeof properties[propertyName] === 'function') {
props[propertyName] = function WrappedProperty(...args: any[]) {
const widgetMeta = widgetMetaMap.get(id);
if (widgetMeta) {
return widgetMeta.originalProperties[propertyName](...args);
}
return properties[propertyName](...args);
};
props[propertyName].unwrap = () => {
const widgetMeta = widgetMetaMap.get(id);
if (widgetMeta) {
return widgetMeta.originalProperties[propertyName];
}
return properties[propertyName];
};
let cachedWrapper = widgetMeta.wrappedFunctionMap.get(propertyName);
if (!cachedWrapper) {
cachedWrapper = function WrappedProperty(...args: any[]) {
const widgetMeta = widgetMetaMap.get(id);
if (widgetMeta) {
return widgetMeta.originalProperties[propertyName](...args);
}
return properties[propertyName](...args);
};
(cachedWrapper as any).unwrap = () => {
const widgetMeta = widgetMetaMap.get(id);
if (widgetMeta) {
return widgetMeta.originalProperties[propertyName];
}
return properties[propertyName];
};
widgetMeta.wrappedFunctionMap.set(propertyName, cachedWrapper);
}
props[propertyName] = cachedWrapper;
} else {
props[propertyName] = properties[propertyName];
}
Expand Down Expand Up @@ -2404,18 +2410,20 @@ export function renderer(renderer: () => RenderResult): Renderer {
mountNode: _mountOptions.domNode,
dirty: false,
invalidator: invalidate,
properties: wrapFunctionProperties(id, next.node.properties),
originalProperties: { ...next.node.properties },
children: next.node.children,
deferRefs: 0,
rendering: true,
middleware: {},
middlewareIds: [],
registry: _mountOptions.registry,
propertiesCalled: false
propertiesCalled: false,
wrappedFunctionMap: new Map(),
properties: {}
};

widgetMetaMap.set(next.id, widgetMeta);
(widgetMeta.properties = wrapFunctionProperties(widgetMeta, id, next.node.properties)),
widgetMetaMap.set(next.id, widgetMeta);
if ((Constructor as any).middlewares && Object.keys((Constructor as any).middlewares).length) {
const { middlewares, ids } = resolveMiddleware((Constructor as any).middlewares, id);
widgetMeta.middleware = middlewares;
Expand Down Expand Up @@ -2508,7 +2516,7 @@ export function renderer(renderer: () => RenderResult): Renderer {
const widgetMeta = widgetMetaMap.get(id);
if (widgetMeta) {
widgetMeta.originalProperties = { ...next.properties };
widgetMeta.properties = wrapFunctionProperties(id, widgetMeta.originalProperties);
widgetMeta.properties = wrapFunctionProperties(widgetMeta, id, widgetMeta.originalProperties);
widgetMeta.children = next.node.children;
widgetMeta.rendering = true;
const customProperties = runDiffs(widgetMeta, current.properties, widgetMeta.originalProperties);
Expand Down
16 changes: 11 additions & 5 deletions tests/core/unit/vdom.tsx
Expand Up @@ -3953,13 +3953,19 @@ jsdomDescribe('vdom', () => {
const renderCount = icache.getOrSet('r', 1);
icache.set('r', renderCount + 1);
const PropertyWidget = A.unwrap();
const previousProperty = icache.getOrSet('previous', () => A);
let different = true;
if (previousProperty === A) {
different = false;
}
return (
<div>
<FunctionChild
onChange={() => {
icache.set('n', result * multiplier);
}}
/>
<div>{`different: ${different}`}</div>
<div>{`result: ${result}`}</div>
<div>{`Parent Rendered: ${renderCount}`}</div>
<PropertyWidget>Class</PropertyWidget>
Expand Down Expand Up @@ -4002,31 +4008,31 @@ jsdomDescribe('vdom', () => {

assert.strictEqual(
root.outerHTML,
'<root><div><button>Multiplier++</button><div>Multiplier: 2</div><div><div><button></button><div>Child Rendered: 1</div></div><div>result: 1</div><div>Parent Rendered: 1</div><div>Class0</div></div></div></root>'
'<root><div><button>Multiplier++</button><div>Multiplier: 2</div><div><div><button></button><div>Child Rendered: 1</div></div><div>different: false</div><div>result: 1</div><div>Parent Rendered: 1</div><div>Class0</div></div></div></root>'
);
(root.children[0].children[0] as HTMLButtonElement).click();
resolvers.resolve();
assert.strictEqual(
root.outerHTML,
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>result: 1</div><div>Parent Rendered: 2</div><div>Class1</div></div></div></root>'
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>different: false</div><div>result: 1</div><div>Parent Rendered: 2</div><div>Class1</div></div></div></root>'
);
(root.children[0].children[2].children[0].children[0] as HTMLButtonElement).click();
resolvers.resolve();
assert.strictEqual(
root.outerHTML,
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>result: 3</div><div>Parent Rendered: 3</div><div>Class2</div></div></div></root>'
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>different: false</div><div>result: 3</div><div>Parent Rendered: 3</div><div>Class2</div></div></div></root>'
);
(root.children[0].children[2].children[0].children[0] as HTMLButtonElement).click();
resolvers.resolve();
assert.strictEqual(
root.outerHTML,
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>result: 9</div><div>Parent Rendered: 4</div><div>Class3</div></div></div></root>'
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>different: false</div><div>result: 9</div><div>Parent Rendered: 4</div><div>Class3</div></div></div></root>'
);
(root.children[0].children[2].children[0].children[0] as HTMLButtonElement).click();
resolvers.resolve();
assert.strictEqual(
root.outerHTML,
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>result: 27</div><div>Parent Rendered: 5</div><div>Class4</div></div></div></root>'
'<root><div><button>Multiplier++</button><div>Multiplier: 3</div><div><div><button></button><div>Child Rendered: 1</div></div><div>different: false</div><div>result: 27</div><div>Parent Rendered: 5</div><div>Class4</div></div></div></root>'
);
});

Expand Down