1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-16 14:00:40 +00:00

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
This commit is contained in:
aaronp64
2025-05-02 12:32:11 -04:00
parent 730adf4801
commit a4ba8c37c9

View File

@@ -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 = value_arr[0];
String key = v_key; String val = value_arr[1];
Variant v_val = value_arr[1];
String val = v_val;
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 = values_arr[i];
String val = v_val;
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);
} }