Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2294)

Unified Diff: base/memory/weak_ptr.cc

Issue 2963623002: Make base::WeakPtr::Get() fast (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/memory/weak_ptr.h ('K') | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/weak_ptr.cc
diff --git a/base/memory/weak_ptr.cc b/base/memory/weak_ptr.cc
index 591b955136a3bafaa0f37720461f48e14c9b220a..393cfb8ee50b7534c156095feb4c9d56cb73e715 100644
--- a/base/memory/weak_ptr.cc
+++ b/base/memory/weak_ptr.cc
@@ -4,75 +4,53 @@
#include "base/memory/weak_ptr.h"
+#include "base/debug/leak_annotations.h"
+
namespace base {
namespace internal {
+static constexpr uintptr_t kTrueMask = ~static_cast<uintptr_t>(0);
+
+WeakReference::Flag::Flag() : is_valid_(kTrueMask) {
#if DCHECK_IS_ON()
-WeakReference::NewFlag::NewFlag(WeakReference::NewFlag::NullFlagTag)
- : is_valid_(false), ref_count_(1) {
// Flags only become bound when checked for validity, or invalidated,
// so that we can check that later validity/invalidation operations on
// the same Flag take place on the same sequenced thread.
sequence_checker_.DetachFromSequence();
-}
-#else
-constexpr WeakReference::NewFlag::NewFlag(WeakReference::NewFlag::NullFlagTag)
- : is_valid_(false), ref_count_(1) {}
#endif
-
-WeakReference::NewFlag WeakReference::NewFlag::g_null_flag(
- WeakReference::NewFlag::kNullFlagTag);
-
-#if 0
-WeakReference::Flag::Flag() : is_valid_(true) {
- // Flags only become bound when checked for validity, or invalidated,
- // so that we can check that later validity/invalidation operations on
- // the same Flag take place on the same sequenced thread.
- sequence_checker_.DetachFromSequence();
}
-void WeakReference::Flag::Invalidate() {
- // The flag being invalidated with a single ref implies that there are no
- // weak pointers in existence. Allow deletion on other thread in this case.
- DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef())
- << "WeakPtrs must be invalidated on the same sequenced thread.";
- is_valid_ = false;
-}
+WeakReference::Flag::Flag(WeakReference::Flag::NullFlagTag) : is_valid_(false) {
+ // There is no need for sequence_checker_.DetachFromSequence() because the
+ // null flag doesn't participate in the sequence checks.
-bool WeakReference::Flag::IsValid() const {
- DCHECK(sequence_checker_.CalledOnValidSequence())
- << "WeakPtrs must be checked on the same sequenced thread.";
- return is_valid_;
+ AddRef(); // Stayin' alive.
}
-WeakReference::Flag::~Flag() {
+WeakReference::Flag* WeakReference::Flag::nullFlag() {
+ ANNOTATE_SCOPED_MEMORY_LEAK;
+ static Flag* g_null_flag = new Flag(kNullFlagTag);
+ return g_null_flag;
}
-#endif
-WeakReference::WeakReference() : new_flag_(NewFlag::NullFlag()) {
-}
+WeakReference::Flag::~Flag() {}
-WeakReference::WeakReference(/*const Flag* flag,*/ const NewFlag* newFlag)
- : /*flag_(flag),*/ new_flag_(newFlag) {}
+WeakReference::WeakReference() : flag_(Flag::nullFlag()) {}
WeakReference::~WeakReference() {
}
-WeakReference::WeakReference(WeakReference&& other) : new_flag_(std::move(other.new_flag_)) {
- other.new_flag_ = NewFlag::NullFlag();
+WeakReference::WeakReference(const Flag* flag) : flag_(flag) {}
+
+WeakReference::WeakReference(WeakReference&& other)
+ : flag_(std::move(other.flag_)) {
+ other.flag_ = Flag::nullFlag();
}
WeakReference::WeakReference(const WeakReference& other) = default;
-#if 0
-bool WeakReference::is_valid() const {
- //return flag_.get() && flag_->IsValid();
- return new_flag_->IsValid();
-}
-#endif
-
-WeakReferenceOwner::WeakReferenceOwner() : new_flag_(WeakReference::NewFlag::NullFlag()) {
-}
+WeakReferenceOwner::WeakReferenceOwner()
+ : flag_(WeakReference::Flag::nullFlag()) {}
WeakReferenceOwner::~WeakReferenceOwner() {
Invalidate();
@@ -80,36 +58,29 @@ WeakReferenceOwner::~WeakReferenceOwner() {
WeakReference WeakReferenceOwner::GetRef() const {
// If we hold the last reference to the Flag then create a new one.
- if (!HasRefs()) {
- //flag_ = new WeakReference::Flag();
- new_flag_ = new WeakReference::NewFlag();
- }
+ if (!HasRefs())
+ flag_ = new WeakReference::Flag();
- return WeakReference(/*flag_.get(),*/ new_flag_.get());
+ return WeakReference(flag_.get());
}
void WeakReferenceOwner::Invalidate() {
-#if 0
- if (flag_.get()) {
- flag_->Invalidate();
- flag_ = NULL;
- }
-#endif
- new_flag_->Invalidate();
- new_flag_ = WeakReference::NewFlag::NullFlag();
+ flag_->Invalidate();
+ flag_ = WeakReference::Flag::nullFlag();
}
-WeakPtrBase::WeakPtrBase() : ptr_(0) { }
+WeakPtrBase::WeakPtrBase() : ptr_(0) {}
-WeakPtrBase::~WeakPtrBase() { }
+WeakPtrBase::~WeakPtrBase() {}
WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
}
-} // namespace internal
-
WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {}
-WeakPtrFactoryBase::~WeakPtrFactoryBase() { ptr_ = 0; }
+WeakPtrFactoryBase::~WeakPtrFactoryBase() {
+ ptr_ = 0;
+}
+} // namespace internal
} // namespace base
« base/memory/weak_ptr.h ('K') | « base/memory/weak_ptr.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698