pbio/os: Number protothread checkpoints from one. - #487
Merged
Conversation
Protothread checkpoints used __LINE__ as the resume state, so the switch that dispatches to them spanned the whole line range of the function. GCC emits a jump table covering that range, one entry per line, with most entries pointing at the default case. Where the range exceeded 255 the table also doubled in width, since __gnu_thumb1_case_uqi can only address a byte. Use __COUNTER__ instead, and capture its value in PBIO_OS_ASYNC_BEGIN() so that each protothread numbers its checkpoints from one. The case labels are then dense and start at zero, so the compiler indexes the table directly with no range offset. On the Move Hub this takes the firmware from 105632 to 104112 bytes, which matters because only 864 bytes of the 104K region were left. For example, the dispatch in init_device_information_service() goes from a 42 entry table to 15 entries, and the one in pbdrv_bluetooth_spi_process_thread() from 79 two byte entries to 12 single byte entries. This also means source line numbers no longer affect the size of the binary, so adding a comment to a protothread does not move code around. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Protothread checkpoints used
__LINE__as the resume state, so the switch that dispatches to them spanned the whole line range of the function. GCC emits a jump table covering that range, one entry per line, with most entries pointing at the default case. Where the range exceeded 255 the table also doubled in width, since__gnu_thumb1_case_uqican only address a byte.Use
__COUNTER__instead, and capture its value inPBIO_OS_ASYNC_BEGIN()so that each protothread numbers its checkpoints from one. The case labels are then dense and start at zero, so the compiler indexes the table directly with no range offset.On the Move Hub this takes the firmware from 105632 to 104112 bytes, which matters because only 864 bytes of the 104K region were left. For example, the dispatch in
init_device_information_service()goes from a 42 entry table to 15 entries, and the one inpbdrv_bluetooth_spi_process_thread()from 79 two byte entries to 12 single byte entries.This also means source line numbers no longer affect the size of the binary, so adding a comment to a protothread does not move code around.