p_511

编译器在没有指示下,会做‘ safe optimization',因此有些优化在没有参数的指示下是不会对代码做优化的,故在程序中应该避免某一类代码,因为它们妨碍了编译器做优化。

optimization blocks: aspects of programs that can severely limit the opportunities for a compiler to generate optimized code;

两类optimization blocks:

1、memory aliasing

 pointers may generate the same memory location is known as memory aliasing. In performing only safe optimizations, the compiler must assume that different pointers may be aliased, limiting the set of possible optimizations.

// from cmu
/* Sum rows is of n X n matrix a
and store in vector b */
void sum_rows1(int *a, int *b, int n) {
int i, j;
for (i = ; i < n; i++) {
b[i] = ;
for (j = ; j < n; j++)
b[i] += a[i*n + j];
}
} /*如果我们调用时为
  int A[9];
  int* B = A + 3;
  sum_rows1(A, B);
  如果编译器将其优化成如下类的形式,显然有背原意
   void sum_rows2(int *a, int *b, int n) {
    int i, j;
    for (i = 0; i < n; i++) {
    double val = 0;
    for (j = 0; j < n; j++)
        val += a[i*n + j];
         b[i] = val;
    }
}*/ /*
简化了
sum_rows1:
.L4:
movl %ebp, %ecx
movl $0, (%edx,%ebp,4)
movl $0, %eax
.L3:
movl (%esi,%eax,4), %ebx ;(%esi, %eax, 4) : &a[i*n + j]
addl %ebx, (%edx,%ecx,4) ;(%edx, %ecx, 4) : &b[i]
addl $1, %eax ;%eax : j
cmpl %edi, %eax ;%edi : n
jne .L3 addl $1, %ebp ;%ebp : i
addl (%esp), %esi
cmpl %edi, %ebp
jne .L4
*/
//书上例子
//当xp = yp时,twiddle1 与 twiddle2显然不同,因此编译器不会做一些优化,以免将twiddle1优化成与twiddle2相同的功能函数
void twiddle1(int *xp, int* yp)
{
*xp += *yp;
*xp += *yp;
} void twiddle2(int *xp, int* yp)
{
*xp += * *yp;
}

2、procedure calls

Most compilers do not try to determine whether a function is free of side effects and hence is a candidate for optimizations.Instead, the compiler assumes the worst case and leaves function call intact(原封不动)

//经典(from cmu)
void lower(char* s)
{
int i = ;
for (i = ; i < strlen(s); i++)
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] -= ('A' - 'a');
} //编译器为何不把strlen(s)提取出来,作为一个临时量,这样就可以减少函数调用了? //Why couldn’t compiler move strlen out of inner loop?
// (1)Procedure may have side effects
// Alters global state each time called
// (2)Function may not return same value for given arguments
// Depends on other parts of global state
// Procedure lower could interact with strlen //Warning:
()Compiler treats procedure call as a black box
()Weak optimizations near them
//书上的例子
int f(void); int func1(void)
{
return f() + f() + f() + f();
//procedure call不可优化成4*f(), 否则就错了
} int func2(void)
{
return * f();
} int counter = ; int f(void)
{
return counter++;
}

