From 09ea4eab062a0419083145742ffe786e1b2f0b9a Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 6 May 2024 01:35:40 -0500 Subject: [PATCH] Crammed LFSR_CAT_NAME to reuse the wasted word in the name lfsr_data_t One annoying thing about lfsr_data_t is when representing in-RAM buffers, the last word of the struct goes completely unused. This commit attempts to save this word of RAM in file names by forcibly (hackily?) truncating the name's lfsr_data_t: .---+---+---+---. . . . . .---+---+---+---. . . . . .---+---+---+---. |0| did_size | |0| did_size | | did_data | +---+---+---+---+ +---+---+---+---+ | | | did_ptr ------. | did_ptr ------. | | +---+---+---+---+ | +---+---+---+---+ | | | | (unused) | | | (unused) | | | | +---+---+---+---+ . | . . +---+---+---+---+ . | . . +---+---+---+---+ |0| name_size | | |0| name_size | | | name_size | +---+---+---+---+ | +---+---+---+---+ | | | | name_ptr | | | name_ptr | | | | +---+---+---+---+ | +---+---+---+---+ | | | | (unused) | | | did | <-' | | +---+---+---+---+ . | . . | | . . . . '---+---+---+---' | did | <-' '---+---+---+---' | | '---+---+---+---' Curiously, this didn't seem to save RAM but saved a bit of code cost? I guess this is because 1. file names, despite being very common, don't occur on the stack hot-path that starts at lfsr_file_sync, and 2. while we don't save stack cost, sometimes the reduced stack pressure can reduce stack manipulation instructions: code stack before: 33728 2776 after: 33672 (-0.2%) 2776 (+0.0%) If we look at the per-function stack cost, we can see the expected minor stack savings in most functions, albeit outside of the stack hot-path: function oframe olimit nframe nlimit dframe dlimit lfsr_file_open 16 2040 16 2032 +0 -8 (+0.0%, -0.4%) lfsr_rename 240 2128 232 2120 -8 -8 (-3.3%, -0.4%) lfsr_remove 176 2064 168 2056 -8 -8 (-4.5%, -0.4%) lfsr_file_opencfg 136 2024 128 2016 -8 -8 (-5.9%, -0.4%) --- lfs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index bfbad9ed..2922720e 100644 --- a/lfs.c +++ b/lfs.c @@ -1510,11 +1510,18 @@ static inline lfsr_cat_t lfsr_cat_fromlleb128(uint32_t word, return LFSR_CAT_BUF(buffer, d); } +// a bit hacky, but we really don't need the last word in our name's +// lfsr_data_t +typedef struct lfsr_data_name { + lfsr_data_t did_data; + lfsr_cat_t name_data; +} lfsr_data_name_t; + #define LFSR_CAT_NAME(_did, _name, _name_size) \ LFSR_CAT_DATAS( \ - ((const lfsr_data_t[2]){ \ + (lfsr_data_t*)(&(lfsr_data_name_t){ \ lfsr_cat_data(LFSR_CAT_LEB128(_did)), \ - LFSR_DATA_BUF(_name, _name_size)}), \ + LFSR_CAT_BUF(_name, _name_size)}), \ 2) // cat <-> bd interactions