Vue3?源碼解讀靜態(tài)提升詳解
什么是靜態(tài)提升
靜態(tài)提升是Vue3編譯優(yōu)化中的其中一個優(yōu)化點。所謂的靜態(tài)提升,就是指在編譯器編譯的過程中,將一些靜態(tài)的節(jié)點或屬性提升到渲染函數(shù)之外。下面,我們通過一個例子來深入理解什么是靜態(tài)提升。
假設我們有如下模板:
<div> <p>static text</p> <p>{{ title }}</p> </div>
在沒有靜態(tài)提升的情況下,它對應的渲染函數(shù)是:
function render() { return (openClock(), createClock('div', null, [ createVNode('p', null, 'static text'), createVNode('p', null, ctx.title, 1 /* TEXT */) ])) }
從上面的代碼中可以看到,在這段虛擬DOM的描述中存在兩個 p 標簽,一個是純靜態(tài)的,而另一個擁有動態(tài)文本。當響應式數(shù)據(jù) title 的值發(fā)生變化時,整個渲染函數(shù)會重新執(zhí)行,并產生新的虛擬DOM樹。在這個過程中存在性能開銷的問題。原因是純靜態(tài)的虛擬節(jié)點在更新時也會被重新創(chuàng)建一次,其實這是沒有必要的。因此我們需要使用靜態(tài)提升來解決這個問題。
所謂的 “靜態(tài)提升”,就是將一些靜態(tài)的節(jié)點或屬性提升到渲染函數(shù)之外。如下面的代碼所示:
// 把靜態(tài)節(jié)點提升到渲染函數(shù)之外 const hoist1 = createVNode('p', null, 'text') function render() { return (openBlock(), createBlock('div', null, [ hoist1, // 靜態(tài)節(jié)點引用 createVNode('p', null, ctx.title, 1 /* TEXT */) ])) }
可以看到,經過靜態(tài)提升后,在渲染函數(shù)內只會持有對靜態(tài)節(jié)點的引用。當響應式數(shù)據(jù)發(fā)生變化,并使得渲染函數(shù)重新執(zhí)行時,并不會重新創(chuàng)建靜態(tài)的虛擬節(jié)點,從而避免了額外的性能開銷。
transform 轉換器
在模板編譯器將模板編譯為渲染函數(shù)的過程中,transform 函數(shù)扮演著十分重要的角色。它用來將模板AST轉換為 JavaScript AST。下面,我們來看看 transform 函數(shù)做了什么。
// packages/compiler-core/src/transform.ts // 將 模板AST 轉換為 JavaScript AST export function transform(root: RootNode, options: TransformOptions) { // 創(chuàng)建轉換上下文 const context = createTransformContext(root, options) // 遍歷所有節(jié)點,執(zhí)行轉換 traverseNode(root, context) // 如果編譯選項中打開了 hoistStatic 選項,則進行靜態(tài)提升 if (options.hoistStatic) { hoistStatic(root, context) } // 創(chuàng)建 Block,收集所有動態(tài)子節(jié)點 if (!options.ssr) { createRootCodegen(root, context) } // finalize meta information // 確定最終的元信息 root.helpers = [...context.helpers.keys()] root.components = [...context.components] root.directives = [...context.directives] root.imports = context.imports root.hoists = context.hoists root.temps = context.temps root.cached = context.cached if (__COMPAT__) { root.filters = [...context.filters!] } }
從上面的源碼中可以看到,transform 函數(shù)的實現(xiàn)并不復雜。
- 首先,調用 createTransformContext 函數(shù)創(chuàng)建了一個轉換上下文對象,該對象存儲著轉換過程中的一些上下文數(shù)據(jù)。例如當前正在轉換的節(jié)點 currentNode、當前轉換節(jié)點的父節(jié)點parent、用于替換當前正在轉換的節(jié)點的 replaceNode 函數(shù)、用于移除當前訪問的節(jié)點的 removeNode 函數(shù)等。
- 接著調用 traverseNode 函數(shù),遞歸遍歷模板AST,將模板AST 轉換為 JavaScript AST。
- 然后判斷編譯選項options中是否開啟了 hoistStatic,如果是,則進行靜態(tài)提升。
- 接下來則創(chuàng)建 Block,收集模板中的動態(tài)子節(jié)點。
- 最后做的事情則是確定最終的元信息。
由于本文主要是介紹靜態(tài)提升,因此我們圍繞靜態(tài)提升的代碼繼續(xù)往下探索,其余部分代碼將在其它文章中介紹。
hoistStatic 靜態(tài)提升
hoistStatic 函數(shù)的源碼如下所示:
// packages/compiler-core/src/transforms/hoistStatic.ts export function hoistStatic(root: RootNode, context: TransformContext) { walk( root, context, // Root node is unfortunately non-hoistable due to potential parent // fallthrough attributes. // 根節(jié)點作為 Block 角色是不能被提升的 isSingleElementRoot(root, root.children[0]) ) }
可以看到,hoistStatic 函數(shù)接收兩個參數(shù),第一個參數(shù)是根節(jié)點,第二個參數(shù)是轉換上下文。在該函數(shù)中,僅僅只是調用了 walk 函數(shù)來實現(xiàn)靜態(tài)提升。
并且在 walk 函數(shù)中調用 isSingleElementRoot 函數(shù)來告知 walk 函數(shù)根節(jié)點是不能提升的,原因是根節(jié)點作為 Block 角色是不可被提升的。
我們接下來繼續(xù)探究 walk 函數(shù)的源碼。
walk 函數(shù)
靜態(tài)提升的真正實現(xiàn)邏輯在 walk 函數(shù)內,其源碼如下所示:
function walk( node: ParentNode, context: TransformContext, // 轉換上下文對象 doNotHoistNode: boolean = false // 節(jié)點是否可以被提升 ) { const { children } = node // 子節(jié)點的數(shù)量 const originalCount = children.length // 可提升節(jié)點的數(shù)量 let hoistedCount = 0 for (let i = 0; i < children.length; i++) { const child = children[i] // only plain elements & text calls are eligible for hoisting. // 只有普通元素和文本才能被提升 if ( child.type === NodeTypes.ELEMENT && child.tagType === ElementTypes.ELEMENT ) { // 如果節(jié)點不能被提升,則將 constantType 賦值為 NOT_CONSTANT 不可被提升的標記 // 否則調用 getConstantType 獲取子節(jié)點的靜態(tài)類型:ConstantTypes 定義了子節(jié)點的靜態(tài)類型 const constantType = doNotHoistNode ? ConstantTypes.NOT_CONSTANT : getConstantType(child, context) // 如果獲取到的 constantType 枚舉值大于 NOT_CONSTANT if (constantType > ConstantTypes.NOT_CONSTANT) { // 如果節(jié)點可以被提升 if (constantType >= ConstantTypes.CAN_HOIST) { // 則將子節(jié)點的 codegenNode 屬性的 patchFlag 標記為 HOISTED ,即可提升 ;(child.codegenNode as VNodeCall).patchFlag = PatchFlags.HOISTED + (__DEV__ ? ` /* HOISTED */` : ``) // 提升節(jié)點,將節(jié)點存儲到 轉換上下文context 的 hoist 數(shù)組中 child.codegenNode = context.hoist(child.codegenNode!) // 提升節(jié)點數(shù)量自增1 hoistedCount++ continue } } else { // node may contain dynamic children, but its props may be eligible for // hoisting. // 包含動態(tài)綁定的節(jié)點本身不會被提升,該動態(tài)節(jié)點上可能存在純靜態(tài)的屬性,靜態(tài)的屬性可以被提升 const codegenNode = child.codegenNode! if (codegenNode.type === NodeTypes.VNODE_CALL) { // 獲取 patchFlag 補丁標志 const flag = getPatchFlag(codegenNode) // 如果不存在 patchFlag 補丁標志 或者 patchFlag 是文本類型 // 并且該節(jié)點的 props 是可以被提升的 if ( (!flag || flag === PatchFlags.NEED_PATCH || flag === PatchFlags.TEXT) && getGeneratedPropsConstantType(child, context) >= ConstantTypes.CAN_HOIST ) { // 獲取節(jié)點的 props,并在轉換上下文對象中執(zhí)行提升操作, // 將被提升的 props 添加到轉換上下文context 的 hoist 數(shù)組中 const props = getNodeProps(child) if (props) { codegenNode.props = context.hoist(props) } } // 將節(jié)點的動態(tài) props 添加到轉換上下文對象中 if (codegenNode.dynamicProps) { codegenNode.dynamicProps = context.hoist(codegenNode.dynamicProps) } } } } else if ( // 如果是節(jié)點類型是 TEXT_CALL,并且節(jié)點可以被提升 child.type === NodeTypes.TEXT_CALL && getConstantType(child.content, context) >= ConstantTypes.CAN_HOIST ) { // 提升節(jié)點 child.codegenNode = context.hoist(child.codegenNode) hoistedCount++ } // walk further if (child.type === NodeTypes.ELEMENT) { // 如果子節(jié)點的 tagType 是組件,則繼續(xù)遍歷子節(jié)點 // 以判斷插槽中的情況 const isComponent = child.tagType === ElementTypes.COMPONENT if (isComponent) { context.scopes.vSlot++ } // 執(zhí)行 walk函數(shù),繼續(xù)判斷插槽中的節(jié)點及節(jié)點屬性是否可以被提升 walk(child, context) if (isComponent) { context.scopes.vSlot-- } } else if (child.type === NodeTypes.FOR) { // Do not hoist v-for single child because it has to be a block // 帶有 v-for 指令的節(jié)點是一個 Block // 如果 v-for 的節(jié)點中只有一個子節(jié)點,則不能被提升 walk(child, context, child.children.length === 1) } else if (child.type === NodeTypes.IF) { // 帶有 v-if 指令的節(jié)點是一個 Block for (let i = 0; i < child.branches.length; i++) { // Do not hoist v-if single child because it has to be a block // 如果只有一個分支條件,則不進行提升 walk( child.branches[i], context, child.branches[i].children.length === 1 ) } } } // 將被提升的節(jié)點序列化,即轉換成字符串 if (hoistedCount && context.transformHoist) { context.transformHoist(children, context, node) } // all children were hoisted - the entire children array is hoistable. if ( hoistedCount && hoistedCount === originalCount && node.type === NodeTypes.ELEMENT && node.tagType === ElementTypes.ELEMENT && node.codegenNode && node.codegenNode.type === NodeTypes.VNODE_CALL && isArray(node.codegenNode.children) ) { node.codegenNode.children = context.hoist( createArrayExpression(node.codegenNode.children) ) } }
可以看到,walk 函數(shù)的代碼比較常,下面,我們將分步對其進行解析。
1、首先,我們來看 walk 函數(shù)的函數(shù)簽名,代碼如下:
function walk( node: ParentNode, context: TransformContext, // 轉換上下文對象 doNotHoistNode: boolean = false // 節(jié)點是否可以被提升 )
從函數(shù)簽名中可以知道,walk 函數(shù)接收三個參數(shù),第一個參數(shù)是一個 node 節(jié)點,第二個參數(shù)是轉換器的轉換上下文對象 context,第三個參數(shù) doNotHoistNode 是一個布爾值,用來判斷傳入節(jié)點的子節(jié)點是否可以被提升。
2、初始化兩個變量,originalCount:子節(jié)點的數(shù)量;hoistedCount:可提升節(jié)點的數(shù)量,該變量將會用于判斷被提升的節(jié)點是否可序列化。
const { children } = node // 子節(jié)點的數(shù)量 const originalCount = children.length // 可提升節(jié)點的數(shù)量 let hoistedCount = 0
3、我們來看 for 循環(huán)語句里的第一個 if 語句分支,這里處理的是普通元素和靜態(tài)文本被提升的情況。
// only plain elements & text calls are eligible for hoisting. // 只有普通元素和文本才能被提升 if ( child.type === NodeTypes.ELEMENT && child.tagType === ElementTypes.ELEMENT ) { // 如果節(jié)點不能被提升,則將 constantType 賦值為 NOT_CONSTANT 不可被提升的標記 // 否則調用 getConstantType 獲取子節(jié)點的靜態(tài)類型:ConstantTypes 定義了子節(jié)點的靜態(tài)類型 const constantType = doNotHoistNode ? ConstantTypes.NOT_CONSTANT : getConstantType(child, context) // 如果獲取到的 constantType 枚舉值大于 NOT_CONSTANT if (constantType > ConstantTypes.NOT_CONSTANT) { // 如果節(jié)點可以被提升 if (constantType >= ConstantTypes.CAN_HOIST) { // 則將子節(jié)點的 codegenNode 屬性的 patchFlag 標記為 HOISTED ,即可提升 ;(child.codegenNode as VNodeCall).patchFlag = PatchFlags.HOISTED + (__DEV__ ? ` /* HOISTED */` : ``) // 提升節(jié)點,將節(jié)點存儲到 轉換上下文context 的 hoist 數(shù)組中 child.codegenNode = context.hoist(child.codegenNode!) // 提升節(jié)點數(shù)量自增1 hoistedCount++ continue } } else { // node may contain dynamic children, but its props may be eligible for // hoisting. // 包含動態(tài)綁定的節(jié)點本身不會被提升,該動態(tài)節(jié)點上可能存在純靜態(tài)的屬性,靜態(tài)的屬性可以被提升 const codegenNode = child.codegenNode! if (codegenNode.type === NodeTypes.VNODE_CALL) { // 獲取 patchFlag 補丁標志 const flag = getPatchFlag(codegenNode) // 如果不存在 patchFlag 補丁標志 或者 patchFlag 是文本類型 // 并且該節(jié)點的 props 是可以被提升的 if ( (!flag || flag === PatchFlags.NEED_PATCH || flag === PatchFlags.TEXT) && getGeneratedPropsConstantType(child, context) >= ConstantTypes.CAN_HOIST ) { // 獲取節(jié)點的 props,并在轉換上下文對象中執(zhí)行提升操作, // 將被提升的 props 添加到轉換上下文context 的 hoist 數(shù)組中 const props = getNodeProps(child) if (props) { codegenNode.props = context.hoist(props) } } // 將節(jié)點的動態(tài) props 添加到轉換上下文對象中 if (codegenNode.dynamicProps) { codegenNode.dynamicProps = context.hoist(codegenNode.dynamicProps) } } }
首先通過外部傳入的 doNotHoistNode 參數(shù)來獲取子節(jié)點的靜態(tài)類型。如果 doNotHoistNode 為 true,則將 constantType 的值設置為 ConstantType 枚舉值中的 NOT_CONSTANT,即不可被提升。否則通過 getConstantType 函數(shù)獲取子節(jié)點的靜態(tài)類型。如下面的代碼所示:
// 如果節(jié)點不能被提升,則將 constantType 賦值為 NOT_CONSTANT 不可被提升的標記 // 否則調用 getConstantType 獲取子節(jié)點的靜態(tài)類型:ConstantTypes 定義了子節(jié)點的靜態(tài)類型 const constantType = doNotHoistNode ? ConstantTypes.NOT_CONSTANT : getConstantType(child, context)
接下來通過判斷 constantType 的枚舉值來處理是需要提升靜態(tài)節(jié)點還是提升動態(tài)節(jié)點的靜態(tài)屬性。
- 如果獲取到的 constantType 枚舉值大于 NOT_CONSTANT,說明該節(jié)點可能被提升或序列化為字符串。
- 如果該節(jié)點可以被提升,則將節(jié)點 codegenNode 屬性的 patchFlag 標記為 PatchFlags.HOISTED ,即可提升。
然后執(zhí)行轉換器上下文中的 hoist 方法,將該節(jié)點存儲到轉換上下文context 的 hoist 數(shù)組中,該數(shù)組中存儲的是可被提升的節(jié)點。如下面的代碼所示:
// 如果獲取到的 constantType 枚舉值大于 NOT_CONSTANT if (constantType > ConstantTypes.NOT_CONSTANT) { // 如果節(jié)點可以被提升 if (constantType >= ConstantTypes.CAN_HOIST) { // 則將子節(jié)點的 codegenNode 屬性的 patchFlag 標記為 HOISTED ,即可提升 ;(child.codegenNode as VNodeCall).patchFlag = PatchFlags.HOISTED + (__DEV__ ? ` /* HOISTED */` : ``) // 提升節(jié)點,將節(jié)點存儲到 轉換上下文context 的 hoist 數(shù)組中 child.codegenNode = context.hoist(child.codegenNode!) // 提升節(jié)點數(shù)量自增1 hoistedCount++ continue } }
如果獲取到的 constantType 枚舉值不大于 NOT_CONSTANT,說明該節(jié)點包含動態(tài)綁定,包含動態(tài)綁定的節(jié)點上如果存在純靜態(tài)的 props,那么這些靜態(tài)的 props 是可以被提升的。
從下面的代碼中我們可以看到,在提升靜態(tài)的 props 時,同樣是執(zhí)行轉換器上下文中的 hoist 方法,將靜態(tài)的props存儲到轉換上下文context 的 hoist 數(shù)組中。
如下面的代碼所示:
else { // node may contain dynamic children, but its props may be eligible for // hoisting. // 包含動態(tài)綁定的節(jié)點本身不會被提升,該動態(tài)節(jié)點上可能存在純靜態(tài)的屬性,靜態(tài)的屬性可以被提升 const codegenNode = child.codegenNode! if (codegenNode.type === NodeTypes.VNODE_CALL) { // 獲取 patchFlag 補丁標志 const flag = getPatchFlag(codegenNode) // 如果不存在 patchFlag 補丁標志 或者 patchFlag 是文本類型 // 并且該節(jié)點的 props 是可以被提升的 if ( (!flag || flag === PatchFlags.NEED_PATCH || flag === PatchFlags.TEXT) && getGeneratedPropsConstantType(child, context) >= ConstantTypes.CAN_HOIST ) { // 獲取節(jié)點的 props,并在轉換上下文對象中執(zhí)行提升操作, // 將被提升的 props 添加到轉換上下文context 的 hoist 數(shù)組中 const props = getNodeProps(child) if (props) { codegenNode.props = context.hoist(props) } } // 將節(jié)點的動態(tài) props 添加到轉換上下文對象中 if (codegenNode.dynamicProps) { codegenNode.dynamicProps = context.hoist(codegenNode.dynamicProps) } } }
4、如果節(jié)點類型時是 TEXT_CALL 類型并且該節(jié)點可以被提升,則同樣執(zhí)行轉換器上下文中的 hoist 方法,將節(jié)點存儲到轉換上下文context 的 hoist 數(shù)組中,如下面的代碼所示:
else if ( // 如果是節(jié)點類型是 TEXT_CALL,并且節(jié)點可以被提升 child.type === NodeTypes.TEXT_CALL && getConstantType(child.content, context) >= ConstantTypes.CAN_HOIST ) { // 提升節(jié)點 child.codegenNode = context.hoist(child.codegenNode) hoistedCount++ }
5、如果子節(jié)點是組件,則遞歸調用 walk 函數(shù),繼續(xù)遍歷子節(jié)點,以判斷插槽中的節(jié)點及節(jié)點屬性是否可以被提升。如下面的代碼所示:
if (child.type === NodeTypes.ELEMENT) { // 如果子節(jié)點的 tagType 是組件,則繼續(xù)遍歷子節(jié)點 // 以判斷插槽中的情況 const isComponent = child.tagType === ElementTypes.COMPONENT if (isComponent) { context.scopes.vSlot++ } // 執(zhí)行 walk函數(shù),繼續(xù)判斷插槽中的節(jié)點及節(jié)點屬性是否可以被提升 walk(child, context) if (isComponent) { context.scopes.vSlot-- } }
6、如果節(jié)點上帶有 v-for 指令或 v-if 指令,則遞歸調用 walk 函數(shù),繼續(xù)判斷子節(jié)點是否可以被提升。如果 v-for 指令的節(jié)點只有一個子節(jié)點,v-if 指令的節(jié)點只有一個分支條件,則不進行提升。如下面的代碼所示:
else if (child.type === NodeTypes.FOR) { // Do not hoist v-for single child because it has to be a block // 帶有 v-for 指令的節(jié)點是一個 Block // 如果 v-for 的節(jié)點中只有一個子節(jié)點,則不能被提升 walk(child, context, child.children.length === 1) } else if (child.type === NodeTypes.IF) { // 帶有 v-if 指令的節(jié)點是一個 Block for (let i = 0; i < child.branches.length; i++) { // Do not hoist v-if single child because it has to be a block // 如果只有一個分支條件,則不進行提升 walk( child.branches[i], context, child.branches[i].children.length === 1 ) } }
walk 函數(shù)流程圖
總結
靜態(tài)提升,就是指在編譯器編譯的過程中,將一些靜態(tài)的節(jié)點或屬性提升到渲染函數(shù)之外。它能夠減少更新時創(chuàng)建虛擬 DOM 帶來的性能開銷和內存占用。
對于純靜態(tài)的節(jié)點和動態(tài)節(jié)點上的純靜態(tài)屬性,則直接執(zhí)行轉換器上下文中的 hoist 方法,將節(jié)點或屬性進行提升。如果節(jié)點是組件、節(jié)點上帶有 v-for 指令或v-if 指令,則遞歸調用 walk 函數(shù)判斷子節(jié)點是否可以被提升。
以上就是Vue3 源碼解讀靜態(tài)提升詳解的詳細內容,更多關于Vue3 靜態(tài)提升的資料請關注腳本之家其它相關文章!
相關文章
Vue3.2?setup語法糖及Hook函數(shù)基本使用
這篇文章主要為大家介紹了Vue3.2?setup語法糖及Hook函數(shù)基本使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07vuejs2.0實現(xiàn)分頁組件使用$emit進行事件監(jiān)聽數(shù)據(jù)傳遞的方法
這篇文章主要介紹了vuejs2.0實現(xiàn)分頁組件使用$emit進行事件監(jiān)聽數(shù)據(jù)傳遞的方法,非常不錯,具有參考借鑒價值,,需要的朋友可以參考下2017-02-02基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗
VuePress為每一個由它生成的頁面提供預加載的html,不僅加載速度極佳,同時對seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗,需要的朋友可以參考下2018-04-04Vue利用computed配合watch實現(xiàn)監(jiān)聽多個屬性的變化
這篇文章主要給大家介紹了在Vue中巧用computed配合watch實現(xiàn)監(jiān)聽多個屬性的變化的方法,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-10-10詳解vue數(shù)據(jù)渲染出現(xiàn)閃爍問題
本篇文章主要介紹了vue數(shù)據(jù)渲染出現(xiàn)閃爍問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06