Added LFS3_2BONLY for a small 2-block configuration

Like LFS3_RDONLY and LFS3_KVONLY, LFS3_2BONLY opts-out of all of the
logic necessary for filesystems larger than 2-blocks (the mimimum size
of a mutable littlefs image).

This has potential for some pretty big savings:

- No block allocation
- No lookahead buffer
- No btrees (but yes bshrubs)
- No bptrs
- No mtree traversal

Which is I guess ~1/4 of the codebase:

            code           stack           ctx
  default: 37836            2416           636
  2bonly:  27704 (-26.8%)   1872 (-22.5%)  592 (-6.9%)

This can be combined with LFS3_KVONLY for a small key-value store
compatible with the full littlefs driver:

                  code           stack           ctx
  default:       37836            2416           636
  kvonly:        30792 (-18.6%)   2168 (-10.3%)  636 (+0.0%)
  kvonly+2bonly: 22900 (-39.5%)   1736 (-28.1%)  592 (-6.9%)

It may be possible to optimize this further, but, as is the case with
LFS3_KVONLY, balancing config-specific optimization vs maintainability
is tricky.

---

I'm not sure why, but this also reduced the default build's size a bit.
Compiler noise?

           code          stack          ctx
  before: 37860           2416          636
  after:  37836 (-0.1%)   2416 (+0.0%)  636 (+0.0%)
This commit is contained in:
Christopher Haster
2025-06-25 17:40:49 -05:00
parent 2c27c61f25
commit ccfc74a547
3 changed files with 256 additions and 72 deletions
+239 -66
View File
File diff suppressed because it is too large Load Diff
+8 -6
View File
@@ -672,7 +672,7 @@ typedef struct lfs3_bptr {
// sign2(size)=0b10 => on-disk data
// sign2(size)=0b11 => block pointer
lfs3_data_t data;
#ifndef LFS3_CKDATACKSUMREADS
#if !defined(LFS3_2BONLY) && !defined(LFS3_CKDATACKSUMREADS)
// sign(cksize)=0 => block not erased
// sign(cksize)=1 => block erased
lfs3_size_t cksize;
@@ -834,7 +834,9 @@ typedef struct lfs3 {
lfs3_omdir_t *omdirs;
lfs3_mdir_t mroot;
#ifndef LFS3_2BONLY
lfs3_btree_t mtree;
#endif
struct lfs3_rcache {
lfs3_block_t block;
@@ -860,7 +862,7 @@ typedef struct lfs3 {
} ptail;
#endif
#ifndef LFS3_RDONLY
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY)
struct lfs3_lookahead {
lfs3_block_t window;
lfs3_block_t off;
@@ -1110,7 +1112,7 @@ lfs3_ssize_t lfs3_file_read(lfs3_t *lfs3, lfs3_file_t *file,
// actually be updated on the storage until either sync or close is called.
//
// Returns the number of bytes written, or a negative error code on failure.
#if !defined(LFS3_KVONLY) && !defined(LFS3_RDONLY)
#if !defined(LFS3_RDONLY) && !defined(LFS3_KVONLY)
lfs3_ssize_t lfs3_file_write(lfs3_t *lfs3, lfs3_file_t *file,
const void *buffer, lfs3_size_t size);
#endif
@@ -1130,7 +1132,7 @@ lfs3_soff_t lfs3_file_seek(lfs3_t *lfs3, lfs3_file_t *file,
// as if the file was filled with zeros.
//
// Returns a negative error code on failure.
#if !defined(LFS3_KVONLY) && !defined(LFS3_RDONLY)
#if !defined(LFS3_RDONLY) && !defined(LFS3_KVONLY)
int lfs3_file_truncate(lfs3_t *lfs3, lfs3_file_t *file, lfs3_off_t size);
#endif
@@ -1140,7 +1142,7 @@ int lfs3_file_truncate(lfs3_t *lfs3, lfs3_file_t *file, lfs3_off_t size);
// as if the file was filled with zeros.
//
// Returns a negative error code on failure.
#if !defined(LFS3_KVONLY) && !defined(LFS3_RDONLY)
#if !defined(LFS3_RDONLY) && !defined(LFS3_KVONLY)
int lfs3_file_fruncate(lfs3_t *lfs3, lfs3_file_t *file, lfs3_off_t size);
#endif
@@ -1354,7 +1356,7 @@ int lfs3_fs_unck(lfs3_t *lfs3, uint32_t flags);
// Note: This is irreversible.
//
// Returns a negative error code on failure.
#ifndef LFS3_RDONLY
#if !defined(LFS3_RDONLY) && !defined(LFS3_2BONLY)
int lfs3_fs_grow(lfs3_t *lfs3, lfs3_size_t block_count);
#endif
+9
View File
@@ -56,6 +56,9 @@
#ifdef LFS3_YES_KVONLY
#define LFS3_KVONLY
#endif
#ifdef LFS3_YES_2BONLY
#define LFS3_2BONLY
#endif
#ifdef LFS3_YES_REVDBG
#define LFS3_REVDBG
#endif
@@ -206,6 +209,12 @@
#define LFS3_IFDEF_KVONLY(a, b) (b)
#endif
#ifdef LFS3_2BONLY
#define LFS3_IFDEF_2BONLY(a, b) (a)
#else
#define LFS3_IFDEF_2BONLY(a, b) (b)
#endif
#ifdef LFS3_REVDBG
#define LFS3_IFDEF_REVDBG(a, b) (a)
#else