Просмотр исходного кода

refactor(runtime-vapor): optimize `_child` helper call by omitting redundant index 0 (#14200)

edison 4 месяцев назад
Родитель
Сommit
a812d23e1d

+ 6 - 6
packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

@@ -223,7 +223,7 @@ export function render(_ctx) {
   const n5 = _next(_child(n6), 1)
   const n7 = _nthChild(n6, 3, 3)
   const p0 = _next(n7, 4)
-  const n4 = _child(p0, 0)
+  const n4 = _child(p0)
   _setInsertionState(n6, n5)
   const n0 = _createComponentWithFallback(_component_Comp)
   _setInsertionState(n6, n7, true)
@@ -243,7 +243,7 @@ const t0 = _template("<div><div></div></div>", true)
 export function render(_ctx) {
   const _component_Comp = _resolveComponent("Comp")
   const n3 = t0()
-  const n1 = _child(n3, 0)
+  const n1 = _child(n3)
   _setInsertionState(n1, null, true)
   const n0 = _createSlot("default", null)
   _setInsertionState(n3, 1, true)
@@ -258,7 +258,7 @@ const t0 = _template("<div><span> </span> <br> </div>", true)
 
 export function render(_ctx) {
   const n3 = t0()
-  const n0 = _child(n3, 0)
+  const n0 = _child(n3)
   const n1 = _next(n0, 1)
   const n2 = _nthChild(n3, 3, 3)
   const x0 = _txt(n0)
@@ -404,9 +404,9 @@ const t0 = _template("<div><div><div><span></span></div></div></div>", true)
 
 export function render(_ctx, $props, $emit, $attrs, $slots) {
   const n1 = t0()
-  const p1 = _child(n1, 0)
-  const p3 = _child(p1, 0)
-  const n0 = _child(p3, 0)
+  const p1 = _child(n1)
+  const p3 = _child(p1)
+  const n0 = _child(p3)
   _renderEffect(() => _setProp(n0, "id", _ctx.foo))
   return n1
 }"

+ 2 - 2
packages/compiler-vapor/__tests__/transforms/__snapshots__/expression.spec.ts.snap

@@ -47,7 +47,7 @@ const t0 = _template("<div> </div>", true)
 
 export function render(_ctx) {
   const n1 = t0()
-  const n0 = _child(n1, 0)
+  const n0 = _child(n1)
   const x1 = _txt(n1)
   _renderEffect(() => {
     const _foo = _ctx.foo
@@ -86,7 +86,7 @@ const t0 = _template("<div> </div>", true)
 
 export function render(_ctx) {
   const n1 = t0()
-  const n0 = _child(n1, 0)
+  const n0 = _child(n1)
   const x1 = _txt(n1)
   _renderEffect(() => {
     const _String = String

+ 4 - 4
packages/compiler-vapor/__tests__/transforms/__snapshots__/transformChildren.spec.ts.snap

@@ -23,7 +23,7 @@ const t0 = _template("<div><p> </p> <p> </p></div>", true)
 
 export function render(_ctx) {
   const n3 = t0()
-  const n0 = _child(n3, 0)
+  const n0 = _child(n3)
   const n1 = _next(n0, 1)
   const n2 = _next(n1, 2)
   const x0 = _txt(n0)
@@ -57,11 +57,11 @@ const t0 = _template("<div><div>x</div><div><span> </span></div><div><span> </sp
 export function render(_ctx) {
   const n3 = t0()
   const p0 = _next(_child(n3), 1)
-  const n0 = _child(p0, 0)
+  const n0 = _child(p0)
   const p1 = _next(p0, 2)
-  const n1 = _child(p1, 0)
+  const n1 = _child(p1)
   const p2 = _next(p1, 3)
-  const n2 = _child(p2, 0)
+  const n2 = _child(p2)
   const x0 = _txt(n0)
   const x1 = _txt(n1)
   const x2 = _txt(n2)

+ 1 - 1
packages/compiler-vapor/__tests__/transforms/__snapshots__/transformText.spec.ts.snap

@@ -17,7 +17,7 @@ const t0 = _template("<div>2 foo1 1 1 1</div>", true)
 
 export function render(_ctx) {
   const n1 = t0()
-  const n0 = _child(n1, 0)
+  const n0 = _child(n1)
   return n1
 }"
 `;

+ 2 - 2
packages/compiler-vapor/__tests__/transforms/__snapshots__/vFor.spec.ts.snap

@@ -243,7 +243,7 @@ export function render(_ctx) {
   const _component_Comp = _resolveComponent("Comp")
   const n0 = _createFor(() => (_ctx.list), (_for_item0) => {
     const n3 = _createComponentWithFallback(_component_Comp)
-    const n2 = _child(n3, 0)
+    const n2 = _child(n3)
     _renderEffect(() => _setText(n2, _toDisplayString(_for_item0.value)))
     return [n2, n3]
   }, undefined, 2)
@@ -259,7 +259,7 @@ export function render(_ctx) {
   const _component_Comp = _resolveComponent("Comp")
   const n0 = _createFor(() => (_ctx.list), (_for_item0) => {
     const n3 = _createComponentWithFallback(_component_Comp)
-    const n2 = _child(n3, 0)
+    const n2 = _child(n3)
     _renderEffect(() => _setText(n2, _toDisplayString(_for_item0.value)))
     return [n2, n3]
   }, undefined, 2)

+ 2 - 2
packages/compiler-vapor/__tests__/transforms/__snapshots__/vOnce.spec.ts.snap

@@ -17,7 +17,7 @@ const t0 = _template("<div> <span></span></div>", true)
 
 export function render(_ctx, $props, $emit, $attrs, $slots) {
   const n2 = t0()
-  const n0 = _child(n2, 0)
+  const n0 = _child(n2)
   const n1 = _next(n0, 1)
   _setText(n0, _toDisplayString(_ctx.msg) + " ")
   _setClass(n1, _ctx.clz)
@@ -54,7 +54,7 @@ const t0 = _template("<div><div></div></div>", true)
 
 export function render(_ctx) {
   const n1 = t0()
-  const n0 = _child(n1, 0)
+  const n0 = _child(n1)
   _setProp(n0, "id", _ctx.foo)
   return n1
 }"

+ 7 - 1
packages/compiler-vapor/src/generators/template.ts

@@ -122,7 +122,13 @@ export function genChildren(
       }
     } else {
       if (elementIndex === 0) {
-        pushBlock(...genCall(helper('child'), from, String(logicalIndex)))
+        pushBlock(
+          ...genCall(
+            helper('child'),
+            from,
+            logicalIndex !== 0 ? String(logicalIndex) : undefined,
+          ),
+        )
       } else {
         // check if there's a node that we can reuse from
         let init = genCall(helper('child'), from)