Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize StringBuilder by using LocalVector instead of Vector. #99775

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions core/string/string_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ String StringBuilder::as_string() const {
int godot_string_elem = 0;
int c_string_elem = 0;

for (int i = 0; i < appended_strings.size(); i++) {
if (appended_strings[i] == -1) {
for (uint32_t i = 0; i < appended_strings.size(); i++) {
const int32_t str_len = appended_strings[i];

if (str_len == -1) {
// Godot string
const String &s = strings[godot_string_elem];

Expand All @@ -81,11 +83,11 @@ String StringBuilder::as_string() const {
} else {
const char *s = c_strings[c_string_elem];

for (int32_t j = 0; j < appended_strings[i]; j++) {
for (int32_t j = 0; j < str_len; j++) {
buffer[current_position + j] = s[j];
}

current_position += appended_strings[i];
current_position += str_len;

c_string_elem++;
}
Expand Down
8 changes: 4 additions & 4 deletions core/string/string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
#define STRING_BUILDER_H

#include "core/string/ustring.h"
#include "core/templates/vector.h"
#include "core/templates/local_vector.h"

class StringBuilder {
uint32_t string_length = 0;

Vector<String> strings;
Vector<const char *> c_strings;
LocalVector<String> strings;
LocalVector<const char *> c_strings;

// -1 means it's a Godot String
// a natural number means C string.
Vector<int32_t> appended_strings;
LocalVector<int32_t> appended_strings;

public:
StringBuilder &append(const String &p_string);
Expand Down