Some optimizations in file system operations.

This commit is contained in:
Martín Lucas Golini
2024-11-10 12:30:44 -03:00
parent 2e1bcfe49e
commit 450b85f3e0
4 changed files with 28 additions and 62 deletions

View File

@@ -58,7 +58,7 @@ class EE_API FileInfo {
const std::string& getFilepath() const;
std::string getFileName() const;
const std::string& getFileName() const;
std::string getDirectoryPath() const;

View File

@@ -160,7 +160,7 @@ const std::string& FileInfo::getFilepath() const {
return mFilepath;
}
std::string FileInfo::getFileName() const {
const std::string& FileInfo::getFileName() const {
return mFileName;
}

View File

@@ -431,31 +431,18 @@ std::vector<String> FileSystem::filesGetInPath( const String& path, const bool&
String fpath( path );
if ( fpath[fpath.size() - 1] != '/' && fpath[fpath.size() - 1] != '\\' )
fpath += getOSSlash();
std::vector<String> folders;
std::vector<String> file;
for ( size_t i = 0; i < files.size(); i++ ) {
if ( FileSystem::isDirectory( fpath + files[i] ) ) {
folders.push_back( files[i] );
} else {
file.push_back( files[i] );
}
}
files.clear();
for ( auto& folder : folders )
files.push_back( folder );
for ( auto& f : file )
files.push_back( f );
std::stable_partition(files.begin(), files.end(), [&fpath](const String& file) {
return FileSystem::isDirectory( fpath + file );
});
}
if ( ignoreHidden ) {
String fpath( path );
if ( fpath[fpath.size() - 1] != '/' && fpath[fpath.size() - 1] != '\\' )
fpath += getOSSlash();
std::vector<String> filtered;
for ( size_t i = 0; i < files.size(); i++ )
if ( !FileSystem::fileIsHidden( fpath + files[i] ) )
filtered.push_back( files[i] );
return filtered;
files.erase(std::remove_if(files.begin(), files.end(), [&fpath](const String& file) {
return FileSystem::fileIsHidden(fpath + file);
}), files.end());
}
return files;
@@ -468,6 +455,7 @@ std::vector<FileInfo> FileSystem::filesInfoGetInPath( std::string path, bool lin
dirAddSlashAtEnd( path );
std::vector<FileInfo> fileInfo;
auto files = filesGetInPath( path, sortByName, foldersFirst, ignoreHidden );
fileInfo.reserve( files.size() );
for ( const auto& file : files )
fileInfo.emplace_back( FileInfo( path + file, linkInfo ) );
return fileInfo;
@@ -533,30 +521,17 @@ std::vector<std::string> FileSystem::filesGetInPath( const std::string& path,
if ( foldersFirst ) {
std::string fpath( path );
dirAddSlashAtEnd( fpath );
std::vector<std::string> folders;
std::vector<std::string> file;
for ( size_t i = 0; i < files.size(); i++ ) {
if ( FileSystem::isDirectory( fpath + files[i] ) ) {
folders.push_back( files[i] );
} else {
file.push_back( files[i] );
}
}
files.clear();
for ( auto& folder : folders )
files.push_back( folder );
for ( auto& f : file )
files.push_back( f );
std::stable_partition(files.begin(), files.end(), [&fpath](const std::string& file) {
return FileSystem::isDirectory( fpath + file );
});
}
if ( ignoreHidden ) {
std::string fpath( path );
dirAddSlashAtEnd( fpath );
std::vector<std::string> filtered;
for ( size_t i = 0; i < files.size(); i++ )
if ( !FileSystem::fileIsHidden( fpath + files[i] ) )
filtered.push_back( files[i] );
return filtered;
files.erase(std::remove_if(files.begin(), files.end(), [&fpath](const std::string& file) {
return FileSystem::fileIsHidden(fpath + file);
}), files.end());
}
return files;

View File

@@ -171,7 +171,7 @@ void FileSystemModel::Node::refresh( const FileSystemModel& model ) {
std::vector<Node*> newChildren;
Node* node = nullptr;
for ( auto file : files ) {
for ( auto& file : files ) {
node = childWithPathExists( file.getFilepath() );
if ( !isAcceptedExtension( displayCfg.acceptedExtensions, file ) )
@@ -197,7 +197,7 @@ void FileSystemModel::Node::refresh( const FileSystemModel& model ) {
for ( Node* oldNode : oldFiles )
eeDelete( oldNode );
mChildren = newChildren;
mChildren = std::move( newChildren );
}
void FileSystemModel::Node::cleanChildren() {
@@ -209,7 +209,6 @@ void FileSystemModel::Node::cleanChildren() {
void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) {
if ( !mInfo.isDirectory() || mHasTraversed )
return;
mHasTraversed = true;
cleanChildren();
const auto& displayCfg = model.getDisplayConfig();
@@ -219,7 +218,7 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) {
const auto& patterns = displayCfg.acceptedExtensions;
bool accepted;
for ( auto file : files ) {
for ( auto& file : files ) {
if ( ( model.getMode() == Mode::DirectoriesOnly &&
( file.isDirectory() || file.linksToDirectory() ) ) ||
model.getMode() == Mode::FilesAndDirectories ) {
@@ -230,9 +229,11 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) {
mChildren.emplace_back( eeNew( Node, ( std::move( file ), this ) ) );
} else {
accepted = false;
if ( patterns.size() ) {
for ( size_t z = 0; z < patterns.size(); z++ ) {
if ( patterns[z] == FileSystem::fileExtension( file.getFilepath() ) ) {
size_t psize = patterns.size();
if ( psize ) {
auto ext( FileSystem::fileExtension( file.getFilepath() ) );
for ( size_t z = 0; z < psize; z++ ) {
if ( patterns[z] == ext ) {
accepted = true;
break;
}
@@ -251,6 +252,7 @@ void FileSystemModel::Node::traverseIfNeeded( const FileSystemModel& model ) {
}
}
}
mHasTraversed = true;
}
void FileSystemModel::Node::refreshIfNeeded( const FileSystemModel& model ) {
@@ -543,6 +545,7 @@ void FileSystemModel::setPreviouslySelectedIndex( const ModelIndex& previouslySe
size_t FileSystemModel::getFileIndex( Node* parent, const FileInfo& file ) {
std::vector<FileInfo> files;
files.reserve( parent->mChildren.size() + 1 );
for ( Node* nodeFile : parent->mChildren ) {
files.emplace_back( nodeFile->info() );
@@ -553,26 +556,14 @@ size_t FileSystemModel::getFileIndex( Node* parent, const FileInfo& file ) {
files.emplace_back( file );
std::sort( files.begin(), files.end(), []( FileInfo a, FileInfo b ) {
std::sort( files.begin(), files.end(), []( const FileInfo& a, const FileInfo& b ) {
return std::strncmp( a.getFileName().c_str(), b.getFileName().c_str(),
a.getFileName().size() ) < 0;
} );
if ( getDisplayConfig().foldersFirst ) {
std::vector<FileInfo> folders;
std::vector<FileInfo> file;
for ( size_t i = 0; i < files.size(); i++ ) {
if ( files[i].isDirectory() ) {
folders.push_back( files[i] );
} else {
file.push_back( files[i] );
}
}
files.clear();
for ( auto& folder : folders )
files.push_back( folder );
for ( auto& f : file )
files.push_back( f );
std::stable_partition( files.begin(), files.end(),
[]( const FileInfo& info ) { return info.isDirectory(); } );
}
size_t pos = parent->childCount();