Can I rely on the compiler finding and optimizing simple boolean loop invariants?

AlbertM

I have a loop like the one below which has an invariant, here the never changing value of scaleEveryValueByTwo. Can I rely on the compiler finding this invariant and not checking the condition in every iteration (essentially compiling to something anologous to the code at the bottom)?

void loadValuesFromDisk(const bool scaleEveryValueByTwo)
{
    std::vector<MyValueType> xs;
    while(fileHasNewValues())
    {
        auto x = loadNextValue();
        if (scaleEveryValueByTwo)
        {
            x *= 2;
        }
        xs.push_back(x);
    }
}

I can of course split this into two loops manually (see below) or put the scaling part in a separate function, but in many cases this makes the code much longer and in my opinion harder to read (for example if I have nested loops for all dimensions of 3D data I would duplicate all three lines of loop headers and up to six lines of curly braces).

void loadValuesFromDisk(const bool scaleEveryValueByTwo)
{
    std::vector<MyValueType> xs;
    while(fileHasNewValues())
    {
        auto x = loadNextValue();
        xs.push_back(x);
    }

    if (scaleEveryValueByTwo)
    {
        for(auto &x : xs)
        {
            x *= 2;
        }
    }
}

I'm primarily interested if I can rely on this (or even better, enforce) this optimization for commonly used compilers like gcc or MSVC, not some exotic ones that might be missing optimization that are de facto standard in most compilers.

Pavan Chandaka

Earlier there used to be /Og (global optimization) in MSVC compiler, which are now enabled by default. My guess is other compilers also do that.

To know how the loop optimization is done, look into below link and search for "Loop optimization"

https://docs.microsoft.com/en-us/cpp/build/reference/og-global-optimizations?view=vs-2019

As this comes by default now, you can rely on compiler.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I describe the invariants for this simple algorithm in Dafny?

Can an optimizing compiler add std::move?

Can I write a type guard that asserts multiple invariants?

Can I rely on the order of an unordered map?

Can I rely on the element's color?

Can I rely on the aclexplode() function in PostgreSQL?

Can I rely on output from Docker?

Can I rely on std::map::operator[] to touch?

Can I rely on CallContext using Web API?

Can I Rely on the Order of a JSON Array?

Can I rely on the comma operator in a bool expression

Can I rely on sddocname being indexed?

Can I rely on GUID to be not parseable to int?

Can I rely on a temporary file to remain unchanged after I close it?

Do I need nodejs or I can rely only on Nginx?

Optimizing/Parallel Computing a simple but big loop based on Pandas

Rely on boolean value to access pointers

Optimizing query for finding friends

Optimizing and finding the computation of this inequality

Can Boolean Arrays Be Initialized in a For Loop?

Flutter: (How) Can I have a PageView of widgets that each rely on Provider?

Can I rely on garbage collector to close asynchronous database connections in Python?

Can I rely on my AV by setting only the "File Exeuction" option?

When crawling with Scrapy, can I rely on requests having referer headers?

Can I rely on named return value optimisation for complicated return types?

Can I rely on React state being updated in a form onSubmit handle?

Can I rely on the order of select wake ups based on sending order?

Does Google Maps have a source country list that I can rely on?

Can I rely that OS schedules threads "optimal" (Parallelization)