optimization blocks (csapp chapter 5.1)的更多相关文章

  1. CSAPP Chapter 8:Exception Control Flow

    prcesssor在运行时,假设program counter的值为a0, a1, ... , an-1,每个ak表示相对应的instruction的地址.从ak到ak+1的变化被称为control ...

  2. [CSAPP] Chapter 1 Overview of Computer

    1.1 information is bits + context All computer programs are just a sequence of bits, each with a val ...

  3. java.lang.String (JDK1.8)

    String类实现了java.io.Serializable, Comparable<String>, CharSequence这三个interface. 看了下这三个interface中 ...

  4. fstrict-aliasing

    承如“optimization blocks”文中所述,由于相同的指针可能指向相关的内存区,因此编译器将不做过分的优化…… 特意搜了下编译器在不同的优化等级下都有哪些默认优化,因此有了此记录(比较长, ...

  5. 转 zabbix 优化方法 以及数据库查询方法 两则

    ###########sample 1 https://www.cnblogs.com/hanshanxiaoheshang/p/10304672.html (不错) 如何从zabbix server ...

  6. 【Convex Optimization (by Boyd) 学习笔记】Chapter 1 - Mathematical Optimization

    以下笔记参考自Boyd老师的教材[Convex Optimization]. I. Mathematical Optimization 1.1 定义 数学优化问题(Mathematical Optim ...

  7. 《CSAPP》读书杂记 - Chapter 2. Representing and Manipulating Information

    1. 一段查看地址内容的代码 代码: #include <stdio.h> typedef unsigned char *byte_pointer; void show_bytes(byt ...

  8. 【Convex Optimization (by Boyd) 学习笔记】Chapter 2 - Convex sets(1) 仿射集&凸集

    I. 仿射凸集(Affine and convex sets) 1. 线与线段 假设\(R^n\)空间内两点\(x_1,x_2\, (x_1≠x_2)\),那么\(y=\theta x_1+(1-\t ...

  9. Chapter 14. Blocks and Statements

    14.5. Statements There are many kinds of statements in the Java programming language. Most correspon ...

随机推荐

  1. php教程之Smarty模板用法实例

    分享下php之Smarty模板的使用方法. 剖析了smarty模板的使用方法,对于学习smarty的朋友有一定的参考价值. 详情如下: 一.模板中的注释每一个Smarty模板文件,都是通过Web前台语 ...

  2. vue-cli脚手架里如何配置屏幕自适应

    很多同学可能在写h5的时候,也会遇到移动端如何控制屏幕自适应问题!在移动端网页开发中,我们可以用手机淘宝的flexible.那么在vue当中,也同样可以用!接下来就介绍下如何在vue-cli配置的项目 ...

  3. IOS 使用XIB 自定义View

    一般自定义View       代码方式 有 在初始化的时候添加 子Views - (id)initWithFrame:(CGRect)frame { self = [super initWithFr ...

  4. get_class_vars

    个人理解:感觉就是一个将类里的属性值转换为数组的一个东西. <?php class myclass { var $var1; // 此变量没有默认值…… var $var2 = "xy ...

  5. 【Unity笔记】UGUI中Canvas屏幕适配

    1.通过RectTransform中的Anchors和Pivot来进行控件和窗体的布局适配. Anchors控制当前Panel相对于父窗体的布局位置,可以设置为居中或者左上角,当父窗体拉伸的时候当前P ...

  6. 【C#/WPF】图像变换的Undo撤销——用Stack命令栈

    需求: 图层中有一张图片,可以对该图层进行平移.缩放.旋转操作,现在要求做Undo撤销功能,使得图层回复上一步操作时的状态. 关于图像的平移.缩放.旋转,可以参考在下的另一篇博客的整理: http:/ ...

  7. linux下设置了SSH免密码登录但还是需要输入密码的解决办法

    今天在设置linux的免密码登录后,仍然需要输入密码,后来找到了原因,是因为用户没有权限修改.ssh目录下的know_hosts文件导致的. 具体情况是这样的: 在/home/username/.ss ...

  8. [Django学习]分页

    分页 Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中 Paginator对象 Paginator(列表,int):返回分页对象,参数为列表数据, ...

  9. iOS边练边学--Segue数据逆传(用block代替delegate)

    一.block与方法的异同点: 相同点是都是保存代码段,什么时候执行,什么时候调用 不同点是block不受类或者对象的约束:方法收到了类或者对象的约束 二.思路:(通讯录练习) 在联系人控制器中,添加 ...

  10. spring boot 自学笔记(四) Redis集成—Jedis

    上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例. 修改Red ...