t: Dropped mtinfo/btinfo, just use data/bptr for everything

It's probably a bad reason, but this avoids wasting too much time
figuring out how to name things.

Now most traversal functions return an lfsr_tag_t + lfsr_bptr_t pair,
which is enough to describe the current relevant traversal objects:

  tag=LFSR_TAG_MDIR   => (lfsr_mdir_t*)bptr.data.u.buffer
  tag=LFSR_TAG_BRANCH => (lfsr_rbyd_t*)bptr.data.u.buffer
  tag=LFSR_TAG_DATA   => bptr.data
  tag=LFSR_TAG_BPTR   => bptr

This would be a bit better if lfsr_data_t's buffer field was a void*,
but that would mess with byte-level arithmetic, which is more common
with lfsr_data_ts.

This also adopts the fragmented/optional out-params used elsewhere in
the codebase. I thought this would add quite a bit more stack cost,
since we need redundant tags/bptrs to make lfsr_mtree_traverse/
lfsr_mtree_gc work, but surprisingly not:

           code          stack
  before: 35256           2680
  after:  35228 (-0.1%)   2680 (+0.0%)

It seems we make up the extra stack cost of redundant tags/bptrs by
giving the compiler more stack-alloc flexibility, tighter per-function
return types, and opting-out of tags/bptrs in most low-level traversals:
lfs_alloc mainly.

But if the fragmented/optional out-params is net harmful for code/stack
size, we should reconsider the pattern system-wide. This does probably
deserve a second look in the future...
This commit is contained in:
Christopher Haster
2024-07-05 14:54:38 -05:00
parent 96834c2460
commit 2e6a5be4e3
6 changed files with 486 additions and 384 deletions
+142 -122
View File
@@ -3369,37 +3369,38 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8] |= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8] |= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8] |= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -3484,37 +3485,38 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8] |= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8] |= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8] |= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -3621,37 +3623,38 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8] |= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8] |= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8] |= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -3779,37 +3782,38 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8] |= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8] |= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8] |= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -3921,37 +3925,41 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8]
|= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8]
|= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8]
|= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -4038,37 +4046,41 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8]
|= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8]
|= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8]
|= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -4197,37 +4209,41 @@ code = '''
// a bit hacky, but this catches infinite loops
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_NOENT);
if (err == LFS_ERR_NOENT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
// keep track of seen blocks
seen[mtinfo.u.mdir->rbyd.blocks[1] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[1] % 8);
seen[mtinfo.u.mdir->rbyd.blocks[0] / 8]
|= 1 << (mtinfo.u.mdir->rbyd.blocks[0] % 8);
seen[mdir->rbyd.blocks[1] / 8]
|= 1 << (mdir->rbyd.blocks[1] % 8);
seen[mdir->rbyd.blocks[0] / 8]
|= 1 << (mdir->rbyd.blocks[0] % 8);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
// keep track of seen blocks
seen[mtinfo.u.rbyd->blocks[0] / 8]
|= 1 << (mtinfo.u.rbyd->blocks[0] % 8);
seen[rbyd->blocks[0] / 8]
|= 1 << (rbyd->blocks[0] % 8);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}
@@ -4319,27 +4335,31 @@ code = '''
// assert that we detect the cycle in a reasonable number of iterations
assert(i < 2*BLOCK_COUNT);
lfsr_mtinfo_t mtinfo;
int err = lfsr_mtree_traverse(&lfs, &mt, &mtinfo);
lfsr_tag_t tag;
lfsr_bptr_t bptr;
int err = lfsr_mtree_traverse(&lfs, &mt,
&tag, &bptr);
assert(!err || err == LFS_ERR_CORRUPT);
if (err == LFS_ERR_CORRUPT) {
break;
}
if (mtinfo.tag == LFSR_TAG_MDIR) {
if (tag == LFSR_TAG_MDIR) {
lfsr_mdir_t *mdir = (lfsr_mdir_t*)bptr.data.u.buffer;
printf("traversal: 0x%x mdir 0x{%x,%x}\n",
mtinfo.tag,
mtinfo.u.mdir->rbyd.blocks[0],
mtinfo.u.mdir->rbyd.blocks[1]);
tag,
mdir->rbyd.blocks[0],
mdir->rbyd.blocks[1]);
} else if (mtinfo.tag == LFSR_TAG_BRANCH) {
} else if (tag == LFSR_TAG_BRANCH) {
lfsr_rbyd_t *rbyd = (lfsr_rbyd_t*)bptr.data.u.buffer;
printf("traversal: 0x%x btree 0x%x.%x\n",
mtinfo.tag,
mtinfo.u.rbyd->blocks[0], mtinfo.u.rbyd->trunk);
tag,
rbyd->blocks[0], rbyd->trunk);
} else {
// this shouldn't happen
printf("traversal: 0x%x\n", mtinfo.tag);
printf("traversal: 0x%x\n", tag);
assert(false);
}
}