<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">I’ve made my own version of std::vector which is called il::Vector. Due to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html" class="">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html</a>, LLVM can optimise away memory allocation. Therefore, the following code optimise away all memory allocation for w resulting in a single allocation during the whole program (for v).</div><div class=""><br class=""></div><div class="">When using my own vector implementation, I realised that the allocation were not optimized away because I was using ::operator new. When I’ve switched back to new, the optimisation came back.</div><div class=""><br class=""></div><div class="">Is it expected or a bug from LLVM?</div><div class=""><br class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><span class="" style="background-color: rgba(255, 255, 255, 0);">François Fayard<br class="">Founder & Consultant - Inside Loop</span></div><div class=""><span class="" style="background-color: rgba(255, 255, 255, 0);">Tel: <a href="tel:+33%206%2001%2044%2006%2093" x-apple-data-detectors="true" x-apple-data-detectors-type="telephone" x-apple-data-detectors-result="0/1" class="">+33 (0)6 01 44 06 93</a></span></div><div class=""><span class="" style="background-color: rgba(255, 255, 255, 0);">Web: <a href="http://www.insideloop.io" class="">www.insideloop.io</a></span></div></div><div class="">Twitter: @insideloop_io</div></div></div></div></div></div>
</div>
<br class=""></div><div class="">=====</div><div class=""><br class=""></div><div class=""><div class="">#include <iostream></div><div class="">#include <vector></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">std::vector<double> f_val(std::size_t i, std::size_t n) {</div><div class="">    auto v = std::vector<double>(n);</div><div class="">    for (std::size_t k = 0; k < v.size(); ++k) {</div><div class="">        v[k] = static_cast<double>(i);</div><div class="">    }</div><div class="">    return v;</div><div class="">}</div><div class=""><br class=""></div><div class="">int main (int argc, char const *argv[])</div><div class="">{</div><div class="">    const auto n = std::size_t{10};</div><div class="">    const auto nb_loops = std::size_t{300000000};</div><div class=""><br class=""></div><div class="">    auto v = std::vector<double>( n, 0.0 );</div><div class="">    for (std::size_t i = 0; i < nb_loops; ++i) {</div><div class="">        auto w = f_val(i, n);</div><div class="">        for (std::size_t k = 0; k < v.size(); ++k) {</div><div class="">            v[k] += w[k];</div><div class="">        }</div><div class="">    }</div><div class="">    std::cout << v[0] << " " << v[n - 1] << std::endl;</div><div class=""><br class=""></div><div class="">    return 0;</div><div class="">}</div></div></body></html>