From a4ba8c37c9aca5dc06bb8a653e0ff79868660bae Mon Sep 17 00:00:00 2001 From: aaronp64 Date: Fri, 2 May 2025 12:32:11 -0400 Subject: [PATCH] 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 --- core/string/ustring.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 8a5abad5df..9751b6fb8b 100644 --- a/core/string/ustring.cpp +++ b/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 new_string = String(ptr()); + String new_string = *this; if (values.get_type() == Variant::ARRAY) { Array values_arr = values; 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]] Array value_arr = values_arr[i]; 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); } 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()); } } 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('_')) { - new_string = new_string.replace(placeholder.replace("_", i_as_str), val); + new_string = new_string.replace(placeholder.replace("_", String::num_int64(i)), val); } else { new_string = new_string.replace_first(placeholder, val); }