Fixed crystal_thresh=0 bugs

There was a mismatch between the lfs3_cfg comment and the actual
crystal_thresh math where crystal_thresh=0 would break things:

- In lfs3_file_flush_, crystal_thresh=0 meant we would never resume
  crystallization, leading to terrible, _terrible_, linear write
  performance.

- In lfs3_file_sync and lfs3_set, it's unclear if small file commit
  optimizations were working properly. I went ahead and added a
  lfs3_max(lfs3->cfg->crystal_thresh, 1) just to be safe.

The other references to crystal_thresh all check for >= crystal_thresh
conditions, so shouldn't be broken (except for an unrelated bug in
lfs3_file_flushset_).

The reason for this is because crystal_thresh=1 is technically the lower
bound for this math. Allowing crystal_thresh=0 is just a convenience,
and honestly allowing it may have a been a bad idea. Maybe we should
require crystal_thresh=1 at minimum? I added a TODO.

All the new v3 config needs revisiting anyways, for defaults, etc.

---

Curiously, this actually saved code? My best guess is maybe some weird
code path in lfs3_file_flush_ was eliminated:

           code          stack          ctx
  before: 37036           2352          684
  after:  37028 (-0.0%)   2352 (+0.0%)  684 (+0.0%)
This commit is contained in:
Christopher Haster
2025-08-22 22:08:01 -05:00
parent 2c67fb1ea2
commit eab526ad9f
2 changed files with 13 additions and 6 deletions
+7 -2
View File
@@ -562,12 +562,17 @@ struct lfs3_cfg {
lfs3_size_t fragment_size;
#endif
// TODO crystal_thresh=0 really just means crystal_thresh=1, should we
// allow crystal_thresh=0? crystal_thresh=0 => block_size/16 or
// block_size/8 is probably a better default. need to benchmark.
// Threshold for compacting multiple fragments into a block. Smaller
// values will crystallize more eagerly, reducing disk usage, but
// increasing the cost of random-writes.
//
// 0 only writes blocks, minimizing disk usage, while -1 or any value >
// block_size only writes fragments, minimizing random-write cost.
// 0 or 1 only writes blocks, minimizing disk usage, while -1 or any
// value > block_size only writes fragments, minimizing random-write
// cost.
#ifndef LFS3_RDONLY
lfs3_size_t crystal_thresh;
#endif