tweedegolf / sequential-storage

A crate for storing data in flash memory with minimal need for erasing pages
Apache License 2.0
99 stars 10 forks source link

Erasing all items #47

Closed ryan-summers closed 2 months ago

ryan-summers commented 5 months ago

Currently, there's a remove_item() API, but there's no way to clear out the entire flash buffer without worrying about the key.

One can do this by erasing the whole range that was given to sequential-storage blindly, but this is often overkill and can be much more flash than actually needs erasing, which can result in unnecessarily long erase times.

As an example, in Stabilizer, we use the second flash bank (1MB) to store our settings, but the settngs themselves are less than a handful of bytes each. The resulting bank erase takes several seconds (~10 or so), when in reality it could likely be done much more quickly.

It seems like it would be a desirable to have a function like remove_all_items(f: &mut Flash, range: Range<u32>) that scans through the range for all sequential-storage items and erases only the flash memory that contains sequential-storage items.

Thoughts?


This came up in https://github.com/quartiq/stabilizer/pull/884#discussion_r1574908716 while discussing a firmware change for reference.

diondokter commented 5 months ago

Ah yeah, I feel like this can be done! Should be the same as the current remove function but without the key check. It still wouldn't be fast, but perhaps faster. And for internal flash it wouldn't lock up.

Good idea! I think I can work on this a bit later this week. But feel free to drop a PR if you feel sufficiently self-nerd-sniped :P

ryan-summers commented 5 months ago

I'll take a look at implementing this over the next day or two and submit a PR :)

jordens commented 5 months ago

erases only the flash memory that contains sequential-storage items

Shouldn't it use its own "overwriting" key-invalidation mechanism instead? I feel like it should only ever need to page-erase when running out of space during store.

diondokter commented 5 months ago

erases only the flash memory that contains sequential-storage items

Shouldn't it use its own "overwriting" key-invalidation mechanism instead? I feel like it should only ever need to page-erase when running out of space during store.

Yes, that's what the remove function does. It 'erases' the items by setting the CRC field to 0. No actual erases are done.