rbyd: Progress towards rbyd balance, separated y-pruning, hacky goto

The main idea here is we need to unconditionally descend down red/yellow
alts to check if they're post-split yellow nodes before pruning, since
yellow nodes should be recolored to maintain the color balance of our
tree.

To make this work:

- Added separate y-pruning logic before our diverged logic, this starts
  to look a bit like previous incarnations of this function.

- Our non-y-pruning logic is all now gated behind the if-diverging
  check, so we shouldn't be non-y-pruning at all unless we've diverged.

  It may be worth rewriting this to use the trimming logic directly,
  instead of reachability.

- Moving non-y-pruning logic behind the if-diverging check ended up
  causing problems for the stitching node, which is still a bit of a
  special case. A hacky goto solves this for now at the risk of
  velociraptors... https://xkcd.com/292

It's hacky, but the goal right now is to just get something working.

This may seem like a lot of changes for only a couple more tests
passing, but progress is progress:

  test_rbyd+balance before: 306/385878 failed
  test_rbyd+balance after:  300/385878 failed (-2.0%)

Code changes:

           code          stack          ctx
  before: 38528           2624          640
  after:  38644 (+0.3%)   2624 (+0.0%)  640 (+0.0%)
This commit is contained in:
Christopher Haster
2025-01-25 13:20:21 -06:00
parent 192779ae84
commit ee3a1374eb
+101 -31
View File
@@ -3619,6 +3619,52 @@ trunk:;
LFS_SWAP(lfs_size_t, &jump, &branch_); LFS_SWAP(lfs_size_t, &jump, &branch_);
} }
// TODO redoc?
// prune?
if (lfsr_tag_isred(p[0].alt)) {
// prune unreachable red alts
// <b >b
// .-'| .-'|
// <y | | |
// .-------'| | | |
// | <r | => | >b
// | .----' | .--------|-'|
// | | <b | <b |
// | | .----'| | .----'| |
// 1 2 3 4 4 1 2 3 4 4 1
if (lfsr_tag_unreachable(
p[0].alt, p[0].weight,
lower_rid, upper_rid,
lower_tag, upper_tag)) {
LFS_DEBUG("%04x->%04x: yprune",
branch, lfsr_rbyd_eoff(rbyd));
alt &= ~LFSR_TAG_R;
lfsr_rbyd_p_pop(p);
// prune other unreachable alts
// <b >b
// .-'| .-'|
// <y | | <b
// .-------'| | .-----------|-'|
// | <r | => | | |
// | .----' | | | |
// | | <b | <b |
// | | .----'| | .----'| |
// 1 2 3 4 4 1 2 3 4 4 2
} else if (lfsr_tag_unreachable2(
alt, weight,
p[0].alt, p[0].weight,
lower_rid, upper_rid,
lower_tag, upper_tag)) {
LFS_DEBUG("%04x->%04x: rprune",
branch, lfsr_rbyd_eoff(rbyd));
alt = p[0].alt & ~LFSR_TAG_R;
weight = p[0].weight;
jump = p[0].jump;
lfsr_rbyd_p_pop(p);
}
}
// TODO need this? does this ever get triggered? // TODO need this? does this ever get triggered?
// both diverging? collapse // both diverging? collapse
// <r >b // <r >b
@@ -3638,7 +3684,7 @@ trunk:;
lower_rid, upper_rid, lower_rid, upper_rid,
a_rid, a_tag, a_rid, a_tag,
b_rid, b_tag); b_rid, b_tag);
if (diverging && diverging_red) { if (!diverged && diverging && diverging_red) {
LFS_DEBUG("%04x->%04x: both diverging", LFS_DEBUG("%04x->%04x: both diverging",
branch, lfsr_rbyd_eoff(rbyd)); branch, lfsr_rbyd_eoff(rbyd));
LFS_ASSERT(a_rid < b_rid || a_tag < b_tag); LFS_ASSERT(a_rid < b_rid || a_tag < b_tag);
@@ -3705,7 +3751,7 @@ trunk:;
&& (diverging || diverging_red)) { && (diverging || diverging_red)) {
LFS_DEBUG("%04x->%04x: diverging", LFS_DEBUG("%04x->%04x: diverging",
branch, lfsr_rbyd_eoff(rbyd)); branch, lfsr_rbyd_eoff(rbyd));
if (lfsr_tag_isred(p[0].alt)) { if (lfsr_tag_isred(alt)) {
LFS_DEBUG("%04x->%04x: wouldnt've diverged", LFS_DEBUG("%04x->%04x: wouldnt've diverged",
branch, lfsr_rbyd_eoff(rbyd)); branch, lfsr_rbyd_eoff(rbyd));
} }
@@ -3758,6 +3804,8 @@ trunk:;
// TODO should we assert we're only diverging here? // TODO should we assert we're only diverging here?
// not diverging_red? // not diverging_red?
// TODO can this be red? can we assert it's black?
// TODO is this uh, how much of this is already in // TODO is this uh, how much of this is already in
// the diverging alt? // the diverging alt?
@@ -3809,14 +3857,52 @@ trunk:;
// // continue to next alt // // continue to next alt
// branch = branch_; // branch = branch_;
// continue; // continue;
goto maybetrim;
} }
// diverged? // diverged?
// : : // : :
// <b => nb // <b => nb
// .-'| .--' // .-'| .--'
// 3 4 3 4 x // 3 4 3 4 x
} else if (diverged && diverging) { } else if (diverged) {
LFS_DEBUG("%04x->%04x: div pruning", // diverging = lfsr_tag_diverging2(
// alt, weight,
// p[0].alt, p[0].weight,
// lower_rid, upper_rid,
// a_rid, a_tag,
// b_rid, b_tag);
// diverging_red = lfsr_tag_isred(p[0].alt)
// && lfsr_tag_diverging(
// p[0].alt, p[0].weight,
// lower_rid, upper_rid,
// a_rid, a_tag,
// b_rid, b_tag);
// if (diverging_red) {
// LFS_DEBUG("%04x->%04x: div r pruning",
// branch, lfsr_rbyd_eoff(rbyd));
// // trim so alt is pruned
// lfsr_tag_trim(
// p[0].alt, p[0].weight,
// &lower_rid, &upper_rid,
// &lower_tag, &upper_tag);
// p[0].weight = 0;
//
// lfsr_rbyd_p_pop(p);
//
// // TODO prune? (trim?)
// }
diverging = lfsr_tag_diverging2(
alt, weight,
p[0].alt, p[0].weight,
lower_rid, upper_rid,
a_rid, a_tag,
b_rid, b_tag);
if (diverging) {
// && (!lfsr_tag_isred(alt)
// || lfsr_tag_isred(p[0].alt))) {
LFS_DEBUG("%04x->%04x: div b pruning",
branch, lfsr_rbyd_eoff(rbyd)); branch, lfsr_rbyd_eoff(rbyd));
// trim so alt is pruned // trim so alt is pruned
lfsr_tag_trim( lfsr_tag_trim(
@@ -3824,33 +3910,8 @@ trunk:;
&lower_rid, &upper_rid, &lower_rid, &upper_rid,
&lower_tag, &upper_tag); &lower_tag, &upper_tag);
weight = 0; weight = 0;
}
// prune?
//
// note if only yellow pruning this could be much simpler
// prune unreachable red alts
// <b >b
// .-'| .-'|
// <y | | |
// .-------'| | | |
// | <r | => | >b
// | .----' | .--------|-'|
// | | <b | <b |
// | | .----'| | .----'| |
// 1 2 3 4 4 1 2 3 4 4 1
if (lfsr_tag_isred(p[0].alt)
&& lfsr_tag_unreachable(
p[0].alt, p[0].weight,
lower_rid, upper_rid,
lower_tag, upper_tag)) {
LFS_DEBUG("%04x->%04x: yprune",
branch, lfsr_rbyd_eoff(rbyd));
alt &= ~LFSR_TAG_R;
lfsr_rbyd_p_pop(p);
}
maybetrim:;
// prune other unreachable alts // prune other unreachable alts
// <b >b // <b >b
// .-'| .-'| // .-'| .-'|
@@ -3874,12 +3935,15 @@ trunk:;
// | .-'| | .-----' // | .-'| | .-----'
// 1 2 3 1 2 3 x // 1 2 3 1 2 3 x
if (lfsr_tag_isred(p[0].alt)) { if (lfsr_tag_isred(p[0].alt)) {
LFS_DEBUG("%04x->%04x: rprune",
branch, lfsr_rbyd_eoff(rbyd));
alt = p[0].alt & ~LFSR_TAG_R; alt = p[0].alt & ~LFSR_TAG_R;
weight = p[0].weight; weight = p[0].weight;
jump = p[0].jump; jump = p[0].jump;
lfsr_rbyd_p_pop(p); lfsr_rbyd_p_pop(p);
// TODO redoc // TODO redoc
// TODO does this ever get hit?
// prune unreachable root alts and red alts // prune unreachable root alts and red alts
// : : // : :
// <r => <b // <r => <b
@@ -3888,6 +3952,8 @@ trunk:;
// | .-'| | .--' // | .-'| | .--'
// 3 4 5 3 4 5 x // 3 4 5 3 4 5 x
} else if (!p[0].alt) { //|| lfsr_tag_isred(alt)) { } else if (!p[0].alt) { //|| lfsr_tag_isred(alt)) {
LFS_DEBUG("%04x->%04x: zprune",
branch, lfsr_rbyd_eoff(rbyd));
branch = branch_; branch = branch_;
continue; continue;
@@ -3899,7 +3965,9 @@ trunk:;
// <b => nb // <b => nb
// .-'| .--' // .-'| .--'
// 3 4 3 4 x // 3 4 3 4 x
} else { } else if (!lfsr_tag_isred(alt)) {
LFS_DEBUG("%04x->%04x: bprune",
branch, lfsr_rbyd_eoff(rbyd));
alt = LFSR_TAG_ALT( alt = LFSR_TAG_ALT(
LFSR_TAG_B, LFSR_TAG_B,
LFSR_TAG_LE, LFSR_TAG_LE,
@@ -3915,6 +3983,8 @@ trunk:;
jump = 0; jump = 0;
} }
} }
}
}
// two reds makes a yellow, split? // two reds makes a yellow, split?
// //