简体   繁体   English

定义新的优化类型

[英]Define a new type of optimization

Is there a way to tell g++ more about a type, function, or specific variable (other than attributes) that I might know is safe to preform. 有没有一种方法可以告诉g++有关我可能知道可以安全执行的类型,函数或特定变量(属性除外)的更多信息。

Example: 例:

TurnLedOn();
TurnLedOn();

Only the first function actually turns the LED on the second function does not actually do anything....so would it be possible to tell g++ more about the function so that it gets rid of a second call if it knows that the LED is on (because it knows that a corresponding TurnLedOff() function has not been called).... 只有第一个功能实际上打开了第二个功能上的LED实际上并没有执行任何操作....因此,可以告诉g++有关该功能的更多信息,以便在知道LED点亮的情况下摆脱第二个调用(因为它知道尚未调用相应的TurnLedOff()函数)。

The reason I do not want to use g++ attributes is because I want to arbitrarily define optimizations, which is really not possible with attributes (and I believe the optimization I am trying here is not actually possible to begin with using attributes) 我不想使用g++属性的原因是因为我想任意定义优化,而使用属性实际上是不可能的(而且我相信我在这里尝试的优化实际上不可能从使用属性开始)

These are optimisations you need to code. 这些是您需要编码的优化。 Such as: 如:

class LedSwitch {
    bool isOn{false};
public:
    inline void turnLedOn(){
        if (!isOn) {
            isOn = true;
            // ...
        }
    }
    // ...
}
// ...

If the code inlines then the compiler might then notice the bool negated in the second hardcoded sequential call, but why do that in the first place? 如果代码内联,则编译器可能会注意到在第二个硬编码的顺序调用中否定了布尔值,但是为什么要首先这样做呢?

Maybe you should revisit design if things like this are slowing down your code. 如果类似这样的事情在减慢代码速度,也许您应该重新设计。

One possibility is to make it so that the second TurnLedOn call does nothing, and make it inline and declare it in a header file so the compiler can see the definition in any source file: 一种可能是使之成为第二个TurnLedOn调用不执行任何操作, 并使其内联并在头文件中声明它,以便编译器可以在任何源文件中查看定义:

extern bool isLedOn; // defined somewhere else
inline void TurnLedOn()
{
    if(!isLedOn)
    {
        ActuallyTurnLedOn();
        isLedOn = true;
    }
}

Then the compiler might be able to figure out by itself that calling TurnLedOn twice does nothing useful. 这样,编译器便可以自己TurnLedOn两次调用TurnLedOn并没有什么用处。 Of course, as with any optimization, you have no guarantees. 当然,与任何优化一样,您无法保证。

Contrary to your thinking, the answer by @immibis is what you were expecting. 与您的想法相反,@ immibis的回答是您的期望。

This way to describe the complex behavior of the function TurnLedOn (ie needn't be called twice in a row unless unlocked by some other action) is indeed how you tell the compiler to perform this "optimization". 这种描述函数TurnLedOn复杂行为的方法(即,除非被其他操作解锁,否则不必连续调用两次)确实是您告诉编译器执行此“优化”的方式。

Could you imagine other annotations such as 您能否想象其他注释,例如

#pragma call_once_toggle_pair(TurnLEDOn, TurnLEDOff)

with innumerable variants describing all your vagaries ? 有无数的变体描述您所有的变化吗?

The C++ language has enough provisions to let you express arbitrarily complex situations, please don't add yet a layer of complexity on top of that. C ++语言有足够的规定可以让您表达任意复杂的情况,请不要在此之上增加任何复杂性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM