arduino-esp32
arduino-esp32 copied to clipboard
Implements seekDir and getNextFileName on FS Lib to improve performance
Description of Change
Implemented two functions:
- getNextFileName: Get the next file name with better performance than getNextFile function. To improve file listing.
- seekDir: Set the position of directory for improve search.
Examples:
File root = SD.open("/data");
String filename;
//Return true if have next file, false if not
if (root.getNextFileName(filename)) {
Serial.println(filename); //print "file1"
}
Tests scenarios
Tested on PlatformIO and Arduino core v2.0.4 with ESP32 LILYGO PoE Board
Related links
#5002
For getNextFileName why don't you return the filename directly (or empty String if not available) instead?
Tested using this sketch confirming that the new function is way much faster:
new function ~0.24ms per folder read
old function openNextFile ~142ms per folder read
The speed improvement is amazing! For a webserver SD filexplorer the time for listing is currently about 2 seconds (very blocking), with this PR only 20 msecs (hundreds of mp3 files on SD card). Works fine here!
But for the Javascript based file explorer tree i also need the information wether it's a file or directory. Is there a way to get it? Suggestion: add a trailing slash in filename if it's a directory or add an overload function with out-param isDirectory.
`
String VFSFileImpl::getNextFileName() { ... name += fname; // add trailing slash for detecting directory if (file->d_type == DT_DIR) { name = "/" + name; } return name; }
`
Thank's!
I made a speedtest example for PIO here https://github.com/tueddy/FS/tree/main/example Tested my SD-cards with a lot of files in root directory:
Samsung Evo 32GB: getNextFileName(), done reading root-directory 36 elemts in 18 ms = 0.50 ms/folder openNextFile(), done reading root-directory 36 elements in 492 ms = 13.67 ms/elements
Kingston 8GB, a bit older card: getNextFileName(), done reading root-directory 42 elemts in 29 ms = 0.69 ms/folder openNextFile(), done reading root-directory 42 elements in 1119 ms = 26.64 ms/elements
Transcend 16GB: getNextFileName(), done reading root-directory 84 elemts in 83 ms = 0.99 ms/folder openNextFile(), done reading root-directory 84 elements in 6960 ms = 82.86 ms/elements
Scandisk 16GB: getNextFileName(), done reading root-directory 151 elemts in 68 ms = 0.45 ms/folder openNextFile(), done reading root-directory 151 elements in 6872 ms = 45.51 ms/elements
@SuGlider waiting on your approval here :)
@Lucasczm - I can't rebase it from here. Please do it in your end in order to be able to merge it. Thanks.
@SuGlider updated!
Can't test it just at the moment, but can I distinguish whether in the listing is a file or directory with this commit? Made a comment above..
Thank's for your work!
@tueddy The seekDir() implementation will only be useful if the other functions are available in FS and VFS too, see https://github.com/espressif/esp-idf/blob/16a4ee7c36a848ca155791677ce011f3ca75c519/components/newlib/platform_include/sys/dirent.h#L51-L57
@Lucasczm How do you use seekDir() without the other functions?
getNextFileName() works fine in 2.0.6, but how can i detect now if return value is a file or directory?