Browse Source

Minor optimization: No reason to construct the observer block until it is used.

Michael Ragazzon 5 years ago
parent
commit
e1031027be
3 changed files with 29 additions and 29 deletions
  1. 27 27
      Include/RmlUi/Core/ObserverPtr.h
  2. 1 1
      Source/Core/Context.cpp
  3. 1 1
      Source/Core/ObserverPtr.cpp

+ 27 - 27
Include/RmlUi/Core/ObserverPtr.h

@@ -106,12 +106,13 @@ public:
 		return block ? static_cast<T*>(block->pointed_to_object) : nullptr;
 	}
 	// Dereference the pointed to object.
-	T* operator->() const noexcept { return static_cast<T*>(block->pointed_to_object); }
+	T* operator->() const noexcept {
+		return static_cast<T*>(block->pointed_to_object);
+	}
 
 	// Reset the pointer so that it does not point to anything.
 	// When the pointed to object and all observer pointers to it have been destroyed, it will deallocate the block.
-	void reset() noexcept
-	{
+	void reset() noexcept {
 		if (block)
 		{
 			block->num_observers -= 1;
@@ -121,11 +122,9 @@ public:
 	}
 
 private:
-
 	friend class Rml::EnableObserverPtr<T>;
 
-	explicit ObserverPtr(ObserverPtrBlock* block) noexcept : block(block)
-	{
+	explicit ObserverPtr(ObserverPtrBlock* block) noexcept : block(block) {
 		if (block)
 			block->num_observers += 1;
 	}
@@ -135,39 +134,38 @@ private:
 
 
 
-
 template<typename T>
 class RMLUICORE_API EnableObserverPtr {
 public:
 
-	ObserverPtr<T> GetObserverPtr() const noexcept { return ObserverPtr<T>(block); }
+	ObserverPtr<T> GetObserverPtr() {
+		InitializeBlock();
+		return ObserverPtr<T>(block);
+	}
 
 protected:
-
-	EnableObserverPtr()
-	{
+	EnableObserverPtr() noexcept {
 		static_assert(std::is_base_of<EnableObserverPtr<T>, T>::value, "T must derive from EnableObserverPtr<T>.");
-		InitializeBlock();
 	}
 
-	~EnableObserverPtr() noexcept 
-	{
-		block->pointed_to_object = nullptr;
-		DeallocateObserverPtrBlockIfEmpty(block);
+	~EnableObserverPtr() noexcept {
+		if (block)
+		{
+			block->pointed_to_object = nullptr;
+			DeallocateObserverPtrBlockIfEmpty(block);
+		}
 	}
 
-	EnableObserverPtr(const EnableObserverPtr<T>&) {
-		// We do not copy or modify the block, it should always point to the same object.
-		InitializeBlock();
+	EnableObserverPtr(const EnableObserverPtr<T>&) noexcept {
+		// Do not copy or modify the block, it should always point to the same object.
 	}
 	EnableObserverPtr<T>& operator=(const EnableObserverPtr<T>&) noexcept { 
 		// Assignment should not do anything, the block must point to the initially constructed object.
 		return *this; 
 	}
 
-	EnableObserverPtr(EnableObserverPtr<T>&&) {
-		// We do not move or modify the block, it should always point to the same object.
-		InitializeBlock();
+	EnableObserverPtr(EnableObserverPtr<T>&&) noexcept {
+		// Do not move or modify the block, it should always point to the same object.
 	}
 	EnableObserverPtr<T>& operator=(EnableObserverPtr<T>&&) noexcept {
 		// Assignment should not do anything, the block must point to the initially constructed object.
@@ -178,15 +176,17 @@ private:
 
 	inline void InitializeBlock()
 	{
-		block = AllocateObserverPtrBlock();
-		block->num_observers = 0;
-		block->pointed_to_object = static_cast<void*>(static_cast<T*>(this));
+		if (!block)
+		{
+			block = AllocateObserverPtrBlock();
+			block->num_observers = 0;
+			block->pointed_to_object = static_cast<void*>(static_cast<T*>(this));
+		}
 	}
 
-	ObserverPtrBlock* block;
+	ObserverPtrBlock* block = nullptr;
 };
 
 
-
 } // namespace Rml
 #endif

+ 1 - 1
Source/Core/Context.cpp

@@ -1280,7 +1280,7 @@ public:
 	using container_type = ElementObserverList;
 
 	ElementObserverListBackInserter(ElementObserverList& elements) : elements(&elements) {}
-	ElementObserverListBackInserter& operator=(const Element* element) {
+	ElementObserverListBackInserter& operator=(Element* element) {
 		elements->push_back(element->GetObserverPtr());
 		return *this;
 	}

+ 1 - 1
Source/Core/ObserverPtr.cpp

@@ -34,7 +34,7 @@ namespace Rml {
 static Pool< ObserverPtrBlock >& GetPool()
 {
 	// Wrap pool in a function to ensure it is initialized before use.
-	static Pool< ObserverPtrBlock > pool(400, true);
+	static Pool< ObserverPtrBlock > pool(128, true);
 	return pool;
 }