Skip to content

Commit 9aa3226

Browse files
lfs_dir_getslice: reverse caching
cache trace show possible caching in lfs_dir_getslice cache recache for block 83 number 23 line=790 cache recache for block 881 number 23 line=790 cache recache for block 929 number 23 line=790 cache recache for block 977 number 23 line=790 cache recache for block 128 number 24 line=790 For the same test than previous patch before patch top read operation (number of flash read size, number of flash op) 15044, 41 15044, 41 15044, 41 15044, 41 16068, 42 total read/write total read=18809040 B write=4287328 B total num read=49477 op write=7732 op after patch top read operation (number of flash read size, number of flash op) 15044, 41 15044, 41 15044, 41 15044, 41 16068, 42 total read/write total read=18852728 B write=4287328 B total num read=45678 op write=7732 op cache recache for block 882 number 2 line=998 cache recache for block 911 number 2 line=998 cache recache for block 930 number 2 line=998 cache recache for block 959 number 2 line=998 cache recache for block 978 number 2 line=998
1 parent acaf3c3 commit 9aa3226

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lfs.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
100100

101101
#define lfs_bd_read(...) lfs_bd_read_raw(__VA_ARGS__, __LINE__)
102102
static int lfs_bd_read_raw(lfs_t *lfs,
103-
const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
103+
const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_ssize_t shint,
104104
lfs_block_t block, lfs_off_t off,
105105
void *buffer, lfs_size_t size, int line) {
106106
uint8_t *data = buffer;
107107
(void)line;
108+
lfs_size_t hint = shint;
109+
int reverse = shint < 0;
110+
if (reverse) {
111+
hint = -hint;
112+
}
108113
if (off+size > lfs->cfg->block_size
109114
|| (lfs->block_count && block >= lfs->block_count)) {
110115
return LFS_ERR_CORRUPT;
@@ -169,10 +174,20 @@ static int lfs_bd_read_raw(lfs_t *lfs,
169174
// load to cache, first condition can no longer fail
170175
LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
171176
rcache->block = block;
172-
rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
177+
lfs_off_t moff = off;
178+
if (reverse) {
179+
lfs_size_t msize;
180+
moff = off + size;
181+
msize = lfs_min(hint, lfs->cfg->cache_size);
182+
if (moff > msize)
183+
moff = lfs_alignup(moff - msize, lfs->cfg->read_size);
184+
else
185+
moff = 0;
186+
}
187+
rcache->off = lfs_aligndown(moff, lfs->cfg->read_size);
173188
rcache->size = lfs_min(
174189
lfs_min(
175-
lfs_alignup(off+hint, lfs->cfg->read_size),
190+
lfs_alignup(moff+hint, lfs->cfg->read_size),
176191
lfs->cfg->block_size)
177192
- rcache->off,
178193
lfs->cfg->cache_size);
@@ -788,7 +803,7 @@ static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir,
788803
off -= lfs_tag_dsize(ntag);
789804
lfs_tag_t tag = ntag;
790805
int err = lfs_bd_read(lfs,
791-
NULL, &lfs->rcache, sizeof(ntag),
806+
NULL, &lfs->rcache, -lfs->cfg->block_size,
792807
dir->pair[0], off, &ntag, sizeof(ntag));
793808
if (err) {
794809
return err;

0 commit comments

Comments
 (0)