[LLVMdev] Deduplication of memory allocation

François Fayard fayard.francois at icloud.com
Fri May 1 09:24:28 PDT 2015


> On 01 May 2015, at 18:11, Daniel Berlin <dberlin at dberlin.org> wrote:
> 
> There will be no allocation in the first example. it will transform
> the first into the equivalent of the second.

The following code is must faster with reference semantics than with value semantics. The “value” code is not transformed into the “reference” code by llvm. Just compile this code, and the difference in timings shows.

#include <iostream>
#include <vector>


std::vector<double> f_val(std::size_t i, std::size_t n) {
    auto v = std::vector<double>( n );
    for (std::size_t k = 0; k < v.size(); ++k) {
        v[k] = static_cast<double>(k + i);
    }
    return v;
}

void f_ref(std::size_t i, std::vector<double>& v) {
    for (std::size_t k = 0; k < v.size(); ++k) {
        v[k] = static_cast<double>(k + i);
    }
}

int main (int argc, char const *argv[])
{
    const auto n = std::size_t{10};

    {
        auto v = std::vector<double>( n, 0.0 );
        for (std::size_t i = 0; i < 100000000; ++i) {
            auto w = f_val(i, n);
            for (std::size_t k = 0; k < v.size(); ++k) {
                v[k] += w[k];
            }
        }
        std::cout << v[n - 1] << std::endl;
    }

    {
        auto v = std::vector<double>( n, 0.0 );
        auto w = std::vector<double>( n );
        for (std::size_t i = 0; i < 100000000; ++i) {
            f_ref(i, w);
            for (std::size_t k = 0; k < v.size(); ++k) {
                v[k] += w[k];
            }
        }
        std::cout << v[n - 1] << std::endl;
    }

    return 0;
}





More information about the llvm-dev mailing list