Browse Source

Optimize get_path() in EditorFileSystemDirectory

kobewi 1 year ago
parent
commit
5a3c7b41d8
1 changed files with 24 additions and 14 deletions
  1. 24 14
      editor/editor_file_system.cpp

+ 24 - 14
editor/editor_file_system.cpp

@@ -94,25 +94,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://";
+	}
 
-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;
+	// 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);
+}
 
-	return "res://" + file;
+String EditorFileSystemDirectory::get_file_path(int p_idx) const {
+	return get_path().path_join(get_file(p_idx));
 }
 
 Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {