<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - atomics are 20x slower compared to gcc if atomic<T>'s T is a packed struct"
   href="https://llvm.org/bugs/show_bug.cgi?id=25335">25335</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>atomics are 20x slower compared to gcc if atomic<T>'s T is a packed struct
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>sami.liedes@iki.fi
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider this code:

------------------------------------------------------------
#include <array>
#include <atomic>
#include <cstdint>
#include <iostream>

struct Entry {
    uint32_t pos;
};

constexpr size_t N = 1000000000;

std::array<std::atomic<Entry>, N> *arr;

int main() {
    arr = new std::array<std::atomic<Entry>, N>();

    std::cout << "lock_free(): " << (*arr)[100].is_lock_free() << std::endl;

    for (size_t i=0; i<N; i++) {
        Entry e;
        e.pos = 1;
        (*arr)[i].store(e, std::memory_order_relaxed);
    }
}
------------------------------------------------------------

When compiled with gcc 4.9 -std=c++14 -O3 for x86_64:

------------------------------------------------------------
$ time ./atomic_bench.gcc
lock_free(): 1

real    0m0.689s
user    0m0.490s
sys     0m0.198s
------------------------------------------------------------

When compiled with recent clang HEAD, -std=c++14 -O3:

------------------------------------------------------------
$ time ./atomic_bench.clang
lock_free(): 1

real    0m11.120s
user    0m10.931s
sys     0m0.187s
------------------------------------------------------------

Profiling shows that the bunch of the time is spent in __atomic_store_4.

Curiously, removing the packed attribute from the struct makes clang as fast as
gcc. However, it is a 4-byte struct; there hardly is a reason why writing it
entirely could not be just as fast as a 4-byte integer.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>