gc: Reverted aborting useless traversals, hopefully better logic

Unsure if this falls into over-engineering. But note lfs3->flags can
change between gc steps, so the aborting useless traversals is probably
worth keeping if only for that reason.

In the future this is a good function to tweak without worrying about
compatibility issues.

Code changes:

                 code          stack          ctx
  before:       35124           2136          660
  after:        35164 (+0.1%)   2136 (+0.0%)  660 (+0.0%)

                 code          stack          ctx
  gbmap before: 38048           2136          776
  gbmap after:  38088 (+0.1%)   2136 (+0.0%)  776 (+0.0%)
This commit is contained in:
Christopher Haster
2025-12-05 23:42:28 -06:00
parent c34da290a5
commit 775825317d
+36 -13
View File
@@ -16354,20 +16354,45 @@ static int lfs3_fs_gc_(lfs3_t *lfs3, lfs3_mgc_t *mgc,
} }
#endif #endif
while ((lfs3_off_t)steps > 0) {
// do we have any pending work? // do we have any pending work?
while ((flags & (lfs3->flags & LFS3_GC_ALL)) uint32_t pending = flags & (LFS3_GC_ALL & lfs3->flags);
&& (lfs3_off_t)steps > 0) { if (!pending) {
// start a new traversal? break;
if (!lfs3_handle_isopen(lfs3, &mgc->t.h)) { }
lfs3_mgc_init(mgc, flags & (lfs3->flags & LFS3_GC_ALL));
lfs3_handle_open(lfs3, &mgc->t.h);
// prioritize lookahead/gbmap before any work that may need to // prioritize lookahead/gbmap before any work that may need to
// allocate // allocate
if (lfs3_t_islookahead(mgc->t.h.flags)) { #ifndef LFS3_RDONLY
mgc->t.h.flags &= ~( if (lfs3_t_islookahead(pending)) {
LFS3_IFDEF_RDONLY(0, LFS3_GC_MKCONSISTENT) pending &= ~(
| LFS3_IFDEF_RDONLY(0, LFS3_GC_COMPACT)); LFS3_GC_MKCONSISTENT
| LFS3_GC_COMPACT);
}
#endif
// start a new traversal?
if (!lfs3_handle_isopen(lfs3, &mgc->t.h)) {
lfs3_mgc_init(mgc, pending);
lfs3_handle_open(lfs3, &mgc->t.h);
}
// mask out any flags that changed
//
// note that even though our current API prevents flags from
// changing mid-traversal, lfs3->flags can be updated by other
// operations
mgc->t.h.flags &= ~(LFS3_GC_ALL ^ pending);
// will this traversal still make progress? no? start over
if (!(mgc->t.h.flags & (
LFS3_GC_ALL
// don't bother with lookahead/gbmap if we've ckpointed
& ~LFS3_IFDEF_RDONLY(
0,
(lfs3_t_isckpointed(mgc->t.h.flags))
? LFS3_GC_LOOKAHEAD
: 0)))) {
lfs3_handle_close(lfs3, &mgc->t.h);
continue;
} }
// do we really need a full traversal? // do we really need a full traversal?
@@ -16377,7 +16402,6 @@ static int lfs3_fs_gc_(lfs3_t *lfs3, lfs3_mgc_t *mgc,
| LFS3_GC_CKDATA))) { | LFS3_GC_CKDATA))) {
mgc->t.h.flags |= LFS3_T_MTREEONLY; mgc->t.h.flags |= LFS3_T_MTREEONLY;
} }
}
// progress gc // progress gc
lfs3_bptr_t bptr; lfs3_bptr_t bptr;
@@ -16387,7 +16411,6 @@ static int lfs3_fs_gc_(lfs3_t *lfs3, lfs3_mgc_t *mgc,
lfs3_handle_close(lfs3, &mgc->t.h); lfs3_handle_close(lfs3, &mgc->t.h);
return tag; return tag;
} }
// end of traversal? // end of traversal?
if (tag == LFS3_ERR_NOENT) { if (tag == LFS3_ERR_NOENT) {
lfs3_handle_close(lfs3, &mgc->t.h); lfs3_handle_close(lfs3, &mgc->t.h);