btree: Tried to better deduplicate split commit building logic

This may have changed during some refactor, but we can reuse the entire
right branch logic, and at least deduplicate the lfs3_data_frombranch
call on the left branch.

Saves a nice bit of code:

           code          stack          ctx
  before: 37020           2392          652
  after:  36952 (-0.2%)   2376 (-0.7%)  652 (+0.0%)

Also deduplicating the lfs3_data_t allocations saved stack, though that
is more concerning than anything else...

Also adopted l/r_buf names in lfs3_bcommit_t. This better matches names
in lfs3_file_graft_ and elsewhere.
This commit is contained in:
Christopher Haster
2025-07-15 21:20:39 -05:00
parent 3e47304e9b
commit 7c1fe0f199
+28 -35
View File
@@ -5445,8 +5445,8 @@ typedef struct lfs3_bcommit {
struct { struct {
lfs3_rattr_t rattrs[4]; lfs3_rattr_t rattrs[4];
lfs3_data_t split_name; lfs3_data_t split_name;
uint8_t branch_l_buf[LFS3_BRANCH_DSIZE]; uint8_t l_buf[LFS3_BRANCH_DSIZE];
uint8_t branch_r_buf[LFS3_BRANCH_DSIZE]; uint8_t r_buf[LFS3_BRANCH_DSIZE];
} ctx; } ctx;
} lfs3_bcommit_t; } lfs3_bcommit_t;
#endif #endif
@@ -5602,12 +5602,15 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
// end up removing an rbyd here // end up removing an rbyd here
bcommit->bid -= pid - (child.weight-1); bcommit->bid -= pid - (child.weight-1);
lfs3_size_t rattr_count = 0; lfs3_size_t rattr_count = 0;
lfs3_data_t branch;
// drop child?
if (child_->weight == 0) { if (child_->weight == 0) {
// drop child
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR(
LFS3_TAG_RM, -child.weight); LFS3_TAG_RM, -child.weight);
} else { } else {
lfs3_data_t branch = lfs3_data_frombranch( // update child
child_, bcommit->ctx.branch_l_buf); branch = lfs3_data_frombranch(child_, bcommit->ctx.l_buf);
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
LFS3_TAG_BRANCH, 0, LFS3_TAG_BRANCH, 0,
branch.u.buffer, lfs3_data_size(branch)); branch.u.buffer, lfs3_data_size(branch));
@@ -5941,46 +5944,36 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
// prepare commit to parent, tail recursing upwards // prepare commit to parent, tail recursing upwards
LFS3_ASSERT(child_->weight > 0); LFS3_ASSERT(child_->weight > 0);
LFS3_ASSERT(sibling.weight > 0); LFS3_ASSERT(sibling.weight > 0);
// new root? // don't worry about bid if new root, we discard it anyways
bcommit->bid -= pid - (child.weight-1);
rattr_count = 0; rattr_count = 0;
branch = lfs3_data_frombranch(child_, bcommit->ctx.l_buf);
// new root?
if (!lfs3_rbyd_trunk(&parent)) { if (!lfs3_rbyd_trunk(&parent)) {
lfs3_data_t branch_l = lfs3_data_frombranch( // new child
child_, bcommit->ctx.branch_l_buf);
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
LFS3_TAG_BRANCH, +child_->weight, LFS3_TAG_BRANCH, +child_->weight,
branch_l.u.buffer, lfs3_data_size(branch_l)); branch.u.buffer, lfs3_data_size(branch));
lfs3_data_t branch_r = lfs3_data_frombranch(
&sibling, bcommit->ctx.branch_r_buf);
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
LFS3_TAG_BRANCH, +sibling.weight,
branch_r.u.buffer, lfs3_data_size(branch_r));
if (lfs3_tag_suptype(split_tag) == LFS3_TAG_NAME) {
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_DATA(
LFS3_TAG_BNAME, 0,
&bcommit->ctx.split_name);
}
// split root? // split root?
} else { } else {
bcommit->bid -= pid - (child.weight-1); // update child
lfs3_data_t branch_l = lfs3_data_frombranch(
child_, bcommit->ctx.branch_l_buf);
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
LFS3_TAG_BRANCH, 0, LFS3_TAG_BRANCH, 0,
branch_l.u.buffer, lfs3_data_size(branch_l)); branch.u.buffer, lfs3_data_size(branch));
if (child_->weight != child.weight) { if (child_->weight != child.weight) {
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR(
LFS3_TAG_GROW, -child.weight + child_->weight); LFS3_TAG_GROW, -child.weight + child_->weight);
} }
lfs3_data_t branch_r = lfs3_data_frombranch( }
&sibling, bcommit->ctx.branch_r_buf); // new sibling
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF( branch = lfs3_data_frombranch(&sibling, bcommit->ctx.r_buf);
LFS3_TAG_BRANCH, +sibling.weight, bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
branch_r.u.buffer, lfs3_data_size(branch_r)); LFS3_TAG_BRANCH, +sibling.weight,
if (lfs3_tag_suptype(split_tag) == LFS3_TAG_NAME) { branch.u.buffer, lfs3_data_size(branch));
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_DATA( if (lfs3_tag_suptype(split_tag) == LFS3_TAG_NAME) {
LFS3_TAG_BNAME, 0, bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_DATA(
&bcommit->ctx.split_name); LFS3_TAG_BNAME, 0,
} &bcommit->ctx.split_name);
} }
LFS3_ASSERT(rattr_count LFS3_ASSERT(rattr_count
<= sizeof(bcommit->ctx.rattrs) <= sizeof(bcommit->ctx.rattrs)
@@ -6069,13 +6062,13 @@ static int lfs3_btree_commit_(lfs3_t *lfs3,
// prepare commit to parent, tail recursing upwards // prepare commit to parent, tail recursing upwards
LFS3_ASSERT(child_->weight > 0); LFS3_ASSERT(child_->weight > 0);
// build attr list
bcommit->bid -= pid - (child.weight-1); bcommit->bid -= pid - (child.weight-1);
rattr_count = 0; rattr_count = 0;
// merge sibling
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR(
LFS3_TAG_RM, -sibling.weight); LFS3_TAG_RM, -sibling.weight);
lfs3_data_t branch = lfs3_data_frombranch( // update child
child_, bcommit->ctx.branch_l_buf); branch = lfs3_data_frombranch(child_, bcommit->ctx.l_buf);
bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF( bcommit->ctx.rattrs[rattr_count++] = LFS3_RATTR_BUF(
LFS3_TAG_BRANCH, 0, LFS3_TAG_BRANCH, 0,
branch.u.buffer, lfs3_data_size(branch)); branch.u.buffer, lfs3_data_size(branch));