From 5875c7c4906873c31b2feb66bb3ab6a19af6f5d7 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 5 Dec 2017 11:17:36 -0500 Subject: [PATCH] fix: revert shared static tree cache to avoid memory leak revert f0a66c5 fix #7184 --- src/compiler/codegen/index.js | 10 ++++------ .../instance/render-helpers/render-static.js | 20 +++++++------------ test/unit/modules/compiler/codegen.spec.js | 4 ++-- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/compiler/codegen/index.js b/src/compiler/codegen/index.js index 7b8a1826d7..e031e15b05 100644 --- a/src/compiler/codegen/index.js +++ b/src/compiler/codegen/index.js @@ -86,15 +86,13 @@ export function genElement (el: ASTElement, state: CodegenState): string { } // hoist static sub-trees out -function genStatic (el: ASTElement, state: CodegenState, once: ?boolean): string { +function genStatic (el: ASTElement, state: CodegenState): string { el.staticProcessed = true state.staticRenderFns.push(`with(this){return ${genElement(el, state)}}`) return `_m(${ state.staticRenderFns.length - 1 - },${ - el.staticInFor ? 'true' : 'false' - },${ - once ? 'true' : 'false' + }${ + el.staticInFor ? ',true' : '' })` } @@ -121,7 +119,7 @@ function genOnce (el: ASTElement, state: CodegenState): string { } return `_o(${genElement(el, state)},${state.onceId++},${key})` } else { - return genStatic(el, state, true) + return genStatic(el, state) } } diff --git a/src/core/instance/render-helpers/render-static.js b/src/core/instance/render-helpers/render-static.js index d80311e560..a26abbb6ee 100644 --- a/src/core/instance/render-helpers/render-static.js +++ b/src/core/instance/render-helpers/render-static.js @@ -7,19 +7,9 @@ import { cloneVNode, cloneVNodes } from 'core/vdom/vnode' */ export function renderStatic ( index: number, - isInFor: boolean, - isOnce: boolean + isInFor: boolean ): VNode | Array { - // render fns generated by compiler < 2.5.4 does not provide v-once - // information to runtime so be conservative - const isOldVersion = arguments.length < 3 - // if a static tree is generated by v-once, it is cached on the instance; - // otherwise it is purely static and can be cached on the shared options - // across all instances. - const renderFns = this.$options.staticRenderFns - const cached = isOldVersion || isOnce - ? (this._staticTrees || (this._staticTrees = [])) - : (renderFns.cached || (renderFns.cached = [])) + const cached = this._staticTrees || (this._staticTrees = []) let tree = cached[index] // if has already-rendered static tree and not inside v-for, // we can reuse the same tree by doing a shallow clone. @@ -29,7 +19,11 @@ export function renderStatic ( : cloneVNode(tree) } // otherwise, render a fresh tree. - tree = cached[index] = renderFns[index].call(this._renderProxy, null, this) + tree = cached[index] = this.$options.staticRenderFns[index].call( + this._renderProxy, + null, + this // for render fns generated for functional component templates + ) markStatic(tree, `__static__${index}`, false) return tree } diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js index 5e042668fd..d8b6c7e683 100644 --- a/test/unit/modules/compiler/codegen.spec.js +++ b/test/unit/modules/compiler/codegen.spec.js @@ -478,7 +478,7 @@ describe('codegen', () => { // have "inline-template'" assertCodegen( '

hello world

', - `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _m(0,false,false)}},staticRenderFns:[function(){with(this){return _c('p',[_c('span',[_v("hello world")])])}}]}})}` + `with(this){return _c('my-component',{inlineTemplate:{render:function(){with(this){return _m(0)}},staticRenderFns:[function(){with(this){return _c('p',[_c('span',[_v("hello world")])])}}]}})}` ) // "have inline-template attrs, but not having exactly one child element assertCodegen( @@ -498,7 +498,7 @@ describe('codegen', () => { it('generate static trees inside v-for', () => { assertCodegen( `

`, - `with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true,false)])}))}`, + `with(this){return _c('div',_l((10),function(i){return _c('div',[_m(0,true)])}))}`, [`with(this){return _c('p',[_c('span')])}`] ) })