在不同模式(debug,release)下,汇编的代码是不一样的。比如在debug,会取值入栈,再从栈取到寄存器等等
优化主要是指汇编指令变少,跳转变少(其中内存读写耗时)。
其中A11 CPU架构中 读写数据速度 寄存器 > 高速缓存 > 内存 > 磁盘
1.Optimization Level优化
其中Optimization有多种
- None
- Fast
- Faster
- Fastest
- 线上release默认 Fastest,Smallest (又快又小)
- Fastest Aggressive optimizations
比如下面简单代码在不同模式下的汇编代码
1 2 3 4 5 6 7 8 9 10 |
int sum(int a, int b) { return a+b; } int main(int argc, char * argv[]) { //sum(1,2); return 0; } |
在 None模式下:
1 2 3 4 5 6 7 8 9 |
huibian-switch`main: 0x100662918 <+0>: sub sp, sp, #0x10 ; =0x10 0x10066291c <+4>: mov w8, #0x0 0x100662920 <+8>: str wzr, [sp, #0xc] 0x100662924 <+12>: str w0, [sp, #0x8] 0x100662928 <+16>: str x1, [sp] -> 0x10066292c <+20>: mov x0, x8 0x100662930 <+24>: add sp, sp, #0x10 ; =0x10 0x100662934 <+28>: ret |
在Fastest Smallest模式下没用使用代码的情况:他直接ret (去掉一些没用垃圾代码)
在Fastest Smallest模式下,有使用代码(他会在编译的时候把函数换成结果,进而优化)
1 2 3 4 5 6 7 8 9 10 11 12 |
int sum(int a, int b) { return a+b; } int main(int argc, char * argv[]) { int s = sum(1, 2); printf("%d",s); return 0; } |
汇编代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
huibian-switch`main: 0x1008feb5c <+0>: sub sp, sp, #0x20 ; =0x20 0x1008feb60 <+4>: stp x29, x30, [sp, #0x10] 0x1008feb64 <+8>: add x29, sp, #0x10 ; =0x10 -> 0x1008feb68 <+12>: orr w8, wzr, #0x3 0x1008feb6c <+16>: str x8, [sp] 0x1008feb70 <+20>: adr x0, #0x13b8 ; "%d" 0x1008feb74 <+24>: nop 0x1008feb78 <+28>: bl 0x1008febf8 ; symbol stub for: printf 0x1008feb7c <+32>: mov w0, #0x0 0x1008feb80 <+36>: ldp x29, x30, [sp, #0x10] 0x1008feb84 <+40>: add sp, sp, #0x20 ; =0x20 0x1008feb88 <+44>: ret |
其中函数在编译的时候已经知道实现的代码结果(内存中找)
其中app安装在磁盘,打开在内存,然后把内存放入8M的高速缓存中,然后寄存器直接去高速缓存中读取