我可以合理地期望编译器能够内联什么? [英] What can I reasonably expect a compiler to be able to inline?

查看:102
本文介绍了我可以合理地期望编译器能够内联什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以使用任何通用规则来评估现代编译器是否将内联函数?额外的堆栈框架的相对成本是多少(我知道它很小,但是有什么方法可以大致量化它-在一个数量级左右)?

Are there any general rules I can use for evaluating whether a modern compiler will inline a function? What is the relative cost of an extra stack frame (I know it's very small, but is there any way to generally quantify it - within an order of magnitude or so)?

我也特别感兴趣:

  • 编译器可以内联cpp中定义的方法吗?
  • 我知道有些编译器甚至在调试中也实现了一些优化(VS在调试中使用RVO而不是NRVO)-内联的情况是什么?我可以想象它已被禁用,以便我们可以看到预期的调用堆栈以进行调试.

我目前正在尝试对内存跟踪系统进行微优化,特别是那些在未启用优化的情况下(在调试中)也可以应用的系统.

I'm currently trying to micro-optimize a memory tracking system, specifically ones that also apply without optimization enabled (in debug).

推荐答案

这很容易预测,很难预测.简单的表达式,例如:

It's easy to predict and hard to predict. Simple expressions, like:

int a = b + (2 * c):
int d = e + (2 * c);

通过最简单的优化进行优化((2 * c)公共子表达式"仅计算一次.

get optimized with the simplest optimizations (the (2 * c) "common subexpression" will only be computed once.

在C/C ++中,通常内联声明的方法将是(尽管并非总是如此).

In C/C++, methods declared inlined generally will be (though not always).

更棘手的是循环优化等.例如

Trickier are loop optimizations and the like. Eg,

for (int i = 1; i < n; i++) {
    a = i + (2 * c);
}

在执行全局优化"的编译器中,表达式"(2 * c)"通常会退出循环,而在仅执行局部优化"的编译器中则不会.而且,当然,表达式会变得更加复杂和令人费解.

the expression (2 * c) will usually get pulled out of the loop, in a compiler that does "global optimization", but not in one that does only "local optimization". And, of course, expressions can get much more complicated and convoluted.

将上述循环的主体更改为a = i * (2 * c);,您将进行到更高级别的全局优化(称为循环归纳"). 智能"编译器会发现,对于循环中的每次迭代,只需将2 * c(预先计算的)添加到a中,而不是对每次迭代进行(更昂贵的)乘法.

Change the body of the above loop to a = i * (2 * c);, and you progress to a slightly higher level of global optimization known as "loop induction". A "smart" compiler will figure out to just add 2 * c (as precomputed) to a for each iteration through the loop, vs doing the (more expensive) multiply on each iteration.

那只是擦伤表面.

但是我不知道Visual Studio编译器的功能.

But I have no idea what the Visual Studio compilers are capable of.

这篇关于我可以合理地期望编译器能够内联什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