From e31a90d8f38b6742391b08d968aca58e65f00a84 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 5 Jun 2025 15:34:30 -0500 Subject: [PATCH] rdonly: Dropped LFS3_TSTATE_OMDIRS/OBTREE when LFS3_RDONLY If we can't write to the filesystem, we can't out out-of-sync files, so there's no need to traverse open file handles at all. Saves a bit of code in LFS3_RDONLY mode: code stack ctx rdonly before: 10776 840 524 rdonly after: 10664 (-1.0%) 840 (+0.0%) 524 (+0.0%) In theory we could also skip this check when mounted LFS3_M_RDONLY, but checking for that flag would add code and we don't really care about CPU-related performance here. No code changes in default mode. --- lfs3.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lfs3.c b/lfs3.c index 7ec6cb0c..0d68ab78 100644 --- a/lfs3.c +++ b/lfs3.c @@ -9458,7 +9458,6 @@ enum { LFS3_TSTATE_MDIRS = 3, LFS3_TSTATE_MDIR = 4, LFS3_TSTATE_BTREE = 5, - // TODO can we skip open-things when LFS3_RDONLY? LFS3_TSTATE_OMDIRS = 6, LFS3_TSTATE_OBTREE = 7, LFS3_TSTATE_DONE = 8, @@ -9673,12 +9672,16 @@ static int lfs3_mtree_traverse_(lfs3_t *lfs3, lfs3_traversal_t *t, // scan for blocks/btrees in our opened file list case LFS3_TSTATE_OMDIRS:; // reached end of opened files? return to mdir traversal - if (!t->ot) { + if (LFS3_IFDEF_RDONLY( + // we don't need to traverse these if rdonly + true, + !t->ot)) { t->b.o.mdir.mid += 1; lfs3_t_settstate(&t->b.o.flags, LFS3_TSTATE_MDIR); continue; } + #ifndef LFS3_RDONLY // skip unrelated files, we only care about unsync reg files // associated with the current mid // @@ -9708,6 +9711,7 @@ static int lfs3_mtree_traverse_(lfs3_t *lfs3, lfs3_traversal_t *t, *bptr = file->leaf.bptr; return 0; } + #endif continue; @@ -9740,11 +9744,15 @@ static int lfs3_mtree_traverse_(lfs3_t *lfs3, lfs3_traversal_t *t, lfs3_t_settstate(&t->b.o.flags, LFS3_TSTATE_OMDIRS); continue; // end of opened btree? go to next opened file - } else if (lfs3_t_tstate(t->b.o.flags) - == LFS3_TSTATE_OBTREE) { + } else if (LFS3_IFDEF_RDONLY( + false, + lfs3_t_tstate(t->b.o.flags) + == LFS3_TSTATE_OBTREE)) { + #ifndef LFS3_RDONLY t->ot = t->ot->next; lfs3_t_settstate(&t->b.o.flags, LFS3_TSTATE_OMDIRS); continue; + #endif } else { LFS3_UNREACHABLE(); }