From 1d6fa2e5f12690564ff8cf34a3e3067c79c6b472 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 29 Dec 2025 23:49:19 -0600 Subject: [PATCH] ecksum: Adopted global constant in lfs3_gbmap_set_ when ecksum is NULL It's two words, and I've probably already spent too long fiddling around with this. This drops the messy NULL checks in lfs3_gbmap_set_, for a wrapper that defaults to a global {.cksize=-1} ecksum when NULL. --- Code changes, with both a compound literal (cl), and global constant (gc). The current code uses a global constant: code stack ctx before: 35144 2136 660 after+cl: 35144 (+0.0%) 2136 (+0.0%) 660 (+0.0%) after+gc: 35144 (+0.0%) 2136 (+0.0%) 660 (+0.0%) code stack ctx gbmap+np before: 38272 2144 776 gbmap+np after+cl: 38412 (+0.4%) 2144 (+0.0%) 776 (+0.0%) gbmap+np after+gc: 38380 (+0.3%) 2144 (+0.0%) 776 (+0.0%) code stack ctx gbmap+yp before: 38940 2168 796 gbmap+yp after+cl: 38952 (+0.0%) 2168 (+0.0%) 796 (+0.0%) gbmap+yp after+gc: 38920 (-0.1%) 2168 (+0.0%) 796 (+0.0%) It's interesting to note that while the global constant generally reduces code cost, it prevents constant-expr optimizations from eliminating the NULL checks when compiling without preerases (np). Is that enough reason to revert this? Probably not. (1) The simpler codebase, and reduced chance of forgetting a NULL check, is preferable, and (2) we don't care about the code cost of niche gbmap configurations as much as the non-gbmap modes. --- lfs3.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lfs3.c b/lfs3.c index 31f629bc..85417cb1 100644 --- a/lfs3.c +++ b/lfs3.c @@ -10507,7 +10507,7 @@ static int lfs3_gbmap_commit(lfs3_t *lfs3, lfs3_btree_t *gbmap, // // the purpose of weight is really just to provide a shortcut for bulk // clearing ranges in lfs3_alloc_lookgbmap -static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, +static int lfs3_gbmap_set__(lfs3_t *lfs3, lfs3_btree_t *gbmap, lfs3_block_t block, lfs3_block_t weight, lfs3_tag_t tag, const lfs3_ecksum_t *ecksum) { // lookup gbmap range @@ -10522,10 +10522,7 @@ static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, } // wait, already set to expected type? guess we're done - if (tag__ == tag - && ((ecksum) - ? lfs3_ecksum_cmp(&ecksum__, ecksum) == 0 - : !lfs3_ecksum_isecksum(&ecksum__))) { + if (tag__ == tag && lfs3_ecksum_cmp(&ecksum__, ecksum) == 0) { return 0; } @@ -10555,10 +10552,7 @@ static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, } LFS3_ASSERT(r_weight == r_bid - block); - if (r_tag == tag - && ((ecksum) - ? lfs3_ecksum_cmp(&r_ecksum, ecksum) == 0 - : !lfs3_ecksum_isecksum(&r_ecksum))) { + if (r_tag == tag && lfs3_ecksum_cmp(&r_ecksum, ecksum) == 0) { // delete to prepare merge int err = lfs3_gbmap_commit(lfs3, &gbmap_, r_bid, LFS3_RATTRS( LFS3_RATTR(2, LFS3_tag_RM, -2), @@ -10590,10 +10584,7 @@ static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, } LFS3_ASSERT(l_bid == block-weight); - if (l_tag == tag - && ((ecksum) - ? lfs3_ecksum_cmp(&l_ecksum, ecksum) == 0 - : !lfs3_ecksum_isecksum(&l_ecksum))) { + if (l_tag == tag && lfs3_ecksum_cmp(&l_ecksum, ecksum) == 0) { // delete to prepare merge int err = lfs3_gbmap_commit(lfs3, &gbmap_, l_bid, LFS3_RATTRS( LFS3_RATTR(2, LFS3_tag_RM, -2), @@ -10623,7 +10614,7 @@ static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, ? LFS3_RATTR(2, LFS3_tag_GROW, -2) : LFS3_RATTR(2, LFS3_tag_RM, -2), LFS3_RATTR_WEIGHT(-((bid__+1) - (block-(weight-1)))), - (ecksum && lfs3_ecksum_isecksum(ecksum)) + (lfs3_ecksum_isecksum(ecksum)) ? LFS3_RATTR(3, tag, -2, LFS3_FROM_ECKSUM) : LFS3_RATTR(3, tag, -2), LFS3_RATTR_WEIGHT(+weight_), @@ -10646,6 +10637,21 @@ static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, } #endif +#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) +static const lfs3_ecksum_t lfs3_gbmap_defaultecksum = {.cksize=-1}; +#endif + +#if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) +static int lfs3_gbmap_set_(lfs3_t *lfs3, lfs3_btree_t *gbmap, + lfs3_block_t block, lfs3_block_t weight, + lfs3_tag_t tag, const lfs3_ecksum_t *ecksum) { + return lfs3_gbmap_set__(lfs3, gbmap, + block, weight, tag, + // default to not-ecksum if NULL + (ecksum) ? ecksum : &lfs3_gbmap_defaultecksum); +} +#endif + #if !defined(LFS3_RDONLY) && defined(LFS3_GBMAP) static int lfs3_gbmap_set(lfs3_t *lfs3, lfs3_btree_t *gbmap, lfs3_block_t block, lfs3_tag_t tag, const lfs3_ecksum_t *ecksum) {