Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2bd0d1e5 authored by Anton Ivanov's avatar Anton Ivanov
Browse files

LocklessQueue: move values into Entries instead of copying.

The motivation for this change is b/408710034 where the allocator
crashes while copying a value into a LocklessQueue Entry, but there is
also a performace win:

BM before:
```
----------------------------------------------------------
Benchmark                Time             CPU   Iterations
----------------------------------------------------------
pushPop/1             41.9 ns         41.9 ns     16696378
pushPop/8             38.6 ns         38.6 ns     18191833
pushPop/64            42.9 ns         42.9 ns     16353414
pushPop/512           86.1 ns         86.0 ns      8216985
pushPop/4096           705 ns          705 ns       997754
pushPop/32768         6162 ns         6157 ns       110737
pushPop/262144       57837 ns        57780 ns        12010
pushPop/1048576     227840 ns       227688 ns         3137
```

BM this change:
```
----------------------------------------------------------
Benchmark                Time             CPU   Iterations
----------------------------------------------------------
pushPop/1             26.8 ns         26.8 ns     26163075
pushPop/8             25.1 ns         25.1 ns     27890003
pushPop/64            25.9 ns         25.9 ns     27154234
pushPop/512           25.7 ns         25.7 ns     27187823
pushPop/4096          25.7 ns         25.7 ns     27187548
pushPop/32768         25.9 ns         25.8 ns     27118373
pushPop/262144        25.9 ns         25.8 ns     27081835
pushPop/1048576       25.9 ns         25.9 ns     27164591
```

Test: `BM=surfaceflinger_microbenchmarks_host_supported; m $BM && $ANDROID_HOST_OUT/benchmarktest64/$BM/$BM` ; presubmit
Flag: EXEMPT refactor
Bug: 408710034

Change-Id: Iad975257c7db2a7fff1441b43847c36493cadc01
parent 6b3382ff
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ class LocklessQueue {
public:
    bool isEmpty() { return (mPush.load() == nullptr) && (mPop.load() == nullptr); }

    void push(T value) {
    void push(T&& value) {
        Entry* entry = new Entry(std::move(value));
        Entry* previousHead = mPush.load(/*std::memory_order_relaxed*/);
        do {
@@ -79,7 +79,7 @@ private:
    public:
        T mValue;
        std::atomic<Entry*> mNext;
        Entry(T value) : mValue(value) {}
        Entry(T&& value) : mValue(std::move(value)) {}
    };
    std::atomic<Entry*> mPush = nullptr;
    std::atomic<Entry*> mPop = nullptr;