Browse Source

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.
aaronp64 2 months ago
parent
commit
8fb3697916
3 changed files with 9 additions and 6 deletions
  1. 5 2
      core/io/resource.cpp
  2. 2 2
      core/math/expression.cpp
  3. 2 2
      core/variant/variant_parser.cpp

+ 5 - 2
core/io/resource.cpp

@@ -142,15 +142,18 @@ String Resource::generate_scene_unique_id() {
 	static constexpr uint32_t char_count = ('z' - 'a');
 	static constexpr uint32_t char_count = ('z' - 'a');
 	static constexpr uint32_t base = char_count + ('9' - '0');
 	static constexpr uint32_t base = char_count + ('9' - '0');
 	String id;
 	String id;
+	id.resize(characters + 1);
+	char32_t *ptr = id.ptrw();
 	for (uint32_t i = 0; i < characters; i++) {
 	for (uint32_t i = 0; i < characters; i++) {
 		uint32_t c = random_num % base;
 		uint32_t c = random_num % base;
 		if (c < char_count) {
 		if (c < char_count) {
-			id += String::chr('a' + c);
+			ptr[i] = ('a' + c);
 		} else {
 		} else {
-			id += String::chr('0' + (c - char_count));
+			ptr[i] = ('0' + (c - char_count));
 		}
 		}
 		random_num /= base;
 		random_num /= base;
 	}
 	}
+	ptr[characters] = '\0';
 
 
 	return id;
 	return id;
 }
 }

+ 2 - 2
core/math/expression.cpp

@@ -408,7 +408,7 @@ Error Expression::_get_token(Token &r_token) {
 						if (reading == READING_DONE) {
 						if (reading == READING_DONE) {
 							break;
 							break;
 						}
 						}
-						num += String::chr(c);
+						num += c;
 						c = GET_CHAR();
 						c = GET_CHAR();
 						is_first_char = false;
 						is_first_char = false;
 					}
 					}
@@ -435,7 +435,7 @@ Error Expression::_get_token(Token &r_token) {
 					cchar = GET_CHAR();
 					cchar = GET_CHAR();
 
 
 					while (is_unicode_identifier_continue(cchar)) {
 					while (is_unicode_identifier_continue(cchar)) {
-						id += String::chr(cchar);
+						id += cchar;
 						cchar = GET_CHAR();
 						cchar = GET_CHAR();
 					}
 					}
 
 

+ 2 - 2
core/variant/variant_parser.cpp

@@ -1757,7 +1757,7 @@ Error VariantParser::_parse_tag(Token &token, Stream *p_stream, int &line, Strin
 				} else {
 				} else {
 					escaping = false;
 					escaping = false;
 				}
 				}
-				r_tag.name += String::chr(c);
+				r_tag.name += c;
 			}
 			}
 		}
 		}
 
 
@@ -1902,7 +1902,7 @@ Error VariantParser::parse_tag_assign_eof(Stream *p_stream, int &line, String &r
 				what = tk.value;
 				what = tk.value;
 
 
 			} else if (c != '=') {
 			} else if (c != '=') {
-				what += String::chr(c);
+				what += c;
 			} else {
 			} else {
 				r_assign = what;
 				r_assign = what;
 				Token token;
 				Token token;