Optimize get_path() in EditorFileSystemDirectory

This commit is contained in:
kobewi 2024-06-25 22:49:36 +02:00
parent 95110ddcb4
commit 5a3c7b41d8

View file

@ -94,25 +94,35 @@ String EditorFileSystemDirectory::get_file(int p_idx) const {
} }
String EditorFileSystemDirectory::get_path() const { String EditorFileSystemDirectory::get_path() const {
String p; int parents = 0;
const EditorFileSystemDirectory *d = this; const EditorFileSystemDirectory *efd = this;
while (d->parent) { // Determine the level of nesting.
p = d->name.path_join(p); while (efd->parent) {
d = d->parent; parents++;
efd = efd->parent;
} }
return "res://" + p; if (parents == 0) {
return "res://";
}
// Using PackedStringArray, because the path is built in reverse order.
PackedStringArray path_bits;
// Allocate an array based on nesting. It will store path bits.
path_bits.resize(parents + 2); // Last String is empty, so paths end with /.
String *path_write = path_bits.ptrw();
path_write[0] = "res:/";
efd = this;
for (int i = parents; i > 0; i--) {
path_write[i] = efd->name;
efd = efd->parent;
}
return String("/").join(path_bits);
} }
String EditorFileSystemDirectory::get_file_path(int p_idx) const { String EditorFileSystemDirectory::get_file_path(int p_idx) const {
String file = get_file(p_idx); return get_path().path_join(get_file(p_idx));
const EditorFileSystemDirectory *d = this;
while (d->parent) {
file = d->name.path_join(file);
d = d->parent;
}
return "res://" + file;
} }
Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const { Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {