Browse Source

List's pushConstruct should have the same optimized allocation as push.

David Piuva 2 years ago
parent
commit
b6f37b94df
1 changed files with 9 additions and 1 deletions
  1. 9 1
      Source/DFPSR/collection/List.h

+ 9 - 1
Source/DFPSR/collection/List.h

@@ -100,7 +100,7 @@ public:
 		impl_baseZeroBoundCheck(indexB, this->length(), "Swap index B");
 		std::swap(this->backend[indexA], this->backend[indexB]);
 	}
-	// Side-effect: Adds a new element at the end
+	// Side-effect: Pushes a new element at the end
 	//   Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer
 	// Post-condition: Returns a reference to the new element in the list
 	T& push(const T& newValue) {
@@ -114,8 +114,16 @@ public:
 		return this->last();
 	}
 	// Side-effect: Pushes a new element constructed using the given arguments
+	//   Warning! Reallocation may invalidate old pointers and references to elements in the replaced buffer
+	// Post-condition: Returns a reference to the new element in the list
 	template<typename... ARGS>
 	T& pushConstruct(ARGS... args) {
+		// Optimize for speed by assuming that we have enough memory
+		if (this->length() == 0) {
+			this->backend.reserve(32);
+		} else if (this->length() >= (int64_t)this->backend.capacity()) {
+			this->backend.reserve((int64_t)this->backend.capacity() * 4);
+		}
 		this->backend.emplace_back(args...);
 		return this->last();
 	}