Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 813017d

Browse filesBrowse files
Bugfix fs read+speed improvements (#127)
* Revert "Edited VFSFileImpl::read to use both read/fread (espressif#6456)" This reverts commit 7b89b39. * Added default file buffer size + function to change it by user Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
1 parent d6071c2 commit 813017d
Copy full SHA for 813017d

File tree

5 files changed

+36
-25
lines changed
Filter options

5 files changed

+36
-25
lines changed

‎libraries/FS/src/FS.cpp

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.cpp
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ size_t File::size() const
130130
return _p->size();
131131
}
132132

133+
bool File::setBufferSize(size_t size)
134+
{
135+
if (!*this) {
136+
return 0;
137+
}
138+
139+
return _p->setBufferSize(size);
140+
}
141+
133142
void File::close()
134143
{
135144
if (_p) {

‎libraries/FS/src/FS.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FS.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class File : public Stream
7070
}
7171
size_t position() const;
7272
size_t size() const;
73+
bool setBufferSize(size_t size);
7374
void close();
7475
operator bool() const;
7576
time_t getLastWrite();

‎libraries/FS/src/FSImpl.h

Copy file name to clipboardExpand all lines: libraries/FS/src/FSImpl.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class FileImpl
3636
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
3737
virtual size_t position() const = 0;
3838
virtual size_t size() const = 0;
39+
virtual bool setBufferSize(size_t size) = 0;
3940
virtual void close() = 0;
4041
virtual time_t getLastWrite() = 0;
4142
virtual const char* path() const = 0;

‎libraries/FS/src/vfs_api.cpp

Copy file name to clipboardExpand all lines: libraries/FS/src/vfs_api.cpp
+23-24Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
// limitations under the License.
1414

1515
#include "vfs_api.h"
16-
#include <stdio_ext.h>
1716

1817
using namespace fs;
1918

20-
#define READ_SIZE_SWITCH 128 //swithc to read func when read size > 128bytes
19+
#define DEFAULT_FILE_BUFFER_SIZE 4096
2120

2221
FileImplPtr VFSImpl::open(const char* fpath, const char* mode, const bool create)
2322
{
@@ -283,6 +282,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
283282
if(!_f) {
284283
log_e("fopen(%s) failed", temp);
285284
}
285+
if(_f && (_stat.st_blksize == 0))
286+
{
287+
setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE);
288+
}
286289
} else if(S_ISDIR(_stat.st_mode)) {
287290
_isDirectory = true;
288291
_d = opendir(temp);
@@ -310,6 +313,10 @@ VFSFileImpl::VFSFileImpl(VFSImpl* fs, const char* fpath, const char* mode)
310313
if(!_f) {
311314
log_e("fopen(%s) failed", temp);
312315
}
316+
if(_f && (_stat.st_blksize == 0))
317+
{
318+
setvbuf(_f,NULL,_IOFBF,DEFAULT_FILE_BUFFER_SIZE);
319+
}
313320
}
314321
}
315322
free(temp);
@@ -377,28 +384,7 @@ size_t VFSFileImpl::read(uint8_t* buf, size_t size)
377384
return 0;
378385
}
379386

380-
//ERASE BYTEBUFFER and use read when size > READ_SIZE_SWITCH always
381-
if(size > READ_SIZE_SWITCH)
382-
{
383-
//check some data in buffer exists –> clear buffer and move pointer to deleted data
384-
size_t bytesinbuf = __fpending(_f);
385-
if (bytesinbuf && (bytesinbuf != 128)) //buffer lenght is 128 bytes
386-
{
387-
fpurge(_f);
388-
lseek(fileno(_f),(-128+bytesinbuf),SEEK_CUR);
389-
}
390-
391-
int res = ::read(fileno(_f), buf, size);
392-
if (res < 0) {
393-
// an error occurred
394-
return 0;
395-
}
396-
return res;
397-
}
398-
else
399-
{
400-
return fread(buf, 1, size, _f);
401-
}
387+
return fread(buf, 1, size, _f);
402388
}
403389

404390
void VFSFileImpl::flush()
@@ -439,6 +425,19 @@ size_t VFSFileImpl::size() const
439425
return _stat.st_size;
440426
}
441427

428+
/*
429+
* Change size of files internal buffer used for read / write operations.
430+
* Need to be called right after opening file before any other operation!
431+
*/
432+
bool VFSFileImpl::setBufferSize(size_t size)
433+
{
434+
if(_isDirectory || !_f) {
435+
return 0;
436+
}
437+
int res = setvbuf(_f,NULL,_IOFBF,size);
438+
return res == 0;
439+
}
440+
442441
const char* VFSFileImpl::path() const
443442
{
444443
return (const char*) _path;

‎libraries/FS/src/vfs_api.h

Copy file name to clipboardExpand all lines: libraries/FS/src/vfs_api.h
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ class VFSFileImpl : public FileImpl
6565
bool seek(uint32_t pos, SeekMode mode) override;
6666
size_t position() const override;
6767
size_t size() const override;
68+
bool setBufferSize(size_t size);
6869
void close() override;
6970
const char* path() const override;
7071
const char* name() const override;
71-
time_t getLastWrite() override;
72+
time_t getLastWrite() override;
7273
boolean isDirectory(void) override;
7374
FileImplPtr openNextFile(const char* mode) override;
7475
void rewindDirectory(void) override;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.