rattrs: Unreverted implicit lfs3_path_namelen in LFS3_FROM_NAME

May rerevert this in the future, but I'm on the fence.

It's true this only saves a small amount of code, but in theory it also
reduces stack consumption in name-related functions. Currently this
doesn't affect the stack hot-path, which is a bit surprising as this
includes lfs3_set, but it may in the future.

The arguments against this optimization are also a bit weak:

- Non-null-terminated strings - We probably shouldn't optimize for a
  theoretical future feature. If anything, we want to optimize in the
  opposite direction to best measure the theoretical code cost.

- Precomputing strlen early - While this is generally a good idea, our
  rattrs benefit greatly from compact encodings, as rattrs sitting on
  the stack are one of the bigger contributors to our stack hot-path.

So for now I'm unreverting to see how long this optimization makes
sense, but could see this being rereverted in the future.

At the very least we probably want to keep the test changes to make
future testing easier.

---

Saves a bit of code:

                 code          stack          ctx
  before:       35188           2136          660
  after:        35160 (-0.1%)   2136 (+0.0%)  660 (+0.0%)

                 code          stack          ctx
  gbmap before: 38048           2152          772
  gbmap after:  38020 (-0.1%)   2152 (+0.0%)  772 (+0.0%)
This commit is contained in:
Christopher Haster
2025-12-01 17:19:10 -06:00
parent 321e33d5d5
commit 0f7dcf068b
4 changed files with 226 additions and 316 deletions
+10 -12
View File
@@ -3260,6 +3260,7 @@ static int lfs3_rbyd_appendtag(lfs3_t *lfs3, lfs3_rbyd_t *rbyd,
#endif
// needed in lfs3_rbyd_appendrattr_
static inline lfs3_size_t lfs3_path_namelen(const char *path);
static lfs3_data_t lfs3_data_frombranch(const lfs3_rbyd_t *branch,
uint8_t buffer[static LFS3_BRANCH_DSIZE]);
static lfs3_data_t lfs3_data_frombtree(const lfs3_btree_t *btree,
@@ -3398,7 +3399,9 @@ static int lfs3_rbyd_appendrattr_(lfs3_t *lfs3, lfs3_rbyd_t *rbyd,
// name?
} else if (from == LFS3_FROM_NAME) {
ctx.u.name.datas[0] = lfs3_data_fromleb128(args[0], ctx.u.name.buf);
ctx.u.name.datas[1] = LFS3_DATA_BUF((const uint8_t*)args[1], args[2]);
ctx.u.name.datas[1] = LFS3_DATA_BUF(
(const char*)args[1],
lfs3_path_namelen((const char*)args[1]));
datas = ctx.u.name.datas;
data_count = 2;
@@ -11334,12 +11337,11 @@ int lfs3_mkdir(lfs3_t *lfs3, const char *path) {
// process
lfs3_grm_pop(lfs3);
err = lfs3_mdir_commit(lfs3, &mdir, LFS3_RATTRS(
LFS3_RATTR(4, LFS3_tag_MASK12 | LFS3_TAG_DIR,
LFS3_RATTR(3, LFS3_tag_MASK12 | LFS3_TAG_DIR,
(tag == LFS3_ERR_NOENT) ? +1 : 0,
LFS3_FROM_NAME),
LFS3_RATTR_ARG(did),
LFS3_RATTR_ARG(name),
LFS3_RATTR_ARG(name_len),
LFS3_RATTR(2, LFS3_TAG_DID, 0, LFS3_FROM_LEB128),
LFS3_RATTR_ARG(did_),
LFS3_RATTR_NULL));
@@ -11487,12 +11489,11 @@ int lfs3_remove(lfs3_t *lfs3, const char *path) {
// we use a create+delete here to also clear any rattrs
// and trim the entry size
(zombie)
? LFS3_RATTR(4, LFS3_tag_MASK12 | LFS3_TAG_STICKYNOTE, 0,
? LFS3_RATTR(3, LFS3_tag_MASK12 | LFS3_TAG_STICKYNOTE, 0,
LFS3_FROM_NAME)
: LFS3_RATTR(4, LFS3_tag_RM, -1),
: LFS3_RATTR(3, LFS3_tag_RM, -1),
LFS3_RATTR_ARG(did),
LFS3_RATTR_ARG(path),
LFS3_RATTR_ARG(lfs3_path_namelen(path)),
LFS3_RATTR_NULL));
if (err) {
return err;
@@ -11663,12 +11664,11 @@ int lfs3_rename(lfs3_t *lfs3, const char *old_path, const char *new_path) {
// rename our entry, copying all tags associated with the old rid to the
// new rid, while also marking the old rid for removal
err = lfs3_mdir_commit(lfs3, &new_mdir, LFS3_RATTRS(
LFS3_RATTR(4, LFS3_tag_MASK12 | old_tag,
LFS3_RATTR(3, LFS3_tag_MASK12 | old_tag,
(new_tag == LFS3_ERR_NOENT) ? +1 : 0,
LFS3_FROM_NAME),
LFS3_RATTR_ARG(new_did),
LFS3_RATTR_ARG(new_path),
LFS3_RATTR_ARG(lfs3_path_namelen(new_path)),
LFS3_RATTR(2, LFS3_tag_MOVE, 0),
LFS3_RATTR_ARG(&old_mdir),
LFS3_RATTR_NULL));
@@ -12423,10 +12423,9 @@ int lfs3_file_opencfg_(lfs3_t *lfs3, lfs3_file_t *file,
file->b.h.flags |= LFS3_o_UNSYNC;
err = lfs3_file_sync_(lfs3, file, LFS3_RATTRS(
LFS3_RATTR(4, LFS3_TAG_REG, +1, LFS3_FROM_NAME),
LFS3_RATTR(3, LFS3_TAG_REG, +1, LFS3_FROM_NAME),
LFS3_RATTR_ARG(did),
LFS3_RATTR_ARG(path),
LFS3_RATTR_ARG(lfs3_path_namelen(path)),
LFS3_RATTR_NULL));
if (err) {
goto failed;
@@ -12436,10 +12435,9 @@ int lfs3_file_opencfg_(lfs3_t *lfs3, lfs3_file_t *file,
// create a stickynote entry if we don't have one, this
// reserves the mid until first sync
err = lfs3_mdir_commit(lfs3, &file->b.h.mdir, LFS3_RATTRS(
LFS3_RATTR(4, LFS3_TAG_STICKYNOTE, +1, LFS3_FROM_NAME),
LFS3_RATTR(3, LFS3_TAG_STICKYNOTE, +1, LFS3_FROM_NAME),
LFS3_RATTR_ARG(did),
LFS3_RATTR_ARG(path),
LFS3_RATTR_ARG(lfs3_path_namelen(path)),
LFS3_RATTR_NULL));
if (err) {
goto failed;