From 340b786e6b94e74922629edcd2e082d0d4e17948 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 4 Jan 2026 00:59:17 -0600 Subject: [PATCH] alloc: Simplified lookahead-relative block calculation Based on what we implemented for the preerase gbmap known window check, adding one block_count before mod is way simpler than a negative-friendly mod in C. Saves a bit of code: code stack ctx before: 35260 2136 660 after: 35256 (-0.0%) 2136 (+0.0%) 660 (+0.0%) code stack ctx gbmap before: 38616 2144 776 gbmap after: 38612 (-0.0%) 2144 (+0.0%) 776 (+0.0%) code stack ctx preerase before: 39280 2168 796 preerase after: 39276 (-0.0%) 2168 (+0.0%) 796 (+0.0%) --- lfs3.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lfs3.c b/lfs3.c index 9f125953..87a999b5 100644 --- a/lfs3.c +++ b/lfs3.c @@ -10899,16 +10899,10 @@ static inline void lfs3_alloc_discard(lfs3_t *lfs3) { // mark a block as in-use #ifndef LFS3_RDONLY static void lfs3_alloc_setinuse(lfs3_t *lfs3, lfs3_block_t block) { - // TODO can this be simplified? - // translate to lookahead-relative - lfs3_block_t block_ = (( - (lfs3_sblock_t)(block - lfs3->lookahead.window) - // we only need this mess because C's mod is actually rem, and - // we want real mod in case block_ goes negative - % (lfs3_sblock_t)lfs3->block_count) - + (lfs3_sblock_t)lfs3->block_count) - % (lfs3_sblock_t)lfs3->block_count; + lfs3_block_t block_ + = (block + lfs3->block_count - lfs3->lookahead.window) + % lfs3->block_count; if (block_ < 8*lfs3->cfg->lookahead_size) { // mark as in-use @@ -11444,8 +11438,7 @@ static int lfs3_alloc_preerase(lfs3_t *lfs3) { // // we're only actually successful if the gbmap didn't allocate // the block we were trying to erase - // TODO can this be simplified? - if (((block+lfs3->block_count - lfs3->gbmap.window) + if (((block + lfs3->block_count - lfs3->gbmap.window) % lfs3->block_count) < lfs3->gbmap.known) { // increment preeraser