Skip to content

Commit 7b15c06

Browse files
committed
Merge pull request #100295 from Ivorforce/string-builder-inplace
Optimize `StringBuilder.as_string` by constructing the string in-place and skipping unnecessary checks.
2 parents 321bd35 + 76af953 commit 7b15c06

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

core/string/string_builder.cpp

Lines changed: 5 additions & 6 deletions
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)