]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Optimize RefCount comparison with raw pointers (#1200)
authorsameer-here <76567789+sameer-here@users.noreply.github.com>
Wed, 7 Dec 2022 17:27:47 +0000 (17:27 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Wed, 7 Dec 2022 17:27:56 +0000 (17:27 +0000)
GCC v12 and Clang v12 tests show that compilers cannot avoid creating
temporaries when comparing RefCount objects with arbitrary pointers.

Also fixes (not yet supported) C++20 build. C++20 automatically reverses
equality comparisons, triggering an ambiguity:

    src/esi/Esi.cc:1920:26: error: ambiguous overload for 'operator=='
        (operand types are 'ESISegment::Pointer' {aka
        'RefCount<ESISegment>'} and std::nullptr_t)
        assert (output->next == nullptr);

    src/esi/Esi.cc:77:6: note: candidate: 'bool operator==(const
        ESIElement*, const Pointer&)' (reversed)

    src/base/RefCount.h:82:10: note: candidate: 'bool
        RefCount<C>::operator==(const RefCount<C>&) const [with C =
        ESISegment]'

src/base/RefCount.h

index 119b74c30b205c94d12c1ca602cd943315717569..a9aba7e9eaf54a0ee082e12c299eb268619d7bb0 100644 (file)
@@ -87,6 +87,18 @@ public:
         return p.p_ != p_;
     }
 
+    template <class Other>
+    bool operator ==(const Other * const p) const
+    {
+        return p == p_;
+    }
+
+    template <class Other>
+    bool operator !=(const Other * const p) const
+    {
+        return p != p_;
+    }
+
 private:
     void dereference(C const *newP = nullptr) {
         /* Setting p_ first is important: