Open
Conversation
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.
Subject: Refactor: Simplify Circular Buffer Index Calculation in InputBuffer
Hello,
Thank you for maintaining this project. I'd like to contribute a refactoring for the InputBuffer class, specifically the circular buffer implementation in InputBuffer.cpp.
Changes Made:
The original code in the write(uint8_t c), push(const char* data), and read() functions used conditional checks to handle buffer wrap-around. I've replaced this logic with modulo operations (% RXBUFFERSIZE) to simplify the index calculations.
Modified Functions:
size_t InputBuffer::write(uint8_t c): Simplified the write index calculation to (_RXbufferpos + _RXbufferSize) % RXBUFFERSIZE.
bool InputBuffer::push(const char* data): Simplified the index calculation within the loop to (_RXbufferpos + _RXbufferSize + i) % RXBUFFERSIZE. Also optimized it to use the pre-calculated data_size instead of calling strlen repeatedly.
int InputBuffer::read(): Simplified the read position update to (_RXbufferpos + 1) % RXBUFFERSIZE.
Benefits:
Conciseness & Readability: The code is shorter and more clearly expresses the circular buffer logic.
Maintainability: The modulo operator provides a standard and robust way to handle index wrapping.
Minor Efficiency: Removed redundant condition checks and an unnecessary strlen call.
The functional behavior remains entirely unchanged. This is a code cleanliness improvement focused on making the logic more elegant and easier to understand.
I have tested the changes to ensure all buffer operations continue to work correctly, including at the buffer boundaries.
Please let me know if you have any questions or feedback.
Thank you for your time and consideration.
Best regards,
[lwfl1111]