From 2721728c9056b442c0281f20792f19eb6a491aa0 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 25 Nov 2020 11:36:24 +0100 Subject: [PATCH] Optimize code in QTaggedPointer Don't execute instructions that will never do anything. Directly add the tag to the pointer in the constructor to avoid additional masking operations, and avoid a masking op that is in practice a no-op in setTag(). Do the same optimization in QTagPreservingPointerToPointer. Pick-to: dev Change-Id: Ia364f89cbe6ccc876ec9bda0c239fc4f57c10501 Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qpropertyprivate.h | 2 +- src/corelib/tools/qtaggedpointer.h | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index 0edfe5781b7..91d9e56d2f0 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -283,7 +283,7 @@ public: void setPointer(T *ptr) { - *d = (reinterpret_cast(ptr) & QTaggedPointer::pointerMask()) | (*d & QTaggedPointer::tagMask()); + *d = reinterpret_cast(ptr) | (*d & QTaggedPointer::tagMask()); } T *get() const diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h index 819ebb2387c..f6ce85eebc3 100644 --- a/src/corelib/tools/qtaggedpointer.h +++ b/src/corelib/tools/qtaggedpointer.h @@ -83,14 +83,13 @@ public: constexpr QTaggedPointer(std::nullptr_t) noexcept : QTaggedPointer() {} explicit QTaggedPointer(T *pointer, Tag tag = Tag()) noexcept - : d(quintptr(pointer)) + : d(quintptr(pointer) | quintptr(tag)) { static_assert(sizeof(Type*) == sizeof(QTaggedPointer)); - Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0, - "QTaggedPointer", "Pointer is not aligned"); - - setTag(tag); + Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0, "QTaggedPointer", "Pointer is not aligned"); + Q_ASSERT_X((static_cast::TagType>(tag) & pointerMask()) == 0, + "QTaggedPointer::setTag", "Tag is larger than allowed by number of available tag bits"); } Type &operator*() const noexcept @@ -125,7 +124,7 @@ public: Q_ASSERT_X((static_cast::TagType>(tag) & pointerMask()) == 0, "QTaggedPointer::setTag", "Tag is larger than allowed by number of available tag bits"); - d = (d & pointerMask()) | (static_cast::TagType>(tag) & tagMask()); + d = (d & pointerMask()) | static_cast(tag); } Tag tag() const noexcept