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
+42 -19
View File
@@ -16354,29 +16354,53 @@ static int lfs3_fs_gc_(lfs3_t *lfs3, lfs3_mgc_t *mgc,
} }
#endif #endif
// do we have any pending work? while ((lfs3_off_t)steps > 0) {
while ((flags & (lfs3->flags & LFS3_GC_ALL)) // do we have any pending work?
&& (lfs3_off_t)steps > 0) { uint32_t pending = flags & (LFS3_GC_ALL & lfs3->flags);
if (!pending) {
break;
}
// prioritize lookahead/gbmap before any work that may need to
// allocate
#ifndef LFS3_RDONLY
if (lfs3_t_islookahead(pending)) {
pending &= ~(
LFS3_GC_MKCONSISTENT
| LFS3_GC_COMPACT);
}
#endif
// start a new traversal? // start a new traversal?
if (!lfs3_handle_isopen(lfs3, &mgc->t.h)) { if (!lfs3_handle_isopen(lfs3, &mgc->t.h)) {
lfs3_mgc_init(mgc, flags & (lfs3->flags & LFS3_GC_ALL)); lfs3_mgc_init(mgc, pending);
lfs3_handle_open(lfs3, &mgc->t.h); 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);
// prioritize lookahead/gbmap before any work that may need to // will this traversal still make progress? no? start over
// allocate if (!(mgc->t.h.flags & (
if (lfs3_t_islookahead(mgc->t.h.flags)) { LFS3_GC_ALL
mgc->t.h.flags &= ~( // don't bother with lookahead/gbmap if we've ckpointed
LFS3_IFDEF_RDONLY(0, LFS3_GC_MKCONSISTENT) & ~LFS3_IFDEF_RDONLY(
| LFS3_IFDEF_RDONLY(0, LFS3_GC_COMPACT)); 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?
if (!(mgc->t.h.flags & ( if (!(mgc->t.h.flags & (
LFS3_IFDEF_RDONLY(0, LFS3_GC_LOOKAHEAD) LFS3_IFDEF_RDONLY(0, LFS3_GC_LOOKAHEAD)
| LFS3_GC_CKMETA | LFS3_GC_CKMETA
| LFS3_GC_CKDATA))) { | LFS3_GC_CKDATA))) {
mgc->t.h.flags |= LFS3_T_MTREEONLY; mgc->t.h.flags |= LFS3_T_MTREEONLY;
}
} }
// progress gc // progress gc
@@ -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);