diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-11-25 11:36:24 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-11-30 23:02:28 +0000 |
commit | 1181286d43bd3f0d4712307c60563d75d4615de3 (patch) | |
tree | 596809932c206a57ce5d43df9f1e7bfe710c74ef /src/corelib/tools | |
parent | de4a73ebd6ab686f18a3b5f450eb3dbd8a440970 (diff) |
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.
Change-Id: Ia364f89cbe6ccc876ec9bda0c239fc4f57c10501
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 2721728c9056b442c0281f20792f19eb6a491aa0)
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qtaggedpointer.h | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/src/corelib/tools/qtaggedpointer.h b/src/corelib/tools/qtaggedpointer.h index 819ebb2387..f6ce85eebc 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<T, Tag>", "Pointer is not aligned"); - - setTag(tag); + Q_ASSERT_X((quintptr(pointer) & tagMask()) == 0, "QTaggedPointer<T, Tag>", "Pointer is not aligned"); + Q_ASSERT_X((static_cast<typename QtPrivate::TagInfo<T>::TagType>(tag) & pointerMask()) == 0, + "QTaggedPointer<T, Tag>::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<typename QtPrivate::TagInfo<T>::TagType>(tag) & pointerMask()) == 0, "QTaggedPointer<T, Tag>::setTag", "Tag is larger than allowed by number of available tag bits"); - d = (d & pointerMask()) | (static_cast<typename QtPrivate::TagInfo<T>::TagType>(tag) & tagMask()); + d = (d & pointerMask()) | static_cast<quintptr>(tag); } Tag tag() const noexcept |