Skip to content

Commit 76af953

Browse files
IvorforceYYF233333
andcommitted
Optimize StringBuilder.as_string by constructing the string in-place and skipping unnecessary checks.
Co-authored-by: YYF233333 <nbyyf2002@mail.ustc.edu.cn>
1 parent a40fc23 commit 76af953

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

core/string/string_builder.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ String StringBuilder::as_string() const {
6161
return "";
6262
}
6363

64-
char32_t *buffer = memnew_arr(char32_t, string_length);
64+
String string;
65+
string.resize(string_length + 1);
66+
char32_t *buffer = string.ptrw();
6567

6668
int current_position = 0;
6769

@@ -92,10 +94,7 @@ String StringBuilder::as_string() const {
9294
c_string_elem++;
9395
}
9496
}
97+
buffer[current_position] = 0;
9598

96-
String final_string = String(buffer, string_length);
97-
98-
memdelete_arr(buffer);
99-
100-
return final_string;
99+
return string;
101100
}

0 commit comments

Comments
 (0)