From 9f246b33a8259fd7ed4b2da90aca7b15764c699f Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Wed, 16 Jul 2025 21:59:52 -0500 Subject: [PATCH] btree: Tweaked btree root conditions in lfs3_btree_commit_ The child rbyd inherits all of the btree's root state when we hit the root, including the shrub bit. This means we don't need to check child.block == btree.block, since only the btree root can be shrubbed (how would non-root shrubs even work? wait... they could work, but I think it would just end up a worse balanced binary tree? anyways). This lets us reorder things into the rare 3-case if statement, which helps a bit with readability: - !lfs3_rbyd_trunk(&child) || lfs3_rbyd_isshrub(&child) => need root - child.blocks[0] == btree->blocks[0] => is root - otherwise => not root Shaved off some code: code stack ctx before: 36832 2368 656 after: 36828 (-0.0%) 2368 (+0.0%) 656 (+0.0%) --- lfs3.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lfs3.c b/lfs3.c index 1677c8df..c6f38f6f 100644 --- a/lfs3.c +++ b/lfs3.c @@ -5525,24 +5525,22 @@ static int lfs3_btree_commit_(lfs3_t *lfs3, // we will always need our parent, so go ahead and find it lfs3_rbyd_t parent = {.trunk=0, .weight=0}; lfs3_srid_t pid = 0; - // are we root? - if (!lfs3_rbyd_trunk(&child) - || child.blocks[0] == btree->blocks[0]) { - // new root? shrub root? yield the final root commit to - // higher-level btree/bshrub logic - if (!lfs3_rbyd_trunk(&child) - || lfs3_rbyd_isshrub(btree)) { - bcommit->bid = rid; - return (!lfs3_rbyd_trunk(&child)) - ? LFS3_ERR_RANGE - : LFS3_ERR_EXIST; - } + // new root? shrub root? yield the final root commit to + // higher-level btree/bshrub logic + if (!lfs3_rbyd_trunk(&child) || lfs3_rbyd_isshrub(&child)) { + bcommit->bid = rid; + return (!lfs3_rbyd_trunk(&child)) + ? LFS3_ERR_RANGE + : LFS3_ERR_EXIST; + // are we root? + } else if (child.blocks[0] == btree->blocks[0]) { // mark btree as unerased in case of failure, our btree rbyd and // root rbyd can diverge if there's a split, but we would have // marked the old root as unerased earlier anyways lfs3_btree_claim(btree); + // need to lookup child's parent } else { int err = lfs3_btree_parent(lfs3, btree, bcommit->bid, &child, &parent, &pid);