Exploring inlined mutable configuration

Currently littlefs uses a separate mutable state struct and immutable
config struct. This lets users place the config struct in ROM where
possible.

However the recent addition of LFS_STATICCFG raises the question of if
this split is still valuable.

If the config is copied into the mutable struct at runtime, this allows
a couple things:
1. Easier user interface, config can be stack allocated, no need to store
   the config struct for the lifetime of littlefs in OSs.
2. Avoids duplication when littlefs would need to change config based on
   observed superblocks, such as LFS_NAME_MAX limits
3. In theory, access to a single struct is faster/smaller as it avoids
   an additional load instruction.

Unfortunately, inlining the dynamic config runs into several issues:

1. The code size actually increases with this change! From digging into
   this it's for a couple reasons:

   - Copying the config over takes code.

   - C has notorious problems with pointer aliasing, accessing
     constants from a const struct actually allows C to assume the
     values aren't going to change in more situations.

     This suggests it may be possible to reduce the code size more by
     loading the config pointer into a variable, but I haven't explored
     this probably not-worth-it optimization.

   - Even assuming deduplication of superblock-dependent configuration,
     the config struct is significantly larger than the mutable struct,
     and it turns out combining these two exceeds the limits of
     immediate-relative-loads, discarding the possible code size
     improvement from avoiding a second dereference.

2. The implementation of dynamic configuration differs significantly from
   the static configuration. This adds mess into the compile-time #ifdefs
   needed to support both options.
This commit is contained in:
Christopher Haster
2020-11-26 10:26:51 -06:00
parent a7cdd563f6
commit f9324d1443
4 changed files with 112 additions and 89 deletions

View File

@@ -12,8 +12,7 @@ code = '''
lfs_formatcfg(&lfs, &cfg) => 0;
// change tail-pointer to invalid pointers
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
@@ -39,8 +38,7 @@ code = '''
lfs_unmount(&lfs) => 0;
// change the dir pointer to be invalid
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
// make sure id 1 == our directory
@@ -88,8 +86,7 @@ code = '''
lfs_unmount(&lfs) => 0;
// change the file pointer to be invalid
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
// make sure id 1 == our file
@@ -140,8 +137,7 @@ code = '''
lfs_file_close(&lfs, &file) => 0;
lfs_unmount(&lfs) => 0;
// change pointer in CTZ skip-list to be invalid
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
// make sure id 1 == our file and get our CTZ structure
@@ -194,8 +190,7 @@ code = '''
lfs_formatcfg(&lfs, &cfg) => 0;
// create an invalid gstate
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
lfs_fs_prepmove(&lfs, 1, (lfs_block_t [2]){
@@ -221,8 +216,7 @@ code = '''
lfs_formatcfg(&lfs, &cfg) => 0;
// change tail-pointer to point to ourself
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
lfs_dir_commit(&lfs, &mdir, LFS_MKATTRS(
@@ -244,8 +238,7 @@ code = '''
lfs_unmount(&lfs) => 0;
// find child
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_block_t pair[2];
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;
@@ -275,8 +268,7 @@ code = '''
lfs_unmount(&lfs) => 0;
// find child
lfs.cfg = &cfg;
lfs_initcommon(&lfs) => 0;
lfs_initcommon(&lfs, &cfg) => 0;
lfs_mdir_t mdir;
lfs_block_t pair[2];
lfs_dir_fetch(&lfs, &mdir, (lfs_block_t[2]){0, 1}) => 0;

View File

@@ -100,7 +100,7 @@ code = '''
lfs_file_open(&lfs, &file, "sequence",
LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC) => 0;
size = lfs_min(lfs.cfg->cache_size, sizeof(buffer)/2);
size = lfs_min(lfs.cfg.cache_size, sizeof(buffer)/2);
lfs_size_t qsize = size / 4;
uint8_t *wb = buffer;
uint8_t *rb = buffer + size;