Reworked lfsr_bshrub_t, renamed file.o -> file.b

This moves all of the shrub tracking logic from lfsr_obshrub_t into
lfsr_bshrub_t, completely drops the lfsr_obshrub_t type, and changes all
lfsr_bshrub_* functions to take lfsr_bshrub_t instead of the mdir+shrub
pair.

This makes the lfsr_bshrub_* functions <-> lfsr_bshrub_t relationship
more consistent with other APIs, such as lfsr_btree_t:

  - lfsr_bshrub_lookupnext(lfs, &file->o.o.mdir, &file->o.bshrub, ...)
  + lfsr_bshrub_lookupnext(lfs, &file->b, ...)

I think the reason why this design wasn't obvious before is because, at
least conceptually, having the lfsr_mdir_t live inside the lfsr_bshrub_t
is a bit weird. It's only thanks to lfsr_file_t invasively using the
internal lfsr_mdir_t that we can avoid duplicate lfsr_mdir_t objects.

This also reorganizes the structs in lfs.h a bit, and renames the
related file.o -> file.b fields (much needed because lfs->gc.t.o.o.mdir.
rbyd.blocks was starting to get _real_ confusing).

---

Unfortunately, reducing the number of arguments to lfsr_bshrub_*
functions did not save nearly as much code as I thought it would. It
even ended up with a net _increase_ of code, apparently due to needing
to recalculate the bshrub->shrub offset more often:

           code          stack          ctx
  before: 36476           2608          640
  after:  36484 (+0.0%)   2608 (+0.0%)  640 (+0.0%)

Strange, but this rework is still worthwhile if only for the code
readability.
This commit is contained in:
Christopher Haster
2025-02-03 03:07:30 -06:00
parent fd62ef9674
commit bc639b03f2
7 changed files with 487 additions and 506 deletions
+53 -53
View File
@@ -5397,7 +5397,7 @@ code = '''
// hack, don't use the internals like this
uint8_t wbuf[SIZE];
while ((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -5433,7 +5433,7 @@ code = '''
lfsr_traversal_read(&lfs, &t, &tinfo) => LFS_ERR_NOENT;
// mdir should have been compacted
assert((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// but because we mutated, we're still marked as uncompacted
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5455,7 +5455,7 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdir should have been compacted
assert((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5649,7 +5649,7 @@ code = '''
// hack, don't use the internals like this
uint8_t wbuf[SIZE];
while ((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -5687,7 +5687,7 @@ code = '''
lfsr_traversal_read(&lfs, &t, &tinfo) => LFS_ERR_NOENT;
// mdir should have been compacted
assert((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// but because we mutated, we're still marked as uncompacted
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5709,7 +5709,7 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdir should have been compacted
assert((file.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5782,10 +5782,10 @@ code = '''
assert(lfs.mtree.weight == 0);
// we need internals to check this
lfs_ssize_t estimate = lfsr_mdir_estimate__(&lfs,
&file1.o.o.mdir, -1, -1,
&file1.b.o.mdir, -1, -1,
NULL);
assert(estimate >= 0);
if ((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) > GC_COMPACT_THRESH
if ((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) > GC_COMPACT_THRESH
&& estimate > BLOCK_SIZE/2) {
break;
}
@@ -5839,8 +5839,8 @@ code = '''
lfsr_traversal_read(&lfs, &t, &tinfo) => LFS_ERR_NOENT;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// but because we mutated, we're still marked as uncompacted
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5862,8 +5862,8 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -5973,7 +5973,7 @@ code = '''
// write to each file until mdir >gc_compact_thresh full
if (COMPACTSET & 0x1) {
// hack, don't use the internals like this
while ((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file1) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf1[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -5985,7 +5985,7 @@ code = '''
if (COMPACTSET & 0x2) {
// hack, don't use the internals like this
while ((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file2) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf2[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -5997,7 +5997,7 @@ code = '''
if (COMPACTSET & 0x4) {
// hack, don't use the internals like this
while ((file3.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file3.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file3) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf3[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -6053,9 +6053,9 @@ code = '''
if (COMPACTSET) {
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// but because we mutated, we're still marked as uncompacted
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6078,9 +6078,9 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6212,10 +6212,10 @@ code = '''
assert(lfs.mtree.weight == orig);
// we need internals to check this
lfs_ssize_t estimate = lfsr_mdir_estimate__(&lfs,
&file2.o.o.mdir, -1, -1,
&file2.b.o.mdir, -1, -1,
NULL);
assert(estimate >= 0);
if ((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) > GC_COMPACT_THRESH
if ((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) > GC_COMPACT_THRESH
&& estimate > BLOCK_SIZE/2) {
break;
}
@@ -6279,10 +6279,10 @@ code = '''
lfsr_traversal_read(&lfs, &t, &tinfo) => LFS_ERR_NOENT;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file4.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file4.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// but because we mutated, we're still marked as uncompacted
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6304,10 +6304,10 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file4.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file3.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file4.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6467,8 +6467,8 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// and we should be marked as consistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6768,8 +6768,8 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// and we should be marked as consistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -6926,8 +6926,8 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// and we should be marked as consistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -7095,8 +7095,8 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// and we should be marked as consistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -7262,8 +7262,8 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// and we should be marked as consistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -7360,7 +7360,7 @@ code = '''
// write to our mdirs until >gc_compact_thresh full
//
// hack, don't use the internals like this
while ((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file1) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf1[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -7369,7 +7369,7 @@ code = '''
lfsr_file_sync(&lfs, &file1) => 0;
}
while ((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file2) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf2[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -7427,12 +7427,12 @@ code = '''
// which means there shouldn't be that many files left
assert(lfs.mtree.weight <= (2 << lfs.mdir_bits));
assert(file1.o.o.mdir.rbyd.weight <= 3);
assert(file2.o.o.mdir.rbyd.weight <= 3);
assert(file1.b.o.mdir.rbyd.weight <= 3);
assert(file2.b.o.mdir.rbyd.weight <= 3);
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// we should be marked as consistent, but because we mutated, we're
// still marked as uncompacted
@@ -7455,8 +7455,8 @@ code = '''
lfsr_traversal_close(&lfs, &t) => 0;
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// uncompacted flag should have been cleared
lfsr_fs_stat(&lfs, &fsinfo) => 0;
@@ -7534,7 +7534,7 @@ code = '''
// write to our mdirs until >gc_compact_thresh full
//
// hack, don't use the internals like this
while ((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file1) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf1[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -7543,7 +7543,7 @@ code = '''
lfsr_file_sync(&lfs, &file1) => 0;
}
while ((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
while ((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH) {
lfsr_file_rewind(&lfs, &file2) => 0;
for (lfs_size_t j = 0; j < SIZE; j++) {
wbuf2[j] = 'a' + (TEST_PRNG(&prng) % 26);
@@ -7622,8 +7622,8 @@ code = '''
}
// mdirs should have been compacted
assert((file1.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.o.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file1.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
assert((file2.b.o.mdir.rbyd.eoff & 0x7fffffff) <= GC_COMPACT_THRESH);
// if we introduced actual orphans, we _must_ be marked as inconsistent
lfsr_fs_stat(&lfs, &fsinfo) => 0;