From 55ac43c1db7978d1818a9edf0d32d0e01fcd8f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 9 Jun 2022 14:55:57 +0200 Subject: [PATCH] [IMP] app: add fast path for when component has no prop --- src/compiler/code_generator.ts | 6 +- src/runtime/app.ts | 9 +- tests/__snapshots__/reactivity.test.ts.snap | 22 +- .../compiler/__snapshots__/misc.test.ts.snap | 6 +- .../__snapshots__/basics.test.ts.snap | 78 +++---- .../__snapshots__/concurrency.test.ts.snap | 166 +++++++-------- .../__snapshots__/error_handling.test.ts.snap | 90 ++++----- .../__snapshots__/event_handling.test.ts.snap | 4 +- .../higher_order_component.test.ts.snap | 16 +- .../__snapshots__/hooks.test.ts.snap | 14 +- .../__snapshots__/lifecycle.test.ts.snap | 62 +++--- .../__snapshots__/props.test.ts.snap | 22 +- .../props_validation.test.ts.snap | 142 ++++++------- .../__snapshots__/reactivity.test.ts.snap | 6 +- .../__snapshots__/refs.test.ts.snap | 4 +- .../__snapshots__/rendering.test.ts.snap | 24 +-- .../__snapshots__/slots.test.ts.snap | 190 +++++++++--------- .../__snapshots__/style_class.test.ts.snap | 36 ++-- .../__snapshots__/t_call.test.ts.snap | 12 +- .../__snapshots__/t_component.test.ts.snap | 12 +- .../__snapshots__/t_foreach.test.ts.snap | 20 +- .../__snapshots__/t_key.test.ts.snap | 16 +- .../__snapshots__/t_on.test.ts.snap | 20 +- .../__snapshots__/t_props.test.ts.snap | 10 +- .../__snapshots__/t_set.test.ts.snap | 18 +- tests/misc/__snapshots__/portal.test.ts.snap | 84 ++++---- 26 files changed, 548 insertions(+), 541 deletions(-) diff --git a/src/compiler/code_generator.ts b/src/compiler/code_generator.ts index f3217687..db7918da 100644 --- a/src/compiler/code_generator.ts +++ b/src/compiler/code_generator.ts @@ -1181,7 +1181,9 @@ export class CodeGenerator { id, expr: `app.createComponent(${ ast.isDynamic ? null : expr - }, ${!ast.isDynamic}, ${!!ast.slots}, ${!!ast.dynamicProps})`, + }, ${!ast.isDynamic}, ${!!ast.slots}, ${!!ast.dynamicProps}, ${ + !ast.props && !ast.dynamicProps + })`, }); let blockExpr = `${id}(${propString}, ${keyArg}, node, ctx, ${ast.isDynamic ? expr : null})`; @@ -1278,7 +1280,7 @@ export class CodeGenerator { let id = generateId("comp"); this.staticDefs.push({ id, - expr: `app.createComponent(null, false, true, false)`, + expr: `app.createComponent(null, false, true, false, false)`, }); const target = compileExpr(ast.target); diff --git a/src/runtime/app.ts b/src/runtime/app.ts index 311d2e74..c2974abf 100644 --- a/src/runtime/app.ts +++ b/src/runtime/app.ts @@ -118,7 +118,8 @@ export class App< name: string | null, isStatic: boolean, hasSlotsProp: boolean, - hasDynamicPropList: boolean + hasDynamicPropList: boolean, + hasNoProp: boolean ) { const isDynamic = !isStatic; function _arePropsDifferent(props1: Props, props2: Props): boolean { @@ -129,7 +130,11 @@ export class App< } return hasDynamicPropList && Object.keys(props1).length !== Object.keys(props2).length; } - const arePropsDifferent = hasSlotsProp ? () => true : _arePropsDifferent; + const arePropsDifferent = hasSlotsProp + ? (_1: any, _2: any) => true + : hasNoProp + ? (_1: any, _2: any) => false + : _arePropsDifferent; const updateAndRender = ComponentNode.prototype.updateAndRender; const initiateRender = ComponentNode.prototype.initiateRender; diff --git a/tests/__snapshots__/reactivity.test.ts.snap b/tests/__snapshots__/reactivity.test.ts.snap index d9ca0e49..4a1e4872 100644 --- a/tests/__snapshots__/reactivity.test.ts.snap +++ b/tests/__snapshots__/reactivity.test.ts.snap @@ -50,7 +50,7 @@ exports[`Reactivity: useState destroyed component before being mounted is inacti "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -82,7 +82,7 @@ exports[`Reactivity: useState destroyed component is inactive 1`] = ` "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -129,7 +129,7 @@ exports[`Reactivity: useState parent and children subscribed to same context 1`] "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -222,8 +222,8 @@ exports[`Reactivity: useState two components are updated in parallel 1`] = ` "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); - const comp2 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); + const comp2 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -253,8 +253,8 @@ exports[`Reactivity: useState two components can subscribe to same context 1`] = "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); - const comp2 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); + const comp2 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -284,8 +284,8 @@ exports[`Reactivity: useState two independent components on different levels are "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); - const comp2 = app.createComponent(\`Parent\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); + const comp2 = app.createComponent(\`Parent\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -315,7 +315,7 @@ exports[`Reactivity: useState two independent components on different levels are "function anonymous(app, bdom, helpers ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; - const comp1 = app.createComponent(\`Child\`, true, false, false); + const comp1 = app.createComponent(\`Child\`, true, false, false, true); let block1 = createBlock(\`
\`); @@ -345,7 +345,7 @@ exports[`Reactivity: useState useless atoms should be deleted 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; let { prepareList, withKey } = helpers; - const comp1 = app.createComponent(\`Quantity\`, true, false, false); + const comp1 = app.createComponent(\`Quantity\`, true, false, false, false); let block1 = createBlock(\`
Total: Count:
\`); diff --git a/tests/compiler/__snapshots__/misc.test.ts.snap b/tests/compiler/__snapshots__/misc.test.ts.snap index d20f68a5..0d85de09 100644 --- a/tests/compiler/__snapshots__/misc.test.ts.snap +++ b/tests/compiler/__snapshots__/misc.test.ts.snap @@ -5,7 +5,7 @@ exports[`misc complex template 1`] = ` ) { let { text, createBlock, list, multi, html, toggler, comment } = bdom; let { prepareList, withKey } = helpers; - const comp1 = app.createComponent(\`SlotButton\`, true, false, false); + const comp1 = app.createComponent(\`SlotButton\`, true, false, false, false); let block1 = createBlock(\`
\`); let block2 = createBlock(\`\`); @@ -184,8 +184,8 @@ exports[`misc other complex template 1`] = ` let { text, createBlock, list, multi, html, toggler, comment } = bdom; let { prepareList, withKey } = helpers; const callTemplate_1 = app.getTemplate(\`LOAD_INFOS_TEMPLATE\`); - const comp1 = app.createComponent(\`BundlesList\`, true, false, false); - const comp2 = app.createComponent(\`BundlesList\`, true, false, false); + const comp1 = app.createComponent(\`BundlesList\`, true, false, false, false); + const comp2 = app.createComponent(\`BundlesList\`, true, false, false, false); let block1 = createBlock(\`