Skip to content

Conversation

@geky
Copy link
Member

@geky geky commented Nov 10, 2022

The previous cycle detection algorithm (a naive check against the largest possible tail list) is simple and gets the job done, but has the potential to take a very long time on disks with many blocks. Brent's algorithm, on the other hand, takes at most 2x the number of blocks in the tail list.

The naive cycle detection algorithm was originally chosen as I was only aware of Floyd's algorithm at the time (the classic tortoise+hare cycle detection). Floyd's algorithm is a poor fit for littlefs as it requires managing two desynced traversals of the tail list, which comes with quite a bit of complexity+code cost. But Brent's algorithm is very well suited for littlefs, requiring only an additional mdir pointer in RAM.

The benefit of adopting Brent's algorithm is not just to speed up cycle detection, but to make cycle detection distinguishable from an infinite loop.

Also added a warning message to help debug when there is an infinite loop. Though this should never happen unless 1. there's a bug in littlefs, or 2. the image on disk is malicious.

@geky geky added enhancement needs minor version new functionality only allowed in minor versions labels Nov 10, 2022
@geky geky force-pushed the brent-cycle-detection branch from 6a041ed to fd9cc9a Compare December 7, 2022 04:48
@geky geky changed the base branch from master to test-and-bench-runners December 7, 2022 04:48
@geky geky force-pushed the test-and-bench-runners branch from 78ebd80 to d677a96 Compare December 7, 2022 05:10
@geky geky force-pushed the brent-cycle-detection branch from fd9cc9a to c4c72fd Compare December 7, 2022 05:22
@geky geky force-pushed the test-and-bench-runners branch from d677a96 to 2d2dd8b Compare December 12, 2022 05:42
@geky geky force-pushed the brent-cycle-detection branch 2 times, most recently from 4f2c237 to 55530d4 Compare December 16, 2022 06:04
@geky geky force-pushed the test-and-bench-runners branch from 076f871 to 17c9665 Compare December 16, 2022 06:18
@geky geky force-pushed the brent-cycle-detection branch from 55530d4 to e4d1c7f Compare December 16, 2022 06:20
@geky geky force-pushed the test-and-bench-runners branch from 17c9665 to 1f37eb5 Compare December 16, 2022 22:47
@geky geky force-pushed the brent-cycle-detection branch from e4d1c7f to cb6c56f Compare December 16, 2022 22:48
The previous cycle detection algorithm (a naive check against the largest
possible tail list) is simple and gets the job done, but has the potential to
take a very long time on disks with many blocks. Brent's algorithm, on
the other hand, takes at most 2x the number of blocks in the tail list.

Originally naive cycle detection was chosen over Floyd's algorithm to
avoid the extra complexity of managing two desynced traversals for every
traversal of the tail list, but Brent's algorithm is very well suited for our
use case, requiring only we keep track of an additional mdir pointer on the
stack as we traverse.
@geky geky force-pushed the brent-cycle-detection branch from cb6c56f to 1278ec1 Compare December 17, 2022 18:41
@geky geky added this to the v2.6 milestone Apr 17, 2023
@geky geky removed the needs minor version new functionality only allowed in minor versions label Apr 26, 2023
@geky geky merged commit 3075472 into test-and-bench-runners Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants