arduino-esp32 icon indicating copy to clipboard operation
arduino-esp32 copied to clipboard

Implements seekDir and getNextFileName on FS Lib to improve performance

Open Lucasczm opened this issue 3 years ago • 3 comments

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

Lucasczm avatar Sep 06 '22 13:09 Lucasczm

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Sep 06 '22 13:09 CLAassistant

For getNextFileName why don't you return the filename directly (or empty String if not available) instead?

me-no-dev avatar Sep 14 '22 08:09 me-no-dev

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

PilnyTomas avatar Sep 16 '22 12:09 PilnyTomas

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!

tueddy avatar Sep 24 '22 21:09 tueddy

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

tueddy avatar Oct 07 '22 19:10 tueddy

@SuGlider waiting on your approval here :)

me-no-dev avatar Nov 02 '22 11:11 me-no-dev

@Lucasczm - I can't rebase it from here. Please do it in your end in order to be able to merge it. Thanks.

SuGlider avatar Dec 07 '22 19:12 SuGlider

@SuGlider updated!

Lucasczm avatar Dec 07 '22 19:12 Lucasczm

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 avatar Dec 07 '22 20:12 tueddy

@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?

BlueAndi avatar Dec 29 '22 23:12 BlueAndi

getNextFileName() works fine in 2.0.6, but how can i detect now if return value is a file or directory?

tueddy avatar Dec 30 '22 20:12 tueddy