From: daiwei Date: Wed, 11 Dec 2024 14:40:14 +0000 (+0800) Subject: refactor: avoid tracking multiple times X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b3d9d070808ea568fb44c630eb5b45a0aef7572f;p=thirdparty%2Fvuejs%2Fcore.git refactor: avoid tracking multiple times --- diff --git a/packages/compiler-vapor/src/generators/operation.ts b/packages/compiler-vapor/src/generators/operation.ts index 2435340dba..b9402ffa0e 100644 --- a/packages/compiler-vapor/src/generators/operation.ts +++ b/packages/compiler-vapor/src/generators/operation.ts @@ -116,45 +116,49 @@ export function genEffect( const operationsExps = genOperations(operations, context) const { processingRenderEffect } = context - const { declareNames, earlyCheckExps } = processingRenderEffect! + const { declareNames, earlyCheckExps, preAccessExps } = + processingRenderEffect! if (declareNames.size) { allDeclareNames.add([...declareNames].join(', ')) } - const newlineCount = operationsExps.filter(frag => frag === NEWLINE).length - if (newlineCount > 1) { - // multiline check expression: if (_foo !== _ctx.foo || _bar !== _ctx.bar) { - const checkExpsStart: CodeFragment[] = - earlyCheckExps.length > 0 - ? [`if(`, ...earlyCheckExps.join(' || '), `) {`, INDENT_START] - : [] - const checkExpsEnd: CodeFragment[] = - earlyCheckExps.length > 0 ? [INDENT_END, NEWLINE, '}'] : [] - // assignment: _foo = _ctx.foo; _bar = _ctx.bar - const assignmentExps: CodeFragment[] = - earlyCheckExps.length > 0 - ? [NEWLINE, ...earlyCheckExps.map(c => c.replace('!==', '=')).join(';')] - : [] - push( - ...checkExpsStart, - ...operationsExps, - ...assignmentExps, - ...checkExpsEnd, - ) - } else { - // single line check expression: (_foo !== _ctx.foo || _bar !== _ctx.bar) && - const multiple = earlyCheckExps.length > 1 - const checkExps: CodeFragment[] = - earlyCheckExps.length > 0 - ? [ - multiple ? `(` : undefined, - ...earlyCheckExps.join(' || '), - multiple ? `)` : undefined, - ' && ', - ] - : [] - push(...checkExps, ...operationsExps.filter(frag => frag !== NEWLINE)) - } + const accessExps: CodeFragment[] = + preAccessExps.size > 0 ? [[...preAccessExps].join(';'), NEWLINE] : [] + // const newlineCount = operationsExps.filter(frag => frag === NEWLINE).length + // if (newlineCount > 1) { + // multiline check expression: if (_foo !== _ctx.foo || _bar !== _ctx.bar) { + const checkExpsStart: CodeFragment[] = + earlyCheckExps.length > 0 + ? [`if(`, ...earlyCheckExps.join(' || '), `) {`, INDENT_START] + : [] + const checkExpsEnd: CodeFragment[] = + earlyCheckExps.length > 0 ? [INDENT_END, NEWLINE, '}'] : [] + // assignment: _foo = _ctx.foo; _bar = _ctx.bar + const assignmentExps: CodeFragment[] = + earlyCheckExps.length > 0 + ? [NEWLINE, ...earlyCheckExps.map(c => c.replace('!==', '=')).join(';')] + : [] + push( + ...accessExps, + ...checkExpsStart, + ...operationsExps, + ...assignmentExps, + ...checkExpsEnd, + ) + // } else { + // single line check expression: (_foo !== _ctx.foo || _bar !== _ctx.bar) && + // const multiple = earlyCheckExps.length > 1 + // const checkExps: CodeFragment[] = + // earlyCheckExps.length > 0 + // ? [ + // multiple ? `(` : undefined, + // ...earlyCheckExps.join(' || '), + // multiple ? `)` : undefined, + // ' && ', + // ] + // : [] + // push(...checkExps, ...operationsExps.filter(frag => frag !== NEWLINE)) + // } return frag } diff --git a/packages/compiler-vapor/src/generators/prop.ts b/packages/compiler-vapor/src/generators/prop.ts index 35716537dd..f8c2e0e37c 100644 --- a/packages/compiler-vapor/src/generators/prop.ts +++ b/packages/compiler-vapor/src/generators/prop.ts @@ -295,32 +295,32 @@ function processValue( needRewrite: boolean = true, ): string[] | undefined { const { processingRenderEffect, allRenderEffectSeenNames } = context - const { declareNames, rewrittenNames, earlyCheckExps, operations } = + const { declareNames, earlyCheckExps, preAccessExps } = processingRenderEffect! - const isSingleLine = operations.length === 1 + // const isSingleLine = operations.length === 1 for (const frag of values) { if (!isArray(frag)) continue // [code, newlineIndex, loc, name] -> [(_name = code), newlineIndex, loc, name] const [newName, , , rawName] = frag if (rawName) { let name = rawName.replace(/[^\w]/g, '_') - if (rewrittenNames.has(name)) continue - rewrittenNames.add(name) + // if (rewrittenNames.has(name)) continue + // rewrittenNames.add(name) name = `_${name}` - if (declareNames.has(name)) continue - - if (allRenderEffectSeenNames[name] === undefined) - allRenderEffectSeenNames[name] = 0 - else name += ++allRenderEffectSeenNames[name] - - declareNames.add(name) - earlyCheckExps.push(`${name} !== ${newName}`) - - if (needRewrite && isSingleLine) { - // replace the original code fragment with the assignment expression - frag[0] = `(${name} = ${newName})` + if (!declareNames.has(name)) { + if (allRenderEffectSeenNames[name] === undefined) + allRenderEffectSeenNames[name] = 0 + else name += ++allRenderEffectSeenNames[name] + declareNames.add(name) + } + const preAccessName = `_${name}` + declareNames.add(`${preAccessName}`) + preAccessExps.add(`${preAccessName} = ${newName}`) + earlyCheckExps.push(`${name} !== ${preAccessName}`) + if (needRewrite) { + frag[0] = `${preAccessName}` } } } diff --git a/packages/compiler-vapor/src/ir/index.ts b/packages/compiler-vapor/src/ir/index.ts index 7dabd0dc2e..46365eecd3 100644 --- a/packages/compiler-vapor/src/ir/index.ts +++ b/packages/compiler-vapor/src/ir/index.ts @@ -265,6 +265,7 @@ export interface IREffect { declareNames: Set rewrittenNames: Set earlyCheckExps: string[] + preAccessExps: Set inVFor: boolean } diff --git a/packages/compiler-vapor/src/transform.ts b/packages/compiler-vapor/src/transform.ts index 0b4f71f8d7..a1609a9287 100644 --- a/packages/compiler-vapor/src/transform.ts +++ b/packages/compiler-vapor/src/transform.ts @@ -163,6 +163,7 @@ export class TransformContext { expressions, operations, earlyCheckExps: [], + preAccessExps: new Set(), declareNames: new Set(), rewrittenNames: new Set(), inVFor: this.inVFor > 0,