Skip to content

Commit

Permalink
Crammed LFSR_CAT_NAME to reuse the wasted word in the name lfsr_data_t
Browse files Browse the repository at this point in the history
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%)
  • Loading branch information
geky committed May 6, 2024
1 parent ff93b0e commit 09ea4ea
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09ea4ea

Please sign in to comment.