Skip to content

Commit 60fb26a

Browse files
committed
Speed up directory listing by factor 4-5 by improving getChildren function
The speed up is achieved by multiple changes: 1) Use list() instead of listFiles() function. The former only returns a String array containing the relative pathnames, and therefore is faster and consumes less memory. 2) The list() function should only be called once. In the previous implementation, the function was called twice (for directories). The first time it was only checked against null and then thrown away. 3) The Strings do not have a "isHidden" property, obviously. However, hidden files can be easily distinguished by checking if the first character is a dot ("."). Performance tests were done in a directory with 12 sub-directories that have about 900 children in total. The old implementation needed about 400-500 ms to list the directory. With the new implementation, only 100ms are needed.
1 parent 01e459d commit 60fb26a

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,15 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat
141141
}
142142

143143
private fun getChildren(file: File): Int {
144-
if (file.listFiles() == null)
144+
var fileList = file.list()
145+
if (fileList == null)
145146
return 0
146147

147148
if (file.isDirectory) {
148149
return if (mShowHidden) {
149-
file.listFiles()?.size ?: 0
150+
fileList.size
150151
} else {
151-
file.listFiles { file -> !file.isHidden }?.size ?: 0
152+
fileList.count { fileName -> fileName[0] != '.' }
152153
}
153154
}
154155
return 0

0 commit comments

Comments
 (0)