Optimize StringBuilder.as_string by constructing the string in-place and skipping unnecessary checks.

Co-authored-by: YYF233333 <nbyyf2002@mail.ustc.edu.cn>
This commit is contained in:
Lukas Tenbrink 2024-12-12 01:17:02 +01:00
parent a40fc2354a
commit 76af9537ed

View File

@ -61,7 +61,9 @@ String StringBuilder::as_string() const {
return "";
}
char32_t *buffer = memnew_arr(char32_t, string_length);
String string;
string.resize(string_length + 1);
char32_t *buffer = string.ptrw();
int current_position = 0;
@ -92,10 +94,7 @@ String StringBuilder::as_string() const {
c_string_elem++;
}
}
buffer[current_position] = 0;
String final_string = String(buffer, string_length);
memdelete_arr(buffer);
return final_string;
return string;
}