Qml应用性能优化QML的执行•QML的编译•QML的执行、实例化•QML的渲染QML的编译QML的执行、实例化QML的显示setSourceApp.execQML的设计思想•QML应用在启动时,会对qml进行编译,并且对所有元素进行实例化–比如显示一个Button:id.visiable=true•QML不是一个传统的解释型语言,运行时–将所有代码编译–将所有代码实例化–目的:牺牲启动性能,保证应用的运行性能•QML的这种设计,导致了。。。QML性能分析ListMyListItem.qmlMyListItem.qmlMyListItem.qmlMyListItem.qmlMyListItem.qmlMyListItem.qmlMyListItem.qmlTextButtonCMOS_BasicDialogCMOS_BasicDialog应用在启动的时候,会创建14个CMOS_BasicDialog,每个CMOS_BasicDialog用时20ms,总共花费280msqmlprofiler•需要在应用的.pro开启qmldebug:CONFIG+=qml_debug•编辑appconfig.xml,修改应用的启动项/usr/bin/cmoscalculator-qmljsdebugger=port:3768,block•重启机器•点击运行应用•在PC端运行QtCreatorQML的编译•所有通过import方式引入进来的qml代码都将会被编译•一个qml文件只会被编译一次•Qml文件会被编译成为特定的字节码控制QML的编译•将QML文件分到多个不同的QML文件•采用动态创建QML组件的方式QML实例化•所有元素在运行前都会被实例化–内存占用很高–应用的启动速度很慢•优化策略:用到的时候才需要实例化–采用Qt.createComponent()的方式,动态创建qml组件三种不同的写法•不编译、不实例化Qt.createComponent(“qrc:/qml/Alarm/AlarmMain.qml”)•只编译,不实例化Component{Item{CMOS_PageStackWindow{}CMOS_BasicDialog{}CMOS_LineEdit{}}}•编译且实例化Tab{id:alarm}异步Loader•本身并不节省时间•可以增加并行效果•可以在总时间不变的情况下,提高用户体验性能优化•Doitfaster•Doitinparallel•Doitlater•Don’tdoitatall•DoitbeforeQML的优化技巧•TextElement•Image•ControllingElementLifetime•Render•PlainText•StyledText•AutoText•RichTextImages•AsynchronousLoading–Imagesareoftenquitelarge,andsoitiswisetoensurethatloadinganimagedoesn'tblocktheUIthread.SettheasynchronouspropertyoftheQMLImageelementtotruetoenableasynchronousloadingofimagesfromthelocalfilesystem•ExplicitSourceSize–Ifyourapplicationloadsalargeimagebutdisplaysitinasmall-sizedelement,setthesourceSizepropertytothesizeoftheelementbeingrenderedtoensurethatthesmaller-scaledversionoftheimageiskeptinmemory,ratherthanthelargeone.ControllingElementLifetime•LazyInitialization–thereisnobetterwaytoreducestartuptimethantoavoiddoingworkyoudon'tneedtodo,anddelayingtheworkuntilitisnecessary.•UsingLoader–UsingtheactivepropertyofaLoader,initializationcanbedelayeduntilrequired.–UsingtheoverloadedversionofthesetSource()function,initialpropertyvaluescanbesupplied.–SettingtheLoaderasynchronouspropertytotruemayalsoimprovefluiditywhileacomponentisinstantiated.ControllingElementLifetime•UsingDynamicCreation–DeveloperscanusetheQt.createComponent()functiontocreateacomponentdynamicallyatruntimefromwithinJavaScript,andthencallcreateObject()toinstantiateit.•DestroyUnusedElements–Elementswhichareinvisiblebecausetheyareachildofanon-visibleelement(forexample,thesecondtabinatab-widget,whilethefirsttabisshown)shouldbeinitializedlazilyinmostcases,anddeletedwhennolongerinuse,toavoidtheongoingcostofleavingthemactive(forexample,rendering,animations,propertybindingevaluation,etc).–AnitemloadedwithaLoaderelementmaybereleasedbyresettingthesourceorsourceComponentpropertyoftheLoader,whileotheritemsmaybeexplicitlyreleasedbycallingdestroy()onthem.Insomecases,itmaybenecessarytoleavetheitemactive,inwhichcaseitshouldbemadeinvisibleattheveryleast.Render•Over-drawingandInvisibleElements–Ifyouhaveelementswhicharetotallycoveredbyother(opaque)elements,itisbesttosettheirvisiblepropertytofalseortheywillbedrawnneedlessly.•TranslucentvsOpaque–Opaquecontentisgenerallyalotfastertodrawthantranslucent.Thereasonbeingthattranslucentcontentneedsblendingandthattherenderercanpotentiallyoptimizeopaquecontentbetter.•Shader–Whendeployingtolow-endhardwareandtheshaderiscoveringalargeamountofpixels,oneshouldkeepthefragmentshadertoafewinstructionstoavoidpoorperformance.文件管理器的优化FileEditBar.qml{CMOS_BasicDialog{id:deleteConfirmDialog}CMOS_BasicDialog{id:renameDialog}CMOS_BasicDialog{id:progressDialog}}•两种优化方法–动态创建–Re-useexistingcomponents应用启动的优化•生成Image时,运行prelink•使用Booster机制•进行CPU调频•调整应用的启动效果•发布Release版本Booster的机制mapplauncherBoosterinvokerFork一个进程Preloadpreload.qml启动进程命令Booster加载进程重命名进程,继续加载对应QML对Booster的优化•优化前CMOS_PageStackWindow{CMOS_BasicDialog{}CMOS_LineEdit{}CMOS_ListView{}CMOS_TabView{}CMOS_Page{}}•优化后Item{Component{Item{CMOS_PageStackWindow{}CMOS_BasicDialog{}CMOS_LineEdit{}CMOS_ListView{}CMOS_TabView{}CMOS_Page{}}}}调整应用启动的效果•为每个应用创建一个splash图片•启动应用的时候,显示启动动画,显示splash图片–启动动画的时长从500ms,减少到300ms•在应用内容刷新后,在将splash与应用内容之间做个淡入淡出的效果参考文档••QmlOptimizationTips