Dropped conditional/noop attrs, prefer incremental attr allocation

So instead of using C's ternary operator everywhere:

  (condition)
      ? LFSR_ATTR(rid, tag, delta, data)
      : LFSR_ATTR_NOOP

Use incremental attr allocation instead:

  lfsr_attr_t attrs[1];
  lfs_size_t attr_count = 0;

  if (condition) {
      attrs[attr_count++] = LFSR_ATTR(rid, tag, delta, data);
  }

  LFS_ASSERT(attr_count <= sizeof(attrs)/sizeof(lfsr_attr_t));

Incremental attr allocation is more flexible, allowing nested conditions
and conditions that span multiple attrs without sacrificing readability,
though at a verbosity cost.

We already need this for lfsr_btree_commit and lfsr_file_carve, adopting
it everywhere we need conditional attrs allows us to drop the noop attr
and avoid messy and hard-to-read C expressions.

This also changes the lfsr_btree_commit to explicitly omit noop grows.
We were relying on lfsr_rbyd_appendattr implicitly skipping these to
avoid unnecessary attr commits, but I think it's probably better to make
these noops explicit.

This does add some code cost though, I'm guessing sequential conditional
attrs landing at different offsets complicates code generation a bit:

            code          stack
  before:  33940           2928
  after:   34052 (+0.3%)   2928 (+0.0%)
This commit is contained in:
Christopher Haster
2024-01-20 15:01:46 -06:00
parent ff6d8a588e
commit 96b62ff804
2 changed files with 87 additions and 60 deletions
+18 -12
View File
@@ -85,18 +85,24 @@ code = '''
return err;
}
return lfsr_btree_commit(lfs, btree, LFSR_ATTRS(
LFSR_ATTR(bid, GROW, +weight1-weight_, NULL()),
LFSR_ATTR(bid-(weight_-1)+weight1-1, TAG(tag1), 0, DATA(data1)),
(lfsr_data_size(&name) > 0
? LFSR_ATTR(bid-(weight_-1)+weight1,
NAME, +weight2, DATA(name))
: LFSR_ATTR_NOOP()),
(lfsr_data_size(&name) > 0
? LFSR_ATTR(bid-(weight_-1)+weight1+weight2-1,
TAG(tag2), 0, DATA(data2))
: LFSR_ATTR(bid-(weight_-1)+weight1,
TAG(tag2), +weight2, DATA(data2)))));
lfsr_attr_t attrs[4];
lfs_size_t attr_count = 0;
attrs[attr_count++] = LFSR_ATTR(bid, GROW, +weight1-weight_, NULL());
attrs[attr_count++] = LFSR_ATTR(bid-(weight_-1)+weight1-1,
TAG(tag1), 0, DATA(data1));
if (lfsr_data_size(&name) > 0) {
attrs[attr_count++] = LFSR_ATTR(bid-(weight_-1)+weight1,
NAME, +weight2, DATA(name));
attrs[attr_count++] = LFSR_ATTR(bid-(weight_-1)+weight1+weight2-1,
TAG(tag2), 0, DATA(data2));
} else {
attrs[attr_count++] = LFSR_ATTR(bid-(weight_-1)+weight1,
TAG(tag2), +weight2, DATA(data2));
}
LFS_ASSERT(attr_count <= sizeof(attrs)/sizeof(lfsr_attr_t));
return lfsr_btree_commit(lfs, btree, attrs, attr_count);
}
'''