rdonly: Let the compiler prune LFS3_TSTATE_OMDIRS/OBTREE

This partially reverts the LFS3_TSTATE_OMDIRS/OBTREE ifdefs, instead
adopting lfs3_m_isrdonly checks that let the compiler prune the
unreachable code paths when compiling with LFS3_RDONLY.

This adds a bit of code to both the default and rdonly builds (the
compiler isn't perfect, but simplifies the codebase:

                   code          stack          ctx
  rdonly before:  10664            840          524
  rdonly after:   10676 (+0.1%)    840 (+0.0%)  524 (+0.0%)

  default before: 37300           2280          636
  default after:  37320 (+0.1%)   2280 (+0.0%)  636 (+0.0%)

Testing the rdonly build is difficult, so minimizing the differences in
the code is quite valuable for maintenance and reliability.

As a plus, the extra ~20 bytes of code in the default build lets us
avoid traversing the omdirs when mounted LFS3_M_RDONLY. This niche
performance optimization isn't really a goal, but it's nice for
LFS3_RDONLY and LFS3_M_RDONLY to match behavior when possible.
This commit is contained in:
Christopher Haster
2025-06-05 15:54:25 -05:00
parent d791576c3f
commit c7923ad1be
+7 -12
View File
@@ -9682,16 +9682,15 @@ 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 (LFS3_IFDEF_RDONLY(
// we don't need to traverse these if rdonly
true,
!t->ot)) {
//
// note we can skip checking opened files if mounted rdonly,
// this saves a bit of code when compiled rdonly
if (lfs3_m_isrdonly(lfs3->flags) || !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
//
@@ -9721,7 +9720,6 @@ static int lfs3_mtree_traverse_(lfs3_t *lfs3, lfs3_traversal_t *t,
*bptr = file->leaf.bptr;
return 0;
}
#endif
continue;
@@ -9754,15 +9752,12 @@ 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_IFDEF_RDONLY(
false,
lfs3_t_tstate(t->b.o.flags)
== LFS3_TSTATE_OBTREE)) {
#ifndef LFS3_RDONLY
} else if (lfs3_m_isrdonly(lfs3->flags)
|| lfs3_t_tstate(t->b.o.flags)
== LFS3_TSTATE_OBTREE) {
t->ot = t->ot->next;
lfs3_t_settstate(&t->b.o.flags, LFS3_TSTATE_OMDIRS);
continue;
#endif
} else {
LFS3_UNREACHABLE();
}