Forráskód Böngészése

Optimize Array methods with SWAP/std::move

Updated Array::shuffle() to use SWAP instead of copying through temporary variables, which makes shuffling simple types around 2x faster, and refcounted object types around 20x faster.

Also updated multiple methods that insert into/modify the array to move their validated Variant to avoid an extra copy, speed increase varies depending on type and how much else the methods are doing, usually around 5-10% faster.
aaronp64 7 hónapja
szülő
commit
3eb4e6c6ff
1 módosított fájl, 6 hozzáadás és 8 törlés
  1. 6 8
      core/variant/array.cpp

+ 6 - 8
core/variant/array.cpp

@@ -277,7 +277,7 @@ void Array::push_back(const Variant &p_value) {
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	Variant value = p_value;
 	Variant value = p_value;
 	ERR_FAIL_COND(!_p->typed.validate(value, "push_back"));
 	ERR_FAIL_COND(!_p->typed.validate(value, "push_back"));
-	_p->array.push_back(value);
+	_p->array.push_back(std::move(value));
 }
 }
 
 
 void Array::append_array(const Array &p_array) {
 void Array::append_array(const Array &p_array) {
@@ -308,14 +308,14 @@ Error Array::insert(int p_pos, const Variant &p_value) {
 	ERR_FAIL_COND_V_MSG(_p->read_only, ERR_LOCKED, "Array is in read-only state.");
 	ERR_FAIL_COND_V_MSG(_p->read_only, ERR_LOCKED, "Array is in read-only state.");
 	Variant value = p_value;
 	Variant value = p_value;
 	ERR_FAIL_COND_V(!_p->typed.validate(value, "insert"), ERR_INVALID_PARAMETER);
 	ERR_FAIL_COND_V(!_p->typed.validate(value, "insert"), ERR_INVALID_PARAMETER);
-	return _p->array.insert(p_pos, value);
+	return _p->array.insert(p_pos, std::move(value));
 }
 }
 
 
 void Array::fill(const Variant &p_value) {
 void Array::fill(const Variant &p_value) {
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	Variant value = p_value;
 	Variant value = p_value;
 	ERR_FAIL_COND(!_p->typed.validate(value, "fill"));
 	ERR_FAIL_COND(!_p->typed.validate(value, "fill"));
-	_p->array.fill(value);
+	_p->array.fill(std::move(value));
 }
 }
 
 
 void Array::erase(const Variant &p_value) {
 void Array::erase(const Variant &p_value) {
@@ -485,7 +485,7 @@ void Array::set(int p_idx, const Variant &p_value) {
 	Variant value = p_value;
 	Variant value = p_value;
 	ERR_FAIL_COND(!_p->typed.validate(value, "set"));
 	ERR_FAIL_COND(!_p->typed.validate(value, "set"));
 
 
-	operator[](p_idx) = value;
+	_p->array.write[p_idx] = std::move(value);
 }
 }
 
 
 const Variant &Array::get(int p_idx) const {
 const Variant &Array::get(int p_idx) const {
@@ -703,9 +703,7 @@ void Array::shuffle() {
 	Variant *data = _p->array.ptrw();
 	Variant *data = _p->array.ptrw();
 	for (int i = n - 1; i >= 1; i--) {
 	for (int i = n - 1; i >= 1; i--) {
 		const int j = Math::rand() % (i + 1);
 		const int j = Math::rand() % (i + 1);
-		const Variant tmp = data[j];
-		data[j] = data[i];
-		data[i] = tmp;
+		SWAP(data[i], data[j]);
 	}
 	}
 }
 }
 
 
@@ -732,7 +730,7 @@ void Array::push_front(const Variant &p_value) {
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
 	Variant value = p_value;
 	Variant value = p_value;
 	ERR_FAIL_COND(!_p->typed.validate(value, "push_front"));
 	ERR_FAIL_COND(!_p->typed.validate(value, "push_front"));
-	_p->array.insert(0, value);
+	_p->array.insert(0, std::move(value));
 }
 }
 
 
 Variant Array::pop_back() {
 Variant Array::pop_back() {