Merge pull request #93611 from KoBeWi/path_rush

Optimize `get_path()` in EditorFileSystemDirectory
This commit is contained in:
Rémi Verschelde 2024-08-28 00:11:16 +02:00
commit 7c02e067cc
No known key found for this signature in database
GPG Key ID: C3336907360768E1

View File

@ -95,25 +95,35 @@ String EditorFileSystemDirectory::get_file(int p_idx) const {
}
String EditorFileSystemDirectory::get_path() const {
String p;
const EditorFileSystemDirectory *d = this;
while (d->parent) {
p = d->name.path_join(p);
d = d->parent;
int parents = 0;
const EditorFileSystemDirectory *efd = this;
// Determine the level of nesting.
while (efd->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 file = get_file(p_idx);
const EditorFileSystemDirectory *d = this;
while (d->parent) {
file = d->name.path_join(file);
d = d->parent;
}
return "res://" + file;
return get_path().path_join(get_file(p_idx));
}
Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {