From 892f75d43020d9c757aa46f43a180c762e647271 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 30 May 2024 21:23:44 +0300 Subject: [PATCH] QDirListing: optimize recursive iteration By moving the "isDir" check to the end of checkAndPushDirectory(). The other checks could be done without calling stat(), depending on the implementation. E.g. readdir on Linux (and BSD) fills the d_type field in `struct dirent`, so we know if it's a symlink without stat'ing, but to know if it's a symlink to a dir, we'll have to stat. Thanks to Thiago for pointing this out in code review. Change-Id: Iad9e954413afddfd4eb4890fb475655cda668385 Reviewed-by: Thiago Macieira (cherry picked from commit 104f8b23f236b1abd840eeaa4b82f54412d8a3b5) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/io/qdirlisting.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qdirlisting.cpp b/src/corelib/io/qdirlisting.cpp index 13d4babd8d1..cca56dcdfd6 100644 --- a/src/corelib/io/qdirlisting.cpp +++ b/src/corelib/io/qdirlisting.cpp @@ -370,10 +370,6 @@ void QDirListingPrivate::checkAndPushDirectory(QDirEntryInfo &entryInfo) if (!iteratorFlags.testAnyFlags(F::Recursive)) return; - // Never follow non-directory entries - if (!entryInfo.isDir()) - return; - // Follow symlinks only when asked if (!iteratorFlags.testAnyFlags(F::FollowDirSymlinks) && entryInfo.isSymLink()) return; @@ -391,6 +387,10 @@ void QDirListingPrivate::checkAndPushDirectory(QDirEntryInfo &entryInfo) if (!includeHidden && entryInfo.isHidden()) return; + // Never follow non-directory entries + if (!entryInfo.isDir()) + return; + pushDirectory(entryInfo); }