Tweaked LFS3_DBGRBYDBALANCE, adopted lfs3_rheight_t

This tweaks LFS3_DBGRBYDBALANCE to be a bit less intrusive, by putting
the relevant heights in the single lfs3_rheight_t struct.

Also added ifdefs to lfs3_rbyd_lookupnext_ just to make it clear this
code is opt-in.

No code changes.
This commit is contained in:
Christopher Haster
2025-07-16 00:40:24 -05:00
parent 7c1fe0f199
commit 55cc661283
+39 -23
View File
@@ -2799,12 +2799,18 @@ static int lfs3_rbyd_ckecksum(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
} }
#endif #endif
// optional height calculation for debugging rbyd balance
typedef struct lfs3_rheight {
lfs3_size_t height;
lfs3_size_t bheight;
} lfs3_rheight_t;
// needed in lfs3_rbyd_fetch_ if debugging rbyd balance // needed in lfs3_rbyd_fetch_ if debugging rbyd balance
static int lfs3_rbyd_lookupnext_(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd, static int lfs3_rbyd_lookupnext_(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
lfs3_srid_t rid, lfs3_tag_t tag, lfs3_srid_t rid, lfs3_tag_t tag,
lfs3_srid_t *rid_, lfs3_tag_t *tag_, lfs3_rid_t *weight_, lfs3_srid_t *rid_, lfs3_tag_t *tag_, lfs3_rid_t *weight_,
lfs3_data_t *data_, lfs3_data_t *data_,
lfs3_size_t *height_, lfs3_size_t *bheight_); lfs3_rheight_t *rheight_);
// fetch an rbyd // fetch an rbyd
static int lfs3_rbyd_fetch_(lfs3_t *lfs3, static int lfs3_rbyd_fetch_(lfs3_t *lfs3,
@@ -3069,12 +3075,11 @@ static int lfs3_rbyd_fetch_(lfs3_t *lfs3,
lfs3_size_t min_bheight = 0; lfs3_size_t min_bheight = 0;
lfs3_size_t max_bheight = 0; lfs3_size_t max_bheight = 0;
while (true) { while (true) {
lfs3_size_t height; lfs3_rheight_t rheight;
lfs3_size_t bheight;
int err = lfs3_rbyd_lookupnext_(lfs3, rbyd, int err = lfs3_rbyd_lookupnext_(lfs3, rbyd,
rid, tag+1, rid, tag+1,
&rid, &tag, NULL, NULL, &rid, &tag, NULL, NULL,
&height, &bheight); &rheight);
if (err) { if (err) {
if (err == LFS3_ERR_NOENT) { if (err == LFS3_ERR_NOENT) {
break; break;
@@ -3083,10 +3088,18 @@ static int lfs3_rbyd_fetch_(lfs3_t *lfs3,
} }
// find the min/max height and bheight // find the min/max height and bheight
min_height = (min_height) ? lfs3_min(min_height, height) : height; min_height = (min_height)
max_height = (max_height) ? lfs3_max(max_height, height) : height; ? lfs3_min(min_height, rheight.height)
min_bheight = (min_bheight) ? lfs3_min(min_bheight, bheight) : bheight; : rheight.height;
max_bheight = (max_bheight) ? lfs3_max(max_bheight, bheight) : bheight; max_height = (max_height)
? lfs3_max(max_height, rheight.height)
: rheight.height;
min_bheight = (min_bheight)
? lfs3_min(min_bheight, rheight.bheight)
: rheight.bheight;
max_bheight = (max_bheight)
? lfs3_max(max_bheight, rheight.bheight)
: rheight.bheight;
} }
LFS3_DEBUG("Fetched rbyd 0x%"PRIx32".%"PRIx32" w%"PRId32", " LFS3_DEBUG("Fetched rbyd 0x%"PRIx32".%"PRIx32" w%"PRId32", "
"height %"PRId32"-%"PRId32", " "height %"PRId32"-%"PRId32", "
@@ -3151,7 +3164,8 @@ static int lfs3_rbyd_lookupnext_(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
lfs3_srid_t rid, lfs3_tag_t tag, lfs3_srid_t rid, lfs3_tag_t tag,
lfs3_srid_t *rid_, lfs3_tag_t *tag_, lfs3_rid_t *weight_, lfs3_srid_t *rid_, lfs3_tag_t *tag_, lfs3_rid_t *weight_,
lfs3_data_t *data_, lfs3_data_t *data_,
lfs3_size_t *height_, lfs3_size_t *bheight_) { lfs3_rheight_t *rheight_) {
(void)rheight_;
// these bits should be clear at this point // these bits should be clear at this point
LFS3_ASSERT(lfs3_tag_mode(tag) == 0); LFS3_ASSERT(lfs3_tag_mode(tag) == 0);
@@ -3165,12 +3179,12 @@ static int lfs3_rbyd_lookupnext_(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
} }
// optionally find height/bheight for debugging rbyd balance // optionally find height/bheight for debugging rbyd balance
if (height_) { #ifdef LFS3_DBGRBYDBALANCE
*height_ = 0; if (rheight_) {
} rheight_->height = 0;
if (bheight_) { rheight_->bheight = 0;
*bheight_ = 0;
} }
#endif
// keep track of bounds as we descend down the tree // keep track of bounds as we descend down the tree
lfs3_size_t branch = lfs3_rbyd_trunk(rbyd); lfs3_size_t branch = lfs3_rbyd_trunk(rbyd);
@@ -3195,18 +3209,20 @@ static int lfs3_rbyd_lookupnext_(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
lfs3_size_t branch_ = branch + d; lfs3_size_t branch_ = branch + d;
// keep track of height for debugging // keep track of height for debugging
if (height_) { #ifdef LFS3_DBGRBYDBALANCE
*height_ += 1; if (rheight_) {
} rheight_->height += 1;
if (bheight_
// only count black+followed alts towards bheight // only count black+followed alts towards bheight
&& (lfs3_tag_isblack(alt) if (lfs3_tag_isblack(alt)
|| lfs3_tag_follow( || lfs3_tag_follow(
alt, weight, alt, weight,
lower_rid, upper_rid, lower_rid, upper_rid,
rid, tag))) { rid, tag)) {
*bheight_ += 1; rheight_->bheight += 1;
}
} }
#endif
// take alt? // take alt?
if (lfs3_tag_follow( if (lfs3_tag_follow(
@@ -3266,7 +3282,7 @@ static int lfs3_rbyd_lookupnext(lfs3_t *lfs3, const lfs3_rbyd_t *rbyd,
lfs3_data_t *data_) { lfs3_data_t *data_) {
return lfs3_rbyd_lookupnext_(lfs3, rbyd, rid, tag, return lfs3_rbyd_lookupnext_(lfs3, rbyd, rid, tag,
rid_, tag_, weight_, data_, rid_, tag_, weight_, data_,
NULL, NULL); NULL);
} }
// lookup assumes a known rid // lookup assumes a known rid