Más contenido relacionado La actualidad más candente (20) Similar a Avm2虚拟机浅析与as3性能优化(陈士凯) (20) Avm2虚拟机浅析与as3性能优化(陈士凯)2. An Introduction to AVM2 & AS3.0 Optimization AVM2虚拟机介绍 对AS3进行性能分析 { Agenda AS3代码优化启发 对AVM2进行扩充和改造 Demos 3. An Introduction to AVM2 & AS3.0 Optimization AVM2 虚拟机 从Flash Player 9.0开始引入,用于AS3.0代码的解释和二进制翻译执行。 采用JIT/Interpret混合执行,大幅提高运行效率 AS3比AS1/2运行速度提升~10x 内建对XML, Array类型的处理支持 已经开源: Tamarin Project I Introduction to AVM2 4. An Introduction to AVM2 & AS3.0 Optimization Flash Framework SWF files .ABC ( Actionscript Byte Code) ActionScript Runtime Flash Player AVM1/AVM2 OS 13. An Introduction to AVM2 & AS3.0 Optimization AS3 -> .ABC AS3 function func(x:int):int{ varans:int; ans= x + 10; return ans; } .abc 0 getlocal0 1 pushscope 2 pushbyte 0 4 setlocal2 5 getlocal1 6 pushbyte 10 8 add 9 convert_i 10 setlocal2 11 getlocal2 12 returnvalue 14. An Introduction to AVM2 & AS3.0 Optimization 对ABC的解析执行(Interpret) 逐条解析执行,没有优化、低效 .abc 0 getlocal0 1 pushscope 2 pushbyte 0 4 setlocal2 5 getlocal1 6 pushbyte 10 8 add 9 convert_i 10 setlocal2 11 getlocal2 12 returnvalue for(;;) { switch (*pc++){ … case op_add: a1 = sp[-1]; a2 = sp[0]; sp--; … dest= toplevel->add2(a1, a2); } … 15. An Introduction to AVM2 & AS3.0 Optimization 对ABC的即时二进制翻译(JIT) ABC -> MIR/LIR -> Target Native Code 高效率、允许进行运行时优化,如CSE和死码删除 .abc 0 getlocal0 1 pushscope 2 pushbyte 0 4 setlocal2 5 getlocal1 6 pushbyte 10 8 add 9 convert_i 10 setlocal2 11 getlocal2 12 returnvalue MIR/LIR @40 use @17 [0] @41 use @22 [1] @42 imm 10 @46 add @41 @42 @47 def @46 @49 usea @47 @50 st8(@7) <- @49 @54 def@46 spans call … @57 use @54 [4] @58 ret @57 x86 Assembly moveax, -88(ebp) movecx, -92(ebp) add ecx, 10 movedx, ecx mov-84(ebp), edx lea edx, -84(ebp) mov -20(ebp), edx mov -96(ebp), ecx moveax, -96(ebp) jmp 2147483647 16. An Introduction to AVM2 & AS3.0 Optimization 对ABC的即时二进制翻译(JIT) ABC -> MIR/LIR -> Target Native Code 高效率、允许运行时优化,如CSE和死码删除 .abc 0 getlocal0 1 pushscope 2 pushbyte 0 4 setlocal2 5 getlocal1 6 pushbyte 10 8 add 9 convert_i 10 setlocal2 11 getlocal2 12 returnvalue MIR/LIR @40 use @17 [0] @41 use @22 [1] @42 imm 10 @46 add @41 @42 @47 def @46 @49 usea @47 @50 st8(@7) <- @49 @54 def@46 spans call … @57 use @54 [4] @58 ret @57 x86 Assembly moveax, -88(ebp) movecx, -92(ebp) add ecx, 10 movedx, ecx mov-84(ebp), edx lea edx, -84(ebp) mov -20(ebp), edx mov -96(ebp), ecx moveax, -96(ebp) jmp 2147483647 17. An Introduction to AVM2 & AS3.0 Optimization JIT V.S. Interpret 同一段代码在第一次JIT时往往花费的时间较长 代码翻译比较耗时 当再次执行这段代码后,VM将直接调用先前的翻译结果,速度提高。 Interpert对同一段代码均具有大致相同的执行速度。 JIT并非性能总优于Interpret: 运行时间 Interpret JIT 执行次数 JIT触发阈值 71. An Introduction to AVM2 & AS3.0 Optimization ActionScript的动态分析 – Code Tracing 和 Profiling AS3DynamicProfile = 1 total count=278 cycles=35508249 avg CPI=127727 user 1.9% gc 0% decoder 97.7% verifier 1.6% codegen 0.2% count cycles %count %time CPIopcode ----- -------- ------- ------- --- ------ 2 34714742 0.7 97.7 17357371 decode 211 602237 75.8 1.6 2854 verifyop 6 89138 2.1 0.2 14856 codegenop 3 78305 1.0 0.2 26101 newclass 7 4813 2.5 0.0 687 findpropstrict 6 3922 2.1 0.0 653 setslot 4 3260 1.4 0.0 815 initproperty 4 2724 1.4 0.0 681 getproperty 9 1859 3.2 0.0 206 getlocal0 77. An Introduction to AVM2 & AS3.0 Optimization ActionScript的动态分析举例 – 分析Flash启动机制和JIT规律 结论:构造函数也会被JIT翻译,仅静态构造函数会被解析执行。 图中演示了$cinit和构造函数执行相同的benchmark的性能表现 83. 对性能的影响*:for (i = 0; i < 100000; i++) { myArray[i] = i; } for (i = 100000; i >=0; i--) { myArray[i] = i; } ~11ms ~44ms * http://jpauclair.net/2009/12/02/tamarin-part-i-as3-array/ 98. the medium goal is to provide specialized libraries to use sockets or database like SQLite etc. 99. the long term goal is to provide a native API that replicate some part of the Flash Player and Adobe Integrated Runtime (AIR) APIIII AVM2 Mod 109. Tamarin part III – Garbage Collector in Flash (10.0) http://jpauclair.net/ # Reference 133. An Introduction to AVM2 & AS3.0 Optimization AVM2 JIT优化分析 – CSE 和 DCE 0:getlocal0 @40 use @17 [0] 1:pushscope 2:pushbyte 0 cse @12 4:setlocal2 5:getlocal1 @41 use @22 [1] 6:pushbyte 10 @42 imm 10 8:add @43 i2d @41 @44 i2d @42 @45 fadd @43 @44 9:convert_i @46 add @41 @42 dead @45 dead @44 dead @43 10:setlocal2 11:getlocal2 12:returnvalue cse @15 @58 ret @57 137. java –jar asc.jar –import global.abc –import playerglobal.abc –swf main,800,600,60 main.as 141. An Introduction to AVM2 & AS3.0 Optimization 分析AVM2实现(Tamarin)进行AS3代码优化 * http://jpauclair.net/2009/12/02/tamarin-part-i-as3-array/