Skip to content

Commit 8fb3697

Browse files
committed
Avoid single character String allocations when appending characters
Removed calls to String::chr() when appending characters to Strings in Expression, Resource, and VariantParser, to avoid creating temporary Strings for each character. Also updated the Resource case to resize String up front, since size is known.
1 parent 19bb187 commit 8fb3697

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

core/io/resource.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,18 @@ String Resource::generate_scene_unique_id() {
142142
static constexpr uint32_t char_count = ('z' - 'a');
143143
static constexpr uint32_t base = char_count + ('9' - '0');
144144
String id;
145+
id.resize(characters + 1);
146+
char32_t *ptr = id.ptrw();
145147
for (uint32_t i = 0; i < characters; i++) {
146148
uint32_t c = random_num % base;
147149
if (c < char_count) {
148-
id += String::chr('a' + c);
150+
ptr[i] = ('a' + c);
149151
} else {
150-
id += String::chr('0' + (c - char_count));
152+
ptr[i] = ('0' + (c - char_count));
151153
}
152154
random_num /= base;
153155
}
156+
ptr[characters] = '\0';
154157

155158
return id;
156159
}

core/math/expression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ Error Expression::_get_token(Token &r_token) {
408408
if (reading == READING_DONE) {
409409
break;
410410
}
411-
num += String::chr(c);
411+
num += c;
412412
c = GET_CHAR();
413413
is_first_char = false;
414414
}
@@ -435,7 +435,7 @@ Error Expression::_get_token(Token &r_token) {
435435
cchar = GET_CHAR();
436436

437437
while (is_unicode_identifier_continue(cchar)) {
438-
id += String::chr(cchar);
438+
id += cchar;
439439
cchar = GET_CHAR();
440440
}
441441

core/variant/variant_parser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, Strin
17571757
} else {
17581758
escaping = false;
17591759
}
1760-
r_tag.name += String::chr(c);
1760+
r_tag.name += c;
17611761
}
17621762
}
17631763

@@ -1902,7 +1902,7 @@ Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r
19021902
what = tk.value;
19031903

19041904
} else if (c != '=') {
1905-
what += String::chr(c);
1905+
what += c;
19061906
} else {
19071907
r_assign = what;
19081908
Token token;

0 commit comments

Comments
 (0)