Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/littlefs-project/littlefs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lfs.c
diff options
context:
space:
mode:
authorChristopher Haster <chaster@utexas.edu>2022-04-11 05:52:27 +0300
committerGitHub <noreply@github.com>2022-04-11 05:52:27 +0300
commita94fbda1cdd00d8f34100dc1f6131133b9cc5bdd (patch)
tree01776ed1d33f3d34add0bdb6411f936ab1513eed /lfs.c
parentcc025653ed66aaf0587f4d09e31bed18e89016fa (diff)
parent425dc810a5fa42bc6680a910a7e4ef982eaf75d6 (diff)
Merge pull request #632 from robekras/patch-1
Fix lfs_file_rawseek performance issue
Diffstat (limited to 'lfs.c')
-rw-r--r--lfs.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/lfs.c b/lfs.c
index 8f00f20..0fe4662 100644
--- a/lfs.c
+++ b/lfs.c
@@ -3100,6 +3100,27 @@ static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file,
return npos;
}
+ // if we're only reading and our new offset is still in the file's cache
+ // we can avoid flushing and needing to reread the data
+ if (
+#ifndef LFS_READONLY
+ !(file->flags & LFS_F_WRITING)
+#else
+ true
+#endif
+ ) {
+ int oindex = lfs_ctz_index(lfs, &(lfs_off_t){file->pos});
+ lfs_off_t noff = npos;
+ int nindex = lfs_ctz_index(lfs, &noff);
+ if (oindex == nindex
+ && noff >= file->cache.off
+ && noff < file->cache.off + file->cache.size) {
+ file->pos = npos;
+ file->off = noff;
+ return npos;
+ }
+ }
+
// write out everything beforehand, may be noop if rdonly
int err = lfs_file_flush(lfs, file);
if (err) {