Browse Source

Reduce allocations/copies in String::format

- Updated initial new_string copy to use copy constructor/increase ref count instead of copying to new allocated memory
- Removed Variant copies from Array before assigning to String
- Only convert i to String when needed
aaronp64 4 months ago
parent
commit
a4ba8c37c9
1 changed files with 5 additions and 11 deletions
  1. 5 11
      core/string/ustring.cpp

+ 5 - 11
core/string/ustring.cpp

@@ -3843,34 +3843,28 @@ bool String::matchn(const String &p_wildcard) const {
 }
 }
 
 
 String String::format(const Variant &values, const String &placeholder) const {
 String String::format(const Variant &values, const String &placeholder) const {
-	String new_string = String(ptr());
+	String new_string = *this;
 
 
 	if (values.get_type() == Variant::ARRAY) {
 	if (values.get_type() == Variant::ARRAY) {
 		Array values_arr = values;
 		Array values_arr = values;
 
 
 		for (int i = 0; i < values_arr.size(); i++) {
 		for (int i = 0; i < values_arr.size(); i++) {
-			String i_as_str = String::num_int64(i);
-
 			if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
 			if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
 				Array value_arr = values_arr[i];
 				Array value_arr = values_arr[i];
 
 
 				if (value_arr.size() == 2) {
 				if (value_arr.size() == 2) {
-					Variant v_key = value_arr[0];
-					String key = v_key;
-
-					Variant v_val = value_arr[1];
-					String val = v_val;
+					String key = value_arr[0];
+					String val = value_arr[1];
 
 
 					new_string = new_string.replace(placeholder.replace("_", key), val);
 					new_string = new_string.replace(placeholder.replace("_", key), val);
 				} else {
 				} else {
 					ERR_PRINT(vformat("Invalid format: the inner Array at index %d needs to contain only 2 elements, as a key-value pair.", i).ascii().get_data());
 					ERR_PRINT(vformat("Invalid format: the inner Array at index %d needs to contain only 2 elements, as a key-value pair.", i).ascii().get_data());
 				}
 				}
 			} else { //Array structure ["RobotGuy","Logis","rookie"]
 			} else { //Array structure ["RobotGuy","Logis","rookie"]
-				Variant v_val = values_arr[i];
-				String val = v_val;
+				String val = values_arr[i];
 
 
 				if (placeholder.contains_char('_')) {
 				if (placeholder.contains_char('_')) {
-					new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
+					new_string = new_string.replace(placeholder.replace("_", String::num_int64(i)), val);
 				} else {
 				} else {
 					new_string = new_string.replace_first(placeholder, val);
 					new_string = new_string.replace_first(placeholder, val);
 				}
 				}